Skip to content

Commit c62a5f8

Browse files
committed
[NFC][CodingStandard] Deprecate use of void casts to suppress warnings
Recommend using attribute `[[maybe_unused]`` for variables that may be unused in non-assert enabled builds to suppress unused variable warnings.
1 parent 50c5704 commit c62a5f8

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

llvm/docs/CodingStandards.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,7 @@ Prefer C++-style casts
587587
^^^^^^^^^^^^^^^^^^^^^^
588588

589589
When casting, use ``static_cast``, ``reinterpret_cast``, and ``const_cast``,
590-
rather than C-style casts. There are two exceptions to this:
591-
592-
* When casting to ``void`` to suppress warnings about unused variables (as an
593-
alternative to ``[[maybe_unused]]``). Prefer C-style casts in this instance.
590+
rather than C-style casts. There is one exception to this:
594591

595592
* When casting between integral types (including enums that are not strongly-
596593
typed), functional-style casts are permitted as an alternative to
@@ -1286,17 +1283,17 @@ value" warning when assertions are disabled. For example, this code will warn:
12861283

12871284
These are two interesting different cases. In the first case, the call to
12881285
``V.size()`` is only useful for the assert, and we don't want it executed when
1289-
assertions are disabled. Code like this should move the call into the assert
1286+
assertions are disabled. Code like this should move the call into the assert
12901287
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:
1288+
the assert is enabled or not. In this case, the value should be defined using
1289+
the ``[[maybe_unused]]`` attribute disable the warning. To be specific, it is
1290+
preferred to write the code like this:
12941291

12951292
.. code-block:: c++
12961293

12971294
assert(V.size() > 42 && "Vector smaller than it should be");
12981295

1299-
bool NewToSet = Myset.insert(Value); (void)NewToSet;
1296+
[[maybe_unused]] bool NewToSet = Myset.insert(Value);
13001297
assert(NewToSet && "The value shouldn't be in the set yet");
13011298

13021299
Do Not Use ``using namespace std``

0 commit comments

Comments
 (0)