Skip to content

Commit da42dcf

Browse files
committed
[clang-tidy] Fix insertion location for certain function pointers in cppcoreguidelines-init-variables
1 parent 08078fb commit da42dcf

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-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
@@ -299,6 +299,10 @@ Changes in existing checks
299299
an additional matcher that generalizes the copy-and-swap idiom pattern
300300
detection.
301301

302+
- Improved :doc:`cppcoreguidelines-init-variables
303+
<clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the
304+
insertion location for function pointers with multiple parameters.
305+
302306
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
303307
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
304308
avoid false positives on inherited members in class templates.

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

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

151+
namespace gh161978 {
152+
void test() {
153+
bool (*fp1)(int);
154+
// CHECK-MESSAGES: :[[@LINE-1]]:14: 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]]:14: 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]]:14: 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]]:14: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables]
164+
// CHECK-FIXES: bool (*fp4)(int, int, int, ...) = nullptr;
165+
}
166+
}

0 commit comments

Comments
 (0)