Author: Da-Viper Date: 2023-10-29T15:17:32+01:00 New Revision: 3f64c0fc48c5b2efc5b9ba11351647d515f23418
URL: https://github.com/llvm/llvm-project/commit/3f64c0fc48c5b2efc5b9ba11351647d515f23418 DIFF: https://github.com/llvm/llvm-project/commit/3f64c0fc48c5b2efc5b9ba11351647d515f23418.diff LOG: [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 Added: Modified: clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp index 6476f1d7fdf2b81..24cbbd8bc60a2b5 100644 --- a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp +++ b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp @@ -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())); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 5ce38ab48bf295f..ecfb3aa9267f140 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -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 diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp index d521fd238b7d521..bc098efe3044a5d 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/avoid-const-params-in-decls.cpp @@ -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 { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits