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

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 23 05:37:52 PDT 2025


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

>From 9d787369e5bb3dd39cc35d6ef07d79d9057cda0c Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Wed, 4 Jun 2025 13:43:28 -0700
Subject: [PATCH 1/3] [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.
---
 llvm/docs/CodingStandards.rst | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst
index ad772f69127ad..782ad011f33d6 100644
--- a/llvm/docs/CodingStandards.rst
+++ b/llvm/docs/CodingStandards.rst
@@ -591,6 +591,8 @@ rather than C-style casts. There are two exceptions to this:
 
 * When casting to ``void`` to suppress warnings about unused variables (as an
   alternative to ``[[maybe_unused]]``). Prefer C-style casts in this instance.
+  Note that if the variable is unused because its used only in ``assert``, use
+  ``[[maybe_unused]]`` instead of a C-style void cast.
 
 * When casting between integral types (including enums that are not strongly-
   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
 ``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);
+    (void)Value;
+
 Do Not Use ``using namespace std``
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

>From 467d0c07c410ff908192bcdb628b654d1f6f646d Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Mon, 23 Jun 2025 05:37:05 -0700
Subject: [PATCH 2/3] Update llvm/docs/CodingStandards.rst

Co-authored-by: James Henderson <James.Henderson at sony.com>
---
 llvm/docs/CodingStandards.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst
index 782ad011f33d6..c48e9d0cb947c 100644
--- a/llvm/docs/CodingStandards.rst
+++ b/llvm/docs/CodingStandards.rst
@@ -591,7 +591,7 @@ rather than C-style casts. There are two exceptions to this:
 
 * When casting to ``void`` to suppress warnings about unused variables (as an
   alternative to ``[[maybe_unused]]``). Prefer C-style casts in this instance.
-  Note that if the variable is unused because its used only in ``assert``, use
+  Note that if the variable is unused because it's used only in ``assert``, use
   ``[[maybe_unused]]`` instead of a C-style void cast.
 
 * When casting between integral types (including enums that are not strongly-

>From fb92f2b2016b166801f5deea837da88274ceeaad Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Mon, 23 Jun 2025 05:37:43 -0700
Subject: [PATCH 3/3] Update llvm/docs/CodingStandards.rst

Co-authored-by: Nikita Popov <github at npopov.com>
---
 llvm/docs/CodingStandards.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst
index c48e9d0cb947c..7d275a511de1a 100644
--- a/llvm/docs/CodingStandards.rst
+++ b/llvm/docs/CodingStandards.rst
@@ -1307,7 +1307,7 @@ suppress unused variable warning as follows:
 .. code-block:: c
 
     LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD);
-    assert(LLVMIsAValueAsMetadata(Value) == NULL);
+    assert(LLVMIsAValueAsMetadata(Value) != NULL);
     (void)Value;
 
 Do Not Use ``using namespace std``



More information about the llvm-commits mailing list