-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Fix insertion location for certain function pointers in cppcoreguidelines-init-variables
#162218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…`cppcoreguidelines-init-variables`
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: None (flovent) ChangesThis patch starts to find terminator from Kind of follow up to #112091 Closes #161978 . Full diff: https://github.com/llvm/llvm-project/pull/162218.diff 3 Files Affected:
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
+ <clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the
+ insertion location for function pointers with multiple parameters.
+
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
<clang-tidy/checks/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..3a198c91e85c0 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]]:14: warning: variable 'fp1' is not initialized [cppcoreguidelines-init-variables]
+ // CHECK-FIXES: bool (*fp1)(int) = nullptr;
+ bool (*fp2)(int, int);
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: 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]]:14: 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]]:14: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables]
+ // CHECK-FIXES: bool (*fp4)(int, int, int, ...) = nullptr;
+ }
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM apart from one suggestion
This patch starts to find terminator from
VarDecl
's end location rather than it'sgetLocation()
to ignore terminator(,
) in function paramaters list.Kind of follow up to #112091
Closes #161978 .