Skip to content

Commit 6398453

Browse files
committed
[clang-tidy] fix insertion location for function pointers in cppcoreguidelines-init-variables
Previously, the insertion location for the `= nullptr` fix would be after the variable name. However, if the variable is of type function pointer that is not an alias, then the insertion would happen inside the type specification: `void (*a1)(void*);` -> `void (*a1 = nullptr)(void*);`. With this change, the insertion location will be at the next 'terminator'. That is, at the next `,` or `;`, as that will finish the current declaration: `void (a1)(void*) = nullptr;`. Fixes #112089
1 parent 1de7165 commit 6398453

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
#include "InitVariablesCheck.h"
1010

11+
#include "../utils/LexerUtils.h"
1112
#include "clang/AST/ASTContext.h"
13+
#include "clang/AST/Type.h"
1214
#include "clang/ASTMatchers/ASTMatchFinder.h"
13-
#include "clang/Lex/PPCallbacks.h"
1415
#include "clang/Lex/Preprocessor.h"
1516
#include <optional>
1617

@@ -107,8 +108,9 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
107108
<< MatchedDecl;
108109
if (*InitializationString != nullptr)
109110
Diagnostic << FixItHint::CreateInsertion(
110-
MatchedDecl->getLocation().getLocWithOffset(
111-
MatchedDecl->getName().size()),
111+
utils::lexer::findNextTerminator(MatchedDecl->getLocation(),
112+
*Result.SourceManager,
113+
Result.Context->getLangOpts()),
112114
*InitializationString);
113115
if (AddMathInclude) {
114116
Diagnostic << IncludeInserter.createIncludeInsertion(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ Changes in existing checks
171171
fix false positive that floating point variable is only used in increment
172172
expression.
173173

174+
- Improved :doc:`cppcoreguidelines-init-variables
175+
<clang-tidy/checks/cppcoreguidelines-init-variables>` check by fixing the
176+
insertion location for function pointers.
177+
174178
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
175179
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
176180
avoid false positive when member initialization depends on a structured
@@ -185,9 +189,9 @@ Changes in existing checks
185189
false positive for C++23 deducing this.
186190

187191
- Improved :doc:`modernize-avoid-c-arrays
188-
<clang-tidy/checks/modernize/avoid-c-arrays>` check to suggest using ``std::span``
189-
as a replacement for parameters of incomplete C array type in C++20 and
190-
``std::array`` or ``std::vector`` before C++20.
192+
<clang-tidy/checks/modernize/avoid-c-arrays>` check to suggest using
193+
``std::span`` as a replacement for parameters of incomplete C array type in
194+
C++20 and ``std::array`` or ``std::vector`` before C++20.
191195

192196
- Improved :doc:`modernize-loop-convert
193197
<clang-tidy/checks/modernize/loop-convert>` check to fix false positive when

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,17 @@ void test_clang_diagnostic_error() {
134134
// CHECK-MESSAGES: :[[@LINE-1]]:3: error: unknown type name 'UnknownType' [clang-diagnostic-error]
135135
// CHECK-FIXES-NOT: {{^}} UnknownType b = 0;{{$}}
136136
}
137+
138+
namespace gh112089 {
139+
void foo(void*);
140+
using FPtr = void(*)(void*);
141+
void test() {
142+
void(*a1)(void*);
143+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: variable 'a1' is not initialized [cppcoreguidelines-init-variables]
144+
// CHECK-FIXES: void(*a1)(void*) = nullptr;
145+
FPtr a2;
146+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'a2' is not initialized [cppcoreguidelines-init-variables]
147+
// CHECK-FIXES: FPtr a2 = nullptr;
148+
}
149+
} // namespace gh112089
150+

0 commit comments

Comments
 (0)