diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp index ed595e1148dec..2545548df4f45 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp @@ -108,7 +108,7 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) { << MatchedDecl; if (*InitializationString != nullptr) Diagnostic << FixItHint::CreateInsertion( - utils::lexer::findNextTerminator(MatchedDecl->getLocation(), + utils::lexer::findNextTerminator(MatchedDecl->getEndLoc(), *Result.SourceManager, Result.Context->getLangOpts()), *InitializationString); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7e836a7114d50..40ed2fa7ee791 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -299,6 +299,10 @@ Changes in existing checks an additional matcher that generalizes the copy-and-swap idiom pattern detection. +- Improved :doc:`cppcoreguidelines-init-variables + ` check by fixing the + insertion location for function pointers with multiple parameters. + - Improved :doc:`cppcoreguidelines-prefer-member-initializer ` check to avoid false positives on inherited members in class templates. 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 824431c1bf52f..2ca4ccad5532b 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 @@ -148,3 +148,19 @@ namespace gh112089 { } } // namespace gh112089 +namespace gh161978 { + void test() { + bool (*fp1)(int); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp1' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp1)(int) = nullptr; + bool (*fp2)(int, int); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp2' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp2)(int, int) = nullptr; + bool (*fp3)(int, int, int); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp3' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp3)(int, int, int) = nullptr; + bool (*fp4)(int, int, int, ...); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp4)(int, int, int, ...) = nullptr; + } +}