[clang-tools-extra] [clang-tidy] Improved cppcoreguidelines-pro-type-const-cast (PR #69501)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 19 22:07:32 PDT 2023
================
@@ -14,13 +14,42 @@ using namespace clang::ast_matchers;
namespace clang::tidy::cppcoreguidelines {
+static bool hasConstQualifier(QualType Type) {
+ const QualType PtrType = Type->getPointeeType();
+ if (!PtrType.isNull())
+ return hasConstQualifier(PtrType);
+
+ return Type.isConstQualified();
+}
+
+namespace {
+AST_MATCHER(QualType, hasConst) { return hasConstQualifier(Node); }
+} // namespace
+
+ProTypeConstCastCheck::ProTypeConstCastCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
+
+void ProTypeConstCastCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "StrictMode", StrictMode);
+}
+
void ProTypeConstCastCheck::registerMatchers(MatchFinder *Finder) {
- Finder->addMatcher(cxxConstCastExpr().bind("cast"), this);
+ if (StrictMode)
+ Finder->addMatcher(cxxConstCastExpr().bind("cast"), this);
+ else
+ Finder->addMatcher(cxxConstCastExpr(unless(hasDestinationType(
+ hasCanonicalType(hasConst()))))
+ .bind("cast"),
+ this);
}
void ProTypeConstCastCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedCast = Result.Nodes.getNodeAs<CXXConstCastExpr>("cast");
- diag(MatchedCast->getOperatorLoc(), "do not use const_cast");
+ diag(MatchedCast->getOperatorLoc(),
+ "do not use const_cast%select{ to cast away const|}0")
----------------
HerrCai0907 wrote:
`cast away const` sounds like a little bit strange.
```suggestion
"do not use const_cast%select{ to remove const qualifier|}0")
```
https://github.com/llvm/llvm-project/pull/69501
More information about the cfe-commits
mailing list