10#include "../utils/FixItHintUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/FixIt.h"
19static const StringRef
CompareMessage =
"do not use 'compare' to test equality "
20 "of strings; use the string equality "
24 const auto StrCompare = cxxMemberCallExpr(
25 callee(cxxMethodDecl(hasName(
"compare"),
26 ofClass(classTemplateSpecializationDecl(
27 hasName(
"::std::basic_string"))))),
28 hasArgument(0, expr().bind(
"str2")), argumentCountIs(1),
29 callee(memberExpr().bind(
"str1")));
34 implicitCastExpr(hasImplicitDestinationType(booleanType()),
41 binaryOperator(hasAnyOperatorName(
"==",
"!="),
42 hasOperands(StrCompare.bind(
"compare"),
43 integerLiteral(equals(0)).bind(
"zero")))
49 if (
const auto *Matched = Result.Nodes.getNodeAs<Stmt>(
"match1")) {
54 if (
const auto *Matched = Result.Nodes.getNodeAs<Stmt>(
"match2")) {
55 const ASTContext &Ctx = *Result.Context;
57 if (
const auto *Zero = Result.Nodes.getNodeAs<Stmt>(
"zero")) {
58 const auto *Str1 = Result.Nodes.getNodeAs<MemberExpr>(
"str1");
59 const auto *Str2 = Result.Nodes.getNodeAs<Stmt>(
"str2");
60 const auto *Compare = Result.Nodes.getNodeAs<Stmt>(
"compare");
65 Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(),
"*");
67 Diag << tooling::fixit::createReplacement(*Zero, *Str2, Ctx)
68 << tooling::fixit::createReplacement(*Compare, *Str1->getBase(),
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
static const StringRef CompareMessage