Skip to content

Commit ebe77cc

Browse files
authored
[clang-tidy] ignore local variable with [maybe_unused] attribute in bugprone-unused-local-non-trivial-variable (#81563)
1 parent ca61e6a commit ebe77cc

File tree

4 files changed

+7
-0
lines changed

4 files changed

+7
-0
lines changed

clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) {
6060
varDecl(isLocalVarDecl(), unless(isReferenced()),
6161
unless(isExceptionVariable()), hasLocalStorage(), isDefinition(),
6262
unless(hasType(isReferenceType())), unless(hasType(isTrivial())),
63+
unless(hasAttr(attr::Kind::Unused)),
6364
hasType(hasUnqualifiedDesugaredType(
6465
anyOf(recordType(hasDeclaration(namedDecl(
6566
matchesAnyListedName(IncludeTypes),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ Changes in existing checks
121121
<clang-tidy/checks/bugprone/too-small-loop-variable>` check by incorporating
122122
better support for ``const`` loop boundaries.
123123

124+
- Improved :doc:`bugprone-unused-local-non-trivial-variable
125+
<clang-tidy/checks/bugprone/unused-local-non-trivial-variable>` check by
126+
ignoring local variable with ``[maybe_unused]`` attribute.
127+
124128
- Cleaned up :doc:`cppcoreguidelines-prefer-member-initializer
125129
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>`
126130
by removing enforcement of rule `C.48

clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The following types of variables are excluded from this check:
1111
* exception variables in catch clauses
1212
* static or thread local
1313
* structured bindings
14+
* variables with ``[[maybe_unused]]`` attribute
1415

1516
This check can be configured to warn on all non-trivial variables by setting
1617
`IncludeTypes` to `.*`, and excluding specific types using `ExcludeTypes`.

clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ T qux(T Generic) {
7777
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: unused local variable 'TemplateType' of type 'async::Future<T>' [bugprone-unused-local-non-trivial-variable]
7878
a::Future<T> AliasTemplateType;
7979
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: unused local variable 'AliasTemplateType' of type 'a::Future<T>' (aka 'Future<type-parameter-0-0>') [bugprone-unused-local-non-trivial-variable]
80+
[[maybe_unused]] async::Future<Units> MaybeUnused;
8081
return Generic;
8182
}
8283

0 commit comments

Comments
 (0)