[clang] [clang] Add -Wused-but-marked-unused to -Wattributes (PR #179790)
John Paul Jepko via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 4 13:53:47 PST 2026
https://github.com/jpjepko created https://github.com/llvm/llvm-project/pull/179790
Running the following code with `-Wattributes` triggers no warnings:
```cpp
int main(__attribute__((unused)) int argc, __attribute__((unused)) char **argv)
{
return argc;
}
```
```
$ clang -Wall -Wattributes main.cpp
[no output]
```
But `-Wused-but-marked-unused` does warn (currently disabled by default):
```
$ clang -Wall -Wused-but-marked-unused main.cpp
/tmp/main.c:4:10: warning: 'argc' was marked unused but was used [-Wused-but-marked-unused]
4 | return argc;
| ^
1 warning generated.
```
This warning was disabled by default and removed from `-Wunused` in 2010 due to concerns about false-positives, as the `unused` attribute was traditionally interpreted as "it's okay if this isn't used" rather than "this must not be used" (paraphrased from https://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20101018/035732.html).
However, now that we have the `maybe_unused` attribute with explicit "may or may not be used" semantics, I would contend `unused` should have a stricter definition of "should not be used." Warning on variables marked `unused` that are actually used would improve code quality without the original false-positive concerns, since programmers should prefer `maybe_unused` in those cases.
Here I propose simply adding it to `-Wattributes`, which leaves it out of `-Wall`, but enables it alongside other attribute-related warnings. Thoughts?
>From a402efdefed48cbe72282969de52d44946c26b7d Mon Sep 17 00:00:00 2001
From: John Jepko <john.jepko at ericsson.com>
Date: Wed, 4 Feb 2026 17:41:24 +0100
Subject: [PATCH] add Wused-but-marked-unused to Wattributes
---
clang/include/clang/Basic/DiagnosticGroups.td | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 488f3a94c4fb6..cf2bc00cc2e21 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1010,8 +1010,9 @@ def IndependentClassAttribute : DiagGroup<"IndependentClass-attribute">;
def UnknownAttributes : DiagGroup<"unknown-attributes">;
def IgnoredAttributes : DiagGroup<"ignored-attributes",
[DllexportExplicitInstantiation]>;
-def Attributes : DiagGroup<"attributes", [UnknownAttributes,
- IgnoredAttributes]>;
+def UsedButMarkedUnused : DiagGroup<"used-but-marked-unused">;
+def Attributes : DiagGroup<"attributes", [UnknownAttributes, IgnoredAttributes,
+ UsedButMarkedUnused]>;
def UnknownSanitizers : DiagGroup<"unknown-sanitizers">;
def UnnamedTypeTemplateArgs : DiagGroup<"unnamed-type-template-args",
[CXX98CompatUnnamedTypeTemplateArgs]>;
@@ -1047,7 +1048,6 @@ def UnusedButSetVariable : DiagGroup<"unused-but-set-variable">;
def UnusedLocalTypedef : DiagGroup<"unused-local-typedef">;
def UnusedPropertyIvar : DiagGroup<"unused-property-ivar">;
def UnusedGetterReturnValue : DiagGroup<"unused-getter-return-value">;
-def UsedButMarkedUnused : DiagGroup<"used-but-marked-unused">;
def UsedSearchPath : DiagGroup<"search-path-usage">;
def UserDefinedLiterals : DiagGroup<"user-defined-literals">;
def UserDefinedWarnings : DiagGroup<"user-defined-warnings">;
More information about the cfe-commits
mailing list