10 #include "../utils/FixItHintUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Tooling/FixIt.h"
19 namespace readability {
21 static const StringRef
CompareMessage =
"do not use 'compare' to test equality "
22 "of strings; use the string equality "
25 void StringCompareCheck::registerMatchers(MatchFinder *Finder) {
26 const auto StrCompare = cxxMemberCallExpr(
27 callee(cxxMethodDecl(hasName(
"compare"),
28 ofClass(classTemplateSpecializationDecl(
29 hasName(
"::std::basic_string"))))),
30 hasArgument(0, expr().bind(
"str2")), argumentCountIs(1),
31 callee(memberExpr().bind(
"str1")));
35 traverse(ast_type_traits::TK_AsIs,
36 implicitCastExpr(hasImplicitDestinationType(booleanType()),
43 binaryOperator(hasAnyOperatorName(
"==",
"!="),
44 hasOperands(StrCompare.bind(
"compare"),
45 integerLiteral(equals(0)).bind(
"zero")))
50 void StringCompareCheck::check(
const MatchFinder::MatchResult &Result) {
51 if (
const auto *Matched = Result.Nodes.getNodeAs<Stmt>(
"match1")) {
56 if (
const auto *Matched = Result.Nodes.getNodeAs<Stmt>(
"match2")) {
57 const ASTContext &
Ctx = *Result.Context;
59 if (
const auto *Zero = Result.Nodes.getNodeAs<Stmt>(
"zero")) {
60 const auto *Str1 = Result.Nodes.getNodeAs<MemberExpr>(
"str1");
61 const auto *Str2 = Result.Nodes.getNodeAs<Stmt>(
"str2");
62 const auto *Compare = Result.Nodes.getNodeAs<Stmt>(
"compare");
67 Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(),
"*");
69 Diag << tooling::fixit::createReplacement(*Zero, *Str2,
Ctx)
70 << tooling::fixit::createReplacement(*Compare, *Str1->getBase(),