[clang-tools-extra] ce6de98 - [clang-tidy] Fix `cppcoreguidelines-init-variables` for invalid vardecl
via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 7 03:35:07 PST 2023
Author: Sockke
Date: 2023-02-07T19:34:54+08:00
New Revision: ce6de98b80ada8efbff93766b5303db5ab052efc
URL: https://github.com/llvm/llvm-project/commit/ce6de98b80ada8efbff93766b5303db5ab052efc
DIFF: https://github.com/llvm/llvm-project/commit/ce6de98b80ada8efbff93766b5303db5ab052efc.diff
LOG: [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
Added:
Modified:
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index b4543e0d2ae42..bdba2314c7056 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -59,6 +59,10 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
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
index 479e5f62eb8b5..e3d50946d1cb8 100644
--- 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 <math.h>
// Ensure that function declarations are not changed.
@@ -124,3 +124,13 @@ void uninitialized_enum() {
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;{{$}}
+}
More information about the cfe-commits
mailing list