Skip to content

Commit 028ea71

Browse files
authored
[clang-tidy] fix insertion location for function pointers in cppcoreguidelines-init-variables (#112091)
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 ff0698b commit 028ea71

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
@@ -194,6 +194,10 @@ Changes in existing checks
194194
fix false positive that floating point variable is only used in increment
195195
expression.
196196

197+
- Improved :doc:`cppcoreguidelines-init-variables
198+
<clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the
199+
insertion location for function pointers.
200+
197201
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
198202
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
199203
avoid false positive when member initialization depends on a structured
@@ -212,9 +216,9 @@ Changes in existing checks
212216
false positive for C++23 deducing this.
213217

214218
- Improved :doc:`modernize-avoid-c-arrays
215-
<clang-tidy/checks/modernize/avoid-c-arrays>` check to suggest using ``std::span``
216-
as a replacement for parameters of incomplete C array type in C++20 and
217-
``std::array`` or ``std::vector`` before C++20.
219+
<clang-tidy/checks/modernize/avoid-c-arrays>` check to suggest using
220+
``std::span`` as a replacement for parameters of incomplete C array type in
221+
C++20 and ``std::array`` or ``std::vector`` before C++20.
218222

219223
- Improved :doc:`modernize-loop-convert
220224
<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)