[llvm] [NFC][CodingStandard] Deprecate use of void casts to suppress warnings (PR #142850)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 4 13:45:56 PDT 2025


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

Recommend using attribute `[[maybe_unused]`` for variables that may be unused in non-assert enabled builds to suppress unused variable warnings.

>From c62a5f8f9e4a5ae2495a2cef6b4859b71cbd002a 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] [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 | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst
index ad772f69127ad..605e40706b9c6 100644
--- a/llvm/docs/CodingStandards.rst
+++ b/llvm/docs/CodingStandards.rst
@@ -587,10 +587,7 @@ Prefer C++-style casts
 ^^^^^^^^^^^^^^^^^^^^^^
 
 When casting, use ``static_cast``, ``reinterpret_cast``, and ``const_cast``,
-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.
+rather than C-style casts. There is one exception to this:
 
 * When casting between integral types (including enums that are not strongly-
   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:
 
 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
+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 disable 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");
 
 Do Not Use ``using namespace std``



More information about the llvm-commits mailing list