Skip to content

Commit

Permalink
[clang-tidy] Fix readability-avoid-const-params-in-decls - point to t…
Browse files Browse the repository at this point in the history
…he correct const location (#69103)

Fixes the warning marker for redundant const from beginning of variable
to actual location of const. 

Fixes #68492
  • Loading branch information
Da-Viper committed Oct 29, 2023
1 parent b5281af commit 3f64c0f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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");
Expand All @@ -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()));
}
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ 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);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
// 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);
Expand All @@ -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*/);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3f64c0f

Please sign in to comment.