Skip to content

Commit 28afda5

Browse files
committed
fix removal range of const kw
1 parent 3633dd5 commit 28afda5

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

clang-tools-extra/clang-tidy/modernize/UseConstexprCheck.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -921,11 +921,16 @@ void UseConstexprCheck::onEndOfTranslationUnit() {
921921
<< Var << R
922922
<< FixItHint::CreateInsertion(Var->getInnerLocStart(),
923923
ConstexprString + " ");
924+
// Since either of the locs can be in a macro, use `makeFileCharRange` to be
925+
// sure that we have a consistent `CharSourceRange`, located entirely in the
926+
// source file.
927+
CharSourceRange FileRange = Lexer::makeFileCharRange(
928+
CharSourceRange::getCharRange(Var->getInnerLocStart(),
929+
Var->getLocation()),
930+
Var->getASTContext().getSourceManager(), getLangOpts());
924931
if (const std::optional<Token> ConstToken =
925932
utils::lexer::getQualifyingToken(
926-
tok::TokenKind::kw_const,
927-
CharSourceRange::getTokenRange(Var->getSourceRange()),
928-
Var->getASTContext(),
933+
tok::TokenKind::kw_const, FileRange, Var->getASTContext(),
929934
Var->getASTContext().getSourceManager())) {
930935
Diag << FixItHint::CreateRemoval(ConstToken->getLocation());
931936
}

clang-tools-extra/test/clang-tidy/checkers/modernize/use-constexpr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ namespace variable {
454454

455455
static auto e1 = TemplatedVar1<int> + TemplatedVar1<unsigned int>;
456456
}
457+
458+
const auto check = [](const int & ref) { };
459+
// CHECK-MESSAGES-17: :[[@LINE-1]]:16: warning: variable 'check' can be declared 'constexpr' [modernize-use-constexpr]
460+
// CHECK-FIXES-17: constexpr auto check = [](const int & ref) { };
457461
} // namespace literal_type
458462

459463
namespace struct_type {

0 commit comments

Comments
 (0)