Skip to content

Commit ce6de98

Browse files
committed
[clang-tidy] Fix cppcoreguidelines-init-variables for invalid vardecl
https://godbolt.org/z/n4cK4fo3o The checker missed a check for invalid vardecl and this could cause a false positive. Reviewed By: carlosgalvezp Differential Revision: https://reviews.llvm.org/D138655
1 parent f3188b9 commit ce6de98

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
5959
const ASTContext &Context = *Result.Context;
6060
const SourceManager &Source = Context.getSourceManager();
6161

62+
// Clang diagnostic error may cause the variable to be an invalid int vardecl
63+
if (MatchedDecl->isInvalidDecl())
64+
return;
65+
6266
// We want to warn about cases where the type name
6367
// comes from a macro like this:
6468
//

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables %t -- -- -fno-delayed-template-parsing -fexceptions
1+
// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables -fix-errors %t -- -- -fno-delayed-template-parsing -fexceptions
22
// CHECK-FIXES: {{^}}#include <math.h>
33

44
// Ensure that function declarations are not changed.
@@ -124,3 +124,13 @@ void uninitialized_enum() {
124124
Fruit fruit;
125125
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'fruit' is not initialized [cppcoreguidelines-init-variables]
126126
}
127+
128+
void test_clang_diagnostic_error() {
129+
int a;
130+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'a' is not initialized [cppcoreguidelines-init-variables]
131+
// CHECK-FIXES: {{^}} int a = 0;{{$}}
132+
133+
UnknownType b;
134+
// CHECK-MESSAGES: :[[@LINE-1]]:3: error: unknown type name 'UnknownType' [clang-diagnostic-error]
135+
// CHECK-FIXES-NOT: {{^}} UnknownType b = 0;{{$}}
136+
}

0 commit comments

Comments
 (0)