Skip to content

Commit 1d3ad74

Browse files
authored
[clang-tidy] Fix insertion location for certain function pointers in cppcoreguidelines-init-variables (#162218)
This patch starts to find terminator from `VarDecl`'s end location rather than it's `getLocation()` to ignore terminator(`,`) in function paramaters list. Kind of follow up to #112091 Closes #161978 .
1 parent a833d90 commit 1d3ad74

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
108108
<< MatchedDecl;
109109
if (*InitializationString != nullptr)
110110
Diagnostic << FixItHint::CreateInsertion(
111-
utils::lexer::findNextTerminator(MatchedDecl->getLocation(),
111+
utils::lexer::findNextTerminator(MatchedDecl->getEndLoc(),
112112
*Result.SourceManager,
113113
Result.Context->getLangOpts()),
114114
*InitializationString);

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ Changes in existing checks
307307
an additional matcher that generalizes the copy-and-swap idiom pattern
308308
detection.
309309

310+
- Improved :doc:`cppcoreguidelines-init-variables
311+
<clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the
312+
insertion location for function pointers with multiple parameters.
313+
310314
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
311315
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
312316
avoid false positives on inherited members in class templates.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,23 @@ namespace gh112089 {
148148
}
149149
} // namespace gh112089
150150

151+
namespace gh161978 {
152+
void test() {
153+
bool (*fp1)(int);
154+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp1' is not initialized [cppcoreguidelines-init-variables]
155+
// CHECK-FIXES: bool (*fp1)(int) = nullptr;
156+
bool (*fp2)(int, int);
157+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp2' is not initialized [cppcoreguidelines-init-variables]
158+
// CHECK-FIXES: bool (*fp2)(int, int) = nullptr;
159+
bool (*fp3)(int, int, int);
160+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp3' is not initialized [cppcoreguidelines-init-variables]
161+
// CHECK-FIXES: bool (*fp3)(int, int, int) = nullptr;
162+
bool (*fp4)(int, int, int, ...);
163+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables]
164+
// CHECK-FIXES: bool (*fp4)(int, int, int, ...) = nullptr;
165+
bool (*fp5)(int, int), (*fp6)(int, int);
166+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp5' is not initialized [cppcoreguidelines-init-variables]
167+
// CHECK-MESSAGES: :[[@LINE-2]]:30: warning: variable 'fp6' is not initialized [cppcoreguidelines-init-variables]
168+
// CHECK-FIXES: bool (*fp5)(int, int) = nullptr, (*fp6)(int, int) = nullptr;
169+
}
170+
}

0 commit comments

Comments
 (0)