[llvm] [NFC][CodingStandard] Require``[[maybe_unused]]`` for unused variables in asserts (PR #142850)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 23 02:00:29 PDT 2025


================
@@ -1288,17 +1290,26 @@ 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:
+the assert is enabled or not. In this case, the value should be defined using
+the ``[[maybe_unused]]`` attribute to suppress the warning. To be specific, it is
+preferred to write the code like this:
 
 .. code-block:: c++
 
   assert(V.size() > 42 && "Vector smaller than it should be");
 
-  bool NewToSet = Myset.insert(Value); (void)NewToSet;
+  [[maybe_unused]] bool NewToSet = Myset.insert(Value);
   assert(NewToSet && "The value shouldn't be in the set yet");
 
+In C code where ``[[maybe_unused]]`` is not supported, use ``void`` cast to
+suppress unused variable warning as follows:
+
+.. code-block:: c
+
+    LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD);
+    assert(LLVMIsAValueAsMetadata(Value) == NULL);
----------------
nikic wrote:

```suggestion
    assert(LLVMIsAValueAsMetadata(Value) != NULL);
```
Probably?

https://github.com/llvm/llvm-project/pull/142850


More information about the llvm-commits mailing list