79 QualType ComparedType) {
80 const QualType LhsType = CondLhs->getType();
81 const QualType RhsType = CondRhs->getType();
82 const QualType LhsCanonicalType =
83 LhsType.getCanonicalType().getNonReferenceType().getUnqualifiedType();
84 const QualType RhsCanonicalType =
85 RhsType.getCanonicalType().getNonReferenceType().getUnqualifiedType();
86 QualType GlobalImplicitCastType;
87 if (LhsCanonicalType != RhsCanonicalType) {
88 if (llvm::isa<IntegerLiteral>(CondRhs))
90 else if (llvm::isa<IntegerLiteral>(CondLhs))
95 return GlobalImplicitCastType;
100 const Expr *AssignLhs,
const SourceManager &Source,
101 const LangOptions &LO, StringRef FunctionName,
102 const BinaryOperator *BO, StringRef Comment =
"") {
103 const llvm::StringRef CondLhsStr = Lexer::getSourceText(
104 Source.getExpansionRange(CondLhs->getSourceRange()), Source, LO);
105 const llvm::StringRef CondRhsStr = Lexer::getSourceText(
106 Source.getExpansionRange(CondRhs->getSourceRange()), Source, LO);
107 const llvm::StringRef AssignLhsStr = Lexer::getSourceText(
108 Source.getExpansionRange(AssignLhs->getSourceRange()), Source, LO);
110 const QualType GlobalImplicitCastType =
113 return (AssignLhsStr +
" = " + FunctionName +
114 (!GlobalImplicitCastType.isNull()
115 ?
"<" + GlobalImplicitCastType.getAsString() +
">("
117 CondLhsStr +
", " + CondRhsStr +
");" + (Comment.empty() ?
"" :
" ") +
133 auto AssignOperator =
134 binaryOperator(hasOperatorName(
"="),
135 hasLHS(expr(unless(isTypeDependent())).bind(
"AssignLhs")),
136 hasRHS(expr(unless(isTypeDependent())).bind(
"AssignRhs")));
137 auto BinaryOperator =
138 binaryOperator(hasAnyOperatorName(
"<",
">",
"<=",
">="),
139 hasLHS(expr(unless(isTypeDependent())).bind(
"CondLhs")),
140 hasRHS(expr(unless(isTypeDependent())).bind(
"CondRhs")))
143 ifStmt(stmt().bind(
"if"), unless(isIfInMacro()),
144 unless(hasElse(stmt())),
145 hasCondition(BinaryOperator),
147 anyOf(stmt(AssignOperator),
148 compoundStmt(statementCountIs(1), has(AssignOperator)))),
149 hasParent(stmt(unless(ifStmt(hasElse(
150 equalsBoundNode(
"if"))))))),
161 const auto *If = Result.Nodes.getNodeAs<IfStmt>(
"if");
162 const clang::LangOptions &LO = Result.Context->getLangOpts();
163 const auto *CondLhs = Result.Nodes.getNodeAs<Expr>(
"CondLhs");
164 const auto *CondRhs = Result.Nodes.getNodeAs<Expr>(
"CondRhs");
165 const auto *AssignLhs = Result.Nodes.getNodeAs<Expr>(
"AssignLhs");
166 const auto *AssignRhs = Result.Nodes.getNodeAs<Expr>(
"AssignRhs");
167 const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>(
"binaryOp");
168 const clang::BinaryOperatorKind BinaryOpcode = BinaryOp->getOpcode();
169 const SourceLocation IfLocation = If->getIfLoc();
170 const SourceLocation ThenLocation = If->getEndLoc();
172 auto ReplaceAndDiagnose = [&](
const llvm::StringRef FunctionName) {
173 const SourceManager &Source = *Result.SourceManager;
174 llvm::SmallString<64> Comment;
176 const auto AppendNormalized = [&](llvm::StringRef Text) {
179 if (!Comment.empty())
185 const auto GetSourceText = [&](SourceLocation StartLoc,
186 SourceLocation EndLoc) {
187 return Lexer::getSourceText(
188 CharSourceRange::getCharRange(
189 Lexer::getLocForEndOfToken(StartLoc, 0, Source, LO), EndLoc),
198 GetSourceText(If->getRParenLoc(), If->getThen()->getBeginLoc()));
200 if (
const auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
201 const Stmt *Inner = CS->body_front();
208 AppendNormalized(GetSourceText(CS->getBeginLoc(), Inner->getBeginLoc()));
213 llvm::StringRef PostInner =
214 GetSourceText(Inner->getEndLoc(), CS->getEndLoc());
218 const size_t Semi = PostInner.find(
';');
219 if (Semi != llvm::StringRef::npos &&
220 PostInner.take_front(Semi).trim().empty()) {
221 PostInner = PostInner.drop_front(Semi + 1);
223 AppendNormalized(PostInner);
226 diag(IfLocation,
"use `%0` instead of `%1`")
227 << FunctionName << BinaryOp->getOpcodeStr()
228 << FixItHint::CreateReplacement(
229 SourceRange(IfLocation, Lexer::getLocForEndOfToken(
230 ThenLocation, 0, Source, LO)),
232 FunctionName, BinaryOp, Comment))
233 << IncludeInserter.createIncludeInsertion(
237 if (
minCondition(BinaryOpcode, CondLhs, CondRhs, AssignLhs, AssignRhs,
238 (*Result.Context))) {
239 ReplaceAndDiagnose(
"std::min");
240 }
else if (
maxCondition(BinaryOpcode, CondLhs, CondRhs, AssignLhs, AssignRhs,
241 (*Result.Context))) {
242 ReplaceAndDiagnose(
"std::max");
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.