diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp @@ -59,6 +59,10 @@ const ASTContext &Context = *Result.Context; const SourceManager &Source = Context.getSourceManager(); + // Clang diagnostic error may cause the variable to be an invalid int vardecl + if (MatchedDecl->isInvalidDecl()) + return; + // We want to warn about cases where the type name // comes from a macro like this: // diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables %t -- -- -fno-delayed-template-parsing -fexceptions +// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables -fix-errors %t -- -- -fno-delayed-template-parsing -fexceptions // CHECK-FIXES: {{^}}#include // Ensure that function declarations are not changed. @@ -124,3 +124,13 @@ Fruit fruit; // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'fruit' is not initialized [cppcoreguidelines-init-variables] } + +void test_clang_diagnostic_error() { + int a; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'a' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: {{^}} int a = 0;{{$}} + + UnknownType b; + // CHECK-MESSAGES: :[[@LINE-1]]:3: error: unknown type name 'UnknownType' [clang-diagnostic-error] + // CHECK-FIXES-NOT: {{^}} UnknownType b = 0;{{$}} +}