comparison clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 0572611fdcc8
children 1f2b6ac9f198
comparison
equal deleted inserted replaced
220:42394fc6a535 221:79ff65ed7e25
90 const auto StringCStrCallExpr = 90 const auto StringCStrCallExpr =
91 cxxMemberCallExpr(on(StringExpr.bind("arg")), 91 cxxMemberCallExpr(on(StringExpr.bind("arg")),
92 callee(memberExpr().bind("member")), 92 callee(memberExpr().bind("member")),
93 callee(cxxMethodDecl(hasAnyName("c_str", "data")))) 93 callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
94 .bind("call"); 94 .bind("call");
95 95 const auto HasRValueTempParent =
96 hasParent(materializeTemporaryExpr(unless(isBoundToLValue())));
96 // Detect redundant 'c_str()' calls through a string constructor. 97 // Detect redundant 'c_str()' calls through a string constructor.
97 // If CxxConstructExpr is the part of some CallExpr we need to 98 // If CxxConstructExpr is the part of some CallExpr we need to
98 // check that matched ParamDecl of the ancestor CallExpr is not rvalue. 99 // check that matched ParamDecl of the ancestor CallExpr is not rvalue.
99 Finder->addMatcher( 100 Finder->addMatcher(
100 traverse(ast_type_traits::TK_AsIs, 101 traverse(
101 cxxConstructExpr(StringConstructorExpr, 102 TK_AsIs,
102 hasArgument(0, StringCStrCallExpr), 103 cxxConstructExpr(
103 unless(hasParent(materializeTemporaryExpr( 104 StringConstructorExpr, hasArgument(0, StringCStrCallExpr),
104 unless(isBoundToLValue())))))), 105 unless(anyOf(HasRValueTempParent, hasParent(cxxBindTemporaryExpr(
106 HasRValueTempParent)))))),
105 this); 107 this);
106 108
107 // Detect: 's == str.c_str()' -> 's == str' 109 // Detect: 's == str.c_str()' -> 's == str'
108 Finder->addMatcher( 110 Finder->addMatcher(
109 cxxOperatorCallExpr( 111 cxxOperatorCallExpr(
154 this); 156 this);
155 157
156 // Detect redundant 'c_str()' calls through a StringRef constructor. 158 // Detect redundant 'c_str()' calls through a StringRef constructor.
157 Finder->addMatcher( 159 Finder->addMatcher(
158 traverse( 160 traverse(
159 ast_type_traits::TK_AsIs, 161 TK_AsIs,
160 cxxConstructExpr( 162 cxxConstructExpr(
161 // Implicit constructors of these classes are overloaded 163 // Implicit constructors of these classes are overloaded
162 // wrt. string types and they internally make a StringRef 164 // wrt. string types and they internally make a StringRef
163 // referring to the argument. Passing a string directly to 165 // referring to the argument. Passing a string directly to
164 // them is preferred to passing a char pointer. 166 // them is preferred to passing a char pointer.