mirror of
https://github.com/intel/llvm.git
synced 2026-01-21 12:19:23 +08:00
[clang-tidy] Fix readability-avoid-const-params-in-decls - point to the correct const location (#69103)
Fixes the warning marker for redundant const from beginning of variable to actual location of const. Fixes #68492
This commit is contained in:
@@ -21,6 +21,24 @@ SourceRange getTypeRange(const ParmVarDecl &Param) {
|
||||
return {Param.getBeginLoc(), Param.getLocation().getLocWithOffset(-1)};
|
||||
}
|
||||
|
||||
// Finds the location of the qualifying `const` token in the `ParmValDecl`'s
|
||||
// return type. Returns `std::nullopt` when the parm type is not
|
||||
// `const`-qualified like when the type is an alias or a macro.
|
||||
static std::optional<Token>
|
||||
findConstToRemove(const ParmVarDecl &Param,
|
||||
const MatchFinder::MatchResult &Result) {
|
||||
|
||||
CharSourceRange FileRange = Lexer::makeFileCharRange(
|
||||
CharSourceRange::getTokenRange(getTypeRange(Param)),
|
||||
*Result.SourceManager, Result.Context->getLangOpts());
|
||||
|
||||
if (FileRange.isInvalid())
|
||||
return std::nullopt;
|
||||
|
||||
return tidy::utils::lexer::getQualifyingToken(
|
||||
tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
||||
@@ -30,11 +48,10 @@ void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
||||
void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
|
||||
const auto ConstParamDecl =
|
||||
parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
|
||||
Finder->addMatcher(
|
||||
functionDecl(unless(isDefinition()),
|
||||
has(typeLoc(forEach(ConstParamDecl))))
|
||||
.bind("func"),
|
||||
this);
|
||||
Finder->addMatcher(functionDecl(unless(isDefinition()),
|
||||
has(typeLoc(forEach(ConstParamDecl))))
|
||||
.bind("func"),
|
||||
this);
|
||||
}
|
||||
|
||||
void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
|
||||
@@ -50,7 +67,10 @@ void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto Diag = diag(Param->getBeginLoc(),
|
||||
const auto Tok = findConstToRemove(*Param, Result);
|
||||
const auto ConstLocation = Tok ? Tok->getLocation() : Param->getBeginLoc();
|
||||
|
||||
auto Diag = diag(ConstLocation,
|
||||
"parameter %0 is const-qualified in the function "
|
||||
"declaration; const-qualification of parameters only has an "
|
||||
"effect in function definitions");
|
||||
@@ -70,18 +90,9 @@ void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
|
||||
// from a macro.
|
||||
return;
|
||||
}
|
||||
|
||||
CharSourceRange FileRange = Lexer::makeFileCharRange(
|
||||
CharSourceRange::getTokenRange(getTypeRange(*Param)),
|
||||
*Result.SourceManager, getLangOpts());
|
||||
|
||||
if (!FileRange.isValid())
|
||||
return;
|
||||
|
||||
auto Tok = tidy::utils::lexer::getQualifyingToken(
|
||||
tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
|
||||
if (!Tok)
|
||||
return;
|
||||
|
||||
Diag << FixItHint::CreateRemoval(
|
||||
CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
|
||||
}
|
||||
|
||||
@@ -369,6 +369,10 @@ Changes in existing checks
|
||||
<clang-tidy/checks/readability/braces-around-statements>` check to
|
||||
ignore false-positive for ``if constexpr`` in lambda expression.
|
||||
|
||||
- Improved :doc:`readability-avoid-const-params-in-decls
|
||||
<clang-tidy/checks/readability/avoid-const-params-in-decls>` diagnositics to
|
||||
highlight the const location
|
||||
|
||||
- Improved :doc:`readability-container-size-empty
|
||||
<clang-tidy/checks/readability/container-size-empty>` check to
|
||||
detect comparison between string and empty string literals and support
|
||||
|
||||
@@ -9,15 +9,15 @@ void F1(const int i);
|
||||
// CHECK-FIXES: void F1(int i);
|
||||
|
||||
void F2(const int *const i);
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 'i' is const-qualified
|
||||
// CHECK-FIXES: void F2(const int *i);
|
||||
|
||||
void F3(int const i);
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: parameter 'i' is const-qualified
|
||||
// CHECK-FIXES: void F3(int i);
|
||||
|
||||
void F4(alias_type const i);
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 'i' is const-qualified
|
||||
// CHECK-FIXES: void F4(alias_type i);
|
||||
|
||||
void F5(const int);
|
||||
@@ -25,7 +25,7 @@ void F5(const int);
|
||||
// CHECK-FIXES: void F5(int);
|
||||
|
||||
void F6(const int *const);
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: parameter 1 is const-qualified
|
||||
// CHECK-FIXES: void F6(const int *);
|
||||
|
||||
void F7(int, const int);
|
||||
@@ -42,7 +42,7 @@ void F9(const int long_name);
|
||||
// CHECK-FIXES: void F9(int long_name);
|
||||
|
||||
void F10(const int *const *const long_name);
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'long_name'
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: parameter 'long_name'
|
||||
// CHECK-FIXES: void F10(const int *const *long_name);
|
||||
|
||||
void F11(const unsigned int /*v*/);
|
||||
@@ -71,11 +71,11 @@ void F15(const A<const int> Named);
|
||||
// CHECK-FIXES: void F15(A<const int> Named);
|
||||
|
||||
void F16(const A<const int> *const);
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 1 is const-qualified
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: parameter 1 is const-qualified
|
||||
// CHECK-FIXES: void F16(const A<const int> *);
|
||||
|
||||
void F17(const A<const int> *const Named);
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'Named' is const-qualified
|
||||
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: parameter 'Named' is const-qualified
|
||||
// CHECK-FIXES: void F17(const A<const int> *Named);
|
||||
|
||||
struct Foo {
|
||||
|
||||
Reference in New Issue
Block a user