diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp index bdba2314c7056..3eef2fd12cc8e 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp @@ -8,9 +8,10 @@ #include "InitVariablesCheck.h" +#include "../utils/LexerUtils.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/Type.h" #include "clang/ASTMatchers/ASTMatchFinder.h" -#include "clang/Lex/PPCallbacks.h" #include "clang/Lex/Preprocessor.h" #include @@ -107,8 +108,9 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) { << MatchedDecl; if (*InitializationString != nullptr) Diagnostic << FixItHint::CreateInsertion( - MatchedDecl->getLocation().getLocWithOffset( - MatchedDecl->getName().size()), + utils::lexer::findNextTerminator(MatchedDecl->getLocation(), + *Result.SourceManager, + Result.Context->getLangOpts()), *InitializationString); if (AddMathInclude) { Diagnostic << IncludeInserter.createIncludeInsertion( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 3f7bcde1eb301..0e19a667bffe5 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -171,6 +171,10 @@ Changes in existing checks fix false positive that floating point variable is only used in increment expression. +- Improved :doc:`cppcoreguidelines-init-variables + ` check by fixing the + insertion location for function pointers. + - Improved :doc:`cppcoreguidelines-prefer-member-initializer ` check to avoid false positive when member initialization depends on a structured @@ -185,9 +189,9 @@ Changes in existing checks false positive for C++23 deducing this. - Improved :doc:`modernize-avoid-c-arrays - ` check to suggest using ``std::span`` - as a replacement for parameters of incomplete C array type in C++20 and - ``std::array`` or ``std::vector`` before C++20. + ` check to suggest using + ``std::span`` as a replacement for parameters of incomplete C array type in + C++20 and ``std::array`` or ``std::vector`` before C++20. - Improved :doc:`modernize-loop-convert ` check to fix false positive when 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 e3d50946d1cb8..824431c1bf52f 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 @@ -134,3 +134,17 @@ void test_clang_diagnostic_error() { // CHECK-MESSAGES: :[[@LINE-1]]:3: error: unknown type name 'UnknownType' [clang-diagnostic-error] // CHECK-FIXES-NOT: {{^}} UnknownType b = 0;{{$}} } + +namespace gh112089 { + void foo(void*); + using FPtr = void(*)(void*); + void test() { + void(*a1)(void*); + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'a1' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: void(*a1)(void*) = nullptr; + FPtr a2; + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'a2' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: FPtr a2 = nullptr; + } +} // namespace gh112089 +