From 97928d136a15b30954c4bf7d4b57ba62301a830e Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 12 Nov 2010 00:19:41 +0000 Subject: describe the preferred approach to silencing 'unused variable warnings' due to asserts. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@118863 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/CodingStandards.html | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'docs') diff --git a/docs/CodingStandards.html b/docs/CodingStandards.html index e3ec3ff..416c29b 100644 --- a/docs/CodingStandards.html +++ b/docs/CodingStandards.html @@ -851,6 +851,38 @@ return 0; +

Another issue is that values used only by assertions will produce an "unused + value" warning when assertions are disabled. For example, this code will warn: +

+ +
+
+  unsigned Size = V.size(); 
+  assert(Size > 42 && "Vector smaller than it should be");
+
+  bool NewToSet = Myset.insert(Value);
+  assert(NewToSet && "The value shouldn't be in the set yet");  
+
+
+ +

These are two interesting different cases: in the first case, the call to +V.size() is only useful for the assert, and we don't want it executed when +assertions are disabled. Code like this should move the call into the assert +itself. In the second case, the side effects of the call must happen whether +the assert is enabled or not. In this case, the value should be cast to void +to disable the warning. To be specific, it is preferred to write the code +like this:

+ +
+
+  assert(V.size() > 42 && "Vector smaller than it should be");
+
+  bool NewToSet = Myset.insert(Value); (void)NewToSet;
+  assert(NewToSet && "The value shouldn't be in the set yet");  
+
+
+ + -- cgit v1.1