Skip to content

Commit e9ebeab

Browse files
committed
clang-tidy/bugprone: match dependent types in
unused-local-non-trivial-variable Also add the ability to match against template specializations, this can be useful if you want all std::future types to be matched, no matter what type is held within the future. Signed-off-by: Tyler Rockwood <[email protected]>
1 parent 6f0b7e5 commit e9ebeab

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ void UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) {
6060
varDecl(isLocalVarDecl(), unless(isReferenced()),
6161
unless(isExceptionVariable()), hasLocalStorage(), isDefinition(),
6262
unless(hasType(isReferenceType())), unless(hasType(isTrivial())),
63-
hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
64-
recordDecl(matchesAnyListedName(IncludeTypes),
65-
unless(matchesAnyListedName(ExcludeTypes))))))))
63+
hasType(hasUnqualifiedDesugaredType(
64+
anyOf(recordType(hasDeclaration(namedDecl(
65+
matchesAnyListedName(IncludeTypes),
66+
unless(matchesAnyListedName(ExcludeTypes))))),
67+
templateSpecializationType(hasDeclaration(namedDecl(
68+
matchesAnyListedName(IncludeTypes),
69+
unless(matchesAnyListedName(ExcludeTypes)))))))))
6670
.bind("var"),
6771
this);
6872
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ T qux(T Generic) {
7373
async::Future<Units> MustBeUsed;
7474
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: unused local variable 'MustBeUsed' of type 'async::Future<Units>' [bugprone-unused-local-non-trivial-variable]
7575
PendingA.get();
76+
async::Future<T> TemplateType;
77+
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: unused local variable 'TemplateType' of type 'async::Future<T>' [bugprone-unused-local-non-trivial-variable]
78+
a::Future<T> AliasTemplateType;
79+
// 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]
7680
return Generic;
7781
}
7882

0 commit comments

Comments
 (0)