84 const auto ComparisonUnaryOperator = unaryOperator(hasOperatorName(
"!"));
85 const auto ComparisonBinaryOperator = binaryOperator(isComparisonOperator());
86 const auto ComparisonOperator =
87 expr(anyOf(ComparisonUnaryOperator, ComparisonBinaryOperator));
95 const auto FunctionCompareDecl =
96 functionDecl(hasAnyName(FunctionNames)).bind(
"decl");
97 const auto DirectStringCompareCallExpr =
98 callExpr(hasDeclaration(FunctionCompareDecl)).bind(
"call");
99 const auto MacroStringCompareCallExpr = conditionalOperator(anyOf(
100 hasTrueExpression(ignoringParenImpCasts(DirectStringCompareCallExpr)),
101 hasFalseExpression(ignoringParenImpCasts(DirectStringCompareCallExpr))));
103 const auto StringCompareCallExpr = ignoringParenImpCasts(
104 anyOf(DirectStringCompareCallExpr, MacroStringCompareCallExpr));
106 if (WarnOnImplicitComparison) {
110 stmt(anyOf(mapAnyOf(ifStmt, whileStmt, doStmt, forStmt)
111 .with(hasCondition(StringCompareCallExpr)),
112 binaryOperator(hasAnyOperatorName(
"&&",
"||"),
113 hasEitherOperand(StringCompareCallExpr))))
114 .bind(
"missing-comparison"),
118 if (WarnOnLogicalNotComparison) {
121 Finder->addMatcher(unaryOperator(hasOperatorName(
"!"),
122 hasUnaryOperand(ignoringParenImpCasts(
123 StringCompareCallExpr)))
124 .bind(
"logical-not-comparison"),
131 implicitCastExpr(unless(hasType(isInteger())),
132 hasSourceExpression(StringCompareCallExpr))
133 .bind(
"invalid-conversion")),
138 binaryOperator(unless(anyOf(isComparisonOperator(), hasOperatorName(
"&&"),
139 hasOperatorName(
"||"), hasOperatorName(
"="))),
140 hasEitherOperand(StringCompareCallExpr))
141 .bind(
"suspicious-operator"),
145 const auto InvalidLiteral = ignoringParenImpCasts(
146 anyOf(integerLiteral(unless(equals(0))),
148 hasOperatorName(
"-"),
149 has(ignoringParenImpCasts(integerLiteral(unless(equals(0)))))),
150 characterLiteral(), cxxBoolLiteral()));
153 binaryOperator(isComparisonOperator(),
154 hasOperands(StringCompareCallExpr, InvalidLiteral))
155 .bind(
"invalid-comparison"),
160 const MatchFinder::MatchResult &Result) {
161 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>(
"decl");
162 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
163 assert(Decl !=
nullptr && Call !=
nullptr);
165 if (Result.Nodes.getNodeAs<Stmt>(
"missing-comparison")) {
166 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
167 Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
170 diag(Call->getBeginLoc(),
171 "function %0 is called without explicitly comparing result")
172 << Decl << FixItHint::CreateInsertion(EndLoc,
" != 0");
175 if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"logical-not-comparison")) {
176 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
177 Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
179 SourceLocation NotLoc = E->getBeginLoc();
181 diag(Call->getBeginLoc(),
182 "function %0 is compared using logical not operator")
184 << FixItHint::CreateRemoval(
185 CharSourceRange::getTokenRange(NotLoc, NotLoc))
186 << FixItHint::CreateInsertion(EndLoc,
" == 0");
189 if (Result.Nodes.getNodeAs<Stmt>(
"invalid-comparison")) {
190 diag(Call->getBeginLoc(),
191 "function %0 is compared to a suspicious constant")
195 if (
const auto *BinOp =
196 Result.Nodes.getNodeAs<BinaryOperator>(
"suspicious-operator")) {
197 diag(Call->getBeginLoc(),
"results of function %0 used by operator '%1'")
198 << Decl << BinOp->getOpcodeStr();
201 if (Result.Nodes.getNodeAs<Stmt>(
"invalid-conversion")) {
202 diag(Call->getBeginLoc(),
"function %0 has suspicious implicit cast")
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.