@@ -591,6 +591,8 @@ rather than C-style casts. There are two exceptions to this:
591
591
592
592
* When casting to ``void `` to suppress warnings about unused variables (as an
593
593
alternative to ``[[maybe_unused]] ``). Prefer C-style casts in this instance.
594
+ Note that if the variable is unused because its used only in ``assert ``, use
595
+ the ``[[maybe_unused]] `` instead of a C-style void cast.
594
596
595
597
* When casting between integral types (including enums that are not strongly-
596
598
typed), functional-style casts are permitted as an alternative to
@@ -1288,17 +1290,26 @@ These are two interesting different cases. In the first case, the call to
1288
1290
``V.size() `` is only useful for the assert, and we don't want it executed when
1289
1291
assertions are disabled. Code like this should move the call into the assert
1290
1292
itself. In the second case, the side effects of the call must happen whether
1291
- the assert is enabled or not. In this case, the value should be cast to void to
1292
- disable the warning. To be specific, it is preferred to write the code like
1293
- this:
1293
+ the assert is enabled or not. In this case, the value should be defined using
1294
+ the `` [[maybe_unused]] `` attribute to suppress the warning. To be specific, it is
1295
+ preferred to write the code like this:
1294
1296
1295
1297
.. code-block :: c++
1296
1298
1297
1299
assert(V.size() > 42 && "Vector smaller than it should be");
1298
1300
1299
- bool NewToSet = Myset.insert(Value); (void)NewToSet ;
1301
+ [[maybe_unused]] bool NewToSet = Myset.insert(Value);
1300
1302
assert(NewToSet && "The value shouldn't be in the set yet");
1301
1303
1304
+ In C code where ``[[maybe_unused]] `` is not supported, use ``void `` cast to
1305
+ suppress unused variable warning as follows:
1306
+
1307
+ .. code-block :: c
1308
+
1309
+ LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD);
1310
+ assert(LLVMIsAValueAsMetadata(Value) == NULL);
1311
+ (void)Value;
1312
+
1302
1313
Do Not Use ``using namespace std ``
1303
1314
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1304
1315
0 commit comments