10#include "clang/AST/Expr.h"
11#include "clang/AST/RecursiveASTVisitor.h"
12#include "clang/Basic/DiagnosticIDs.h"
13#include "clang/Lex/Lexer.h"
14#include "llvm/Support/SaveAndRestore.h"
24static StringRef
getText(
const ASTContext &Context, SourceRange Range) {
25 return Lexer::getSourceText(CharSourceRange::getTokenRange(Range),
26 Context.getSourceManager(),
27 Context.getLangOpts());
31static StringRef
getText(
const ASTContext &Context, T &Node) {
32 return getText(Context, Node.getSourceRange());
36 "redundant boolean literal supplied to boolean operator";
38 "redundant boolean literal in if statement condition";
40 "redundant boolean literal in conditional return statement";
43 E = E->IgnoreImpCasts();
44 if (isa<BinaryOperator>(E) || isa<ConditionalOperator>(E))
47 if (
const auto *Op = dyn_cast<CXXOperatorCallExpr>(E))
48 return Op->getNumArgs() == 2 && Op->getOperator() != OO_Call &&
49 Op->getOperator() != OO_Subscript;
54static std::pair<BinaryOperatorKind, BinaryOperatorKind>
Opposites[] = {
55 {BO_LT, BO_GE}, {BO_GT, BO_LE}, {BO_EQ, BO_NE}};
58 const BinaryOperatorKind Opcode = BinOp->getOpcode();
60 if (Opcode == NegatableOp.first)
61 return BinaryOperator::getOpcodeStr(NegatableOp.second);
62 if (Opcode == NegatableOp.second)
63 return BinaryOperator::getOpcodeStr(NegatableOp.first);
69 {OO_EqualEqual,
"=="}, {OO_ExclaimEqual,
"!="}, {OO_Less,
"<"},
70 {OO_GreaterEqual,
">="}, {OO_Greater,
">"}, {OO_LessEqual,
"<="}};
74 if (Name.first == OpKind)
80static std::pair<OverloadedOperatorKind, OverloadedOperatorKind>
82 {OO_Less, OO_GreaterEqual},
83 {OO_Greater, OO_LessEqual}};
86 const OverloadedOperatorKind Opcode = OpCall->getOperator();
88 if (Opcode == NegatableOp.first)
90 if (Opcode == NegatableOp.second)
96static std::string
asBool(StringRef Text,
bool NeedsStaticCast) {
98 return (
"static_cast<bool>(" + Text +
")").str();
100 return std::string(Text);
104 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E))
105 return ImpCast->getCastKind() == CK_PointerToBoolean ||
106 ImpCast->getCastKind() == CK_MemberPointerToBoolean;
112 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E))
113 return ImpCast->getCastKind() == CK_IntegralToBoolean;
119 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
120 if (ImpCast->getCastKind() == CK_UserDefinedConversion &&
121 ImpCast->getSubExpr()->getType()->isBooleanType()) {
122 if (
const auto *MemCall =
123 dyn_cast<CXXMemberCallExpr>(ImpCast->getSubExpr())) {
124 if (
const auto *MemDecl =
125 dyn_cast<CXXConversionDecl>(MemCall->getMethodDecl())) {
126 if (MemDecl->isExplicit())
133 E = E->IgnoreImpCasts();
134 return !E->getType()->isBooleanType();
138 const Expr *E,
bool Negated,
139 const char *Constant) {
140 E = E->IgnoreImpCasts();
141 const std::string ExprText =
142 (isa<BinaryOperator>(E) ? (
"(" +
getText(Context, *E) +
")")
145 return ExprText +
" " + (Negated ?
"!=" :
"==") +
" " + Constant;
149 const Expr *E,
bool Negated) {
150 const char *NullPtr = Context.getLangOpts().CPlusPlus11 ?
"nullptr" :
"NULL";
155 const Expr *E,
bool Negated) {
160 bool Negated,
const Expr *E) {
161 E = E->IgnoreParenBaseCasts();
162 if (
const auto *EC = dyn_cast<ExprWithCleanups>(E))
163 E = EC->getSubExpr();
167 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E)) {
168 if (UnOp->getOpcode() == UO_LNot) {
185 StringRef NegatedOperator;
186 const Expr *LHS =
nullptr;
187 const Expr *RHS =
nullptr;
188 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
190 LHS = BinOp->getLHS();
191 RHS = BinOp->getRHS();
192 }
else if (
const auto *OpExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
193 if (OpExpr->getNumArgs() == 2) {
195 LHS = OpExpr->getArg(0);
196 RHS = OpExpr->getArg(1);
199 if (!NegatedOperator.empty() && LHS && RHS)
200 return (
asBool((
getText(Context, *LHS) +
" " + NegatedOperator +
" " +
205 const StringRef Text =
getText(Context, *E);
207 return (
"!(" + Text +
")").str();
215 return (
"!" +
asBool(Text, NeedsStaticCast));
218 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E)) {
219 if (UnOp->getOpcode() == UO_LNot) {
238 CharSourceRange CharRange) {
239 std::string ReplacementText =
240 Lexer::getSourceText(CharRange, Context.getSourceManager(),
241 Context.getLangOpts())
243 Lexer Lex(CharRange.getBegin(), Context.getLangOpts(), ReplacementText.data(),
244 ReplacementText.data(),
245 ReplacementText.data() + ReplacementText.size());
246 Lex.SetCommentRetentionState(
true);
249 while (!Lex.LexFromRawLexer(Tok))
250 if (Tok.is(tok::TokenKind::comment) || Tok.is(tok::TokenKind::hash))
257 using Base = RecursiveASTVisitor<Visitor>;
261 : Check(Check), Context(Context) {}
266 switch (S->getStmtClass()) {
267 case Stmt::ImplicitCastExprClass:
268 case Stmt::MaterializeTemporaryExprClass:
269 case Stmt::CXXBindTemporaryExprClass:
279 if (Check->canBeBypassed(S))
282 StmtStack.push_back(S);
288 assert(StmtStack.back() == S);
289 StmtStack.pop_back();
295 Check->reportBinOp(Context, Op);
301 if (
const auto *Bool = dyn_cast<CXXBoolLiteralExpr>(E)) {
302 if (FilterMacro && Bool->getBeginLoc().isMacroID())
304 return Bool->getValue();
306 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E)) {
307 if (FilterMacro && UnaryOp->getBeginLoc().isMacroID())
309 if (UnaryOp->getOpcode() == UO_LNot)
311 UnaryOp->getSubExpr()->IgnoreImplicit(), FilterMacro))
321 operator bool()
const {
return Item !=
nullptr; }
329 const auto *RS = dyn_cast<ReturnStmt>(S);
330 if (!RS || !RS->getRetValue())
332 if (std::optional<bool> Ret =
334 return {RS->getRetValue(), *Ret};
342 template <
typename Functor>
344 if (
auto *CS = dyn_cast<CompoundStmt>(S)) {
346 return F(CS->body_front());
353 return StmtStack.size() < 2 ? nullptr : StmtStack[StmtStack.size() - 2];
359 if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval())
366 const Expr *Cond = If->getCond()->IgnoreImplicit();
369 Check->replaceWithThenStatement(Context, If, Cond);
371 Check->replaceWithElseStatement(Context, If, Cond);
383 if (ElseReturnBool && ThenReturnBool.
Bool != ElseReturnBool.
Bool) {
384 if (Check->ChainedConditionalReturn ||
385 !isa_and_nonnull<IfStmt>(
parent())) {
386 Check->replaceWithReturnCondition(Context, If, ThenReturnBool.Item,
387 ElseReturnBool.
Bool);
397 auto VarBoolAssignmentMatcher = [&Var,
399 const auto *BO = dyn_cast<BinaryOperator>(S);
400 if (!BO || BO->getOpcode() != BO_Assign)
402 std::optional<bool> RightasBool =
406 Expr *IgnImp = BO->getLHS()->IgnoreImplicit();
409 Loc = BO->getRHS()->getBeginLoc();
412 if (
auto *DRE = dyn_cast<DeclRefExpr>(IgnImp))
413 return {DRE->getDecl(), *RightasBool};
414 if (
auto *ME = dyn_cast<MemberExpr>(IgnImp))
415 return {ME->getMemberDecl(), *RightasBool};
422 if (ElseAssignment.
Item == ThenAssignment.Item &&
423 ElseAssignment.
Bool != ThenAssignment.Bool) {
424 if (Check->ChainedConditionalAssignment ||
425 !isa_and_nonnull<IfStmt>(
parent())) {
426 Check->replaceWithAssignment(Context, If, Var, Loc,
427 ElseAssignment.
Bool);
441 if (std::optional<bool> Then =
443 if (std::optional<bool> Else =
446 Check->replaceWithCondition(Context, Cond, *Else);
455 bool CurIf =
false, PrevIf =
false;
456 for (
auto First = CS->body_begin(), Second = std::next(First),
457 End = CS->body_end();
458 Second != End; ++Second, ++First) {
460 CurIf = isa<IfStmt>(*First);
462 if (!TrailingReturnBool)
470 auto *If = cast<IfStmt>(*First);
471 if (!If->hasInitStorage() && !If->hasVarStorage() &&
472 !If->isConsteval()) {
475 if (ThenReturnBool &&
476 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
477 if ((Check->ChainedConditionalReturn || !PrevIf) &&
478 If->getElse() ==
nullptr) {
479 Check->replaceCompoundReturnWithCondition(
480 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
481 If, ThenReturnBool.
Item);
485 }
else if (isa<LabelStmt, CaseStmt, DefaultStmt>(*First)) {
491 isa<LabelStmt>(*First) ? cast<LabelStmt>(*First)->getSubStmt()
492 : isa<CaseStmt>(*First) ? cast<CaseStmt>(*First)->getSubStmt()
493 : cast<DefaultStmt>(*First)->getSubStmt();
494 auto *SubIf = dyn_cast<IfStmt>(SubStmt);
495 if (SubIf && !SubIf->getElse() && !SubIf->hasInitStorage() &&
496 !SubIf->hasVarStorage() && !SubIf->isConsteval()) {
499 if (ThenReturnBool &&
500 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
501 Check->replaceCompoundReturnWithCondition(
502 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
503 SubIf, ThenReturnBool.
Item);
512 return !Check->canBeBypassed(E) && isa<UnaryOperator>(E) &&
513 cast<UnaryOperator>(E)->getOpcode() == UO_LNot;
517 const auto *BinaryOp = dyn_cast<BinaryOperator>(E);
518 return !Check->canBeBypassed(E) && BinaryOp && BinaryOp->isLogicalOp() &&
519 BinaryOp->getType()->isBooleanType();
522 template <
typename Functor>
524 return Func(BO->getLHS()) || Func(BO->getRHS());
528 const auto *BO = dyn_cast<BinaryOperator>(E->IgnoreUnlessSpelledInSource());
531 if (!BO->getType()->isBooleanType())
533 switch (BO->getOpcode()) {
556 if (!Check->SimplifyDeMorgan || Op->getOpcode() != UO_LNot)
557 return Base::TraverseUnaryOperator(Op);
558 const Expr *SubImp = Op->getSubExpr()->IgnoreImplicit();
559 const auto *Parens = dyn_cast<ParenExpr>(SubImp);
560 const Expr *SubExpr =
561 Parens ? Parens->getSubExpr()->IgnoreImplicit() : SubImp;
563 return Base::TraverseUnaryOperator(Op);
564 const auto *BinaryOp = cast<BinaryOperator>(SubExpr);
565 if (Check->SimplifyDeMorganRelaxed ||
570 BinaryOp, [
this](
const Expr *E) {
return nestedDemorgan(E, 1); })) {
571 if (Check->reportDeMorgan(Context, Op, BinaryOp, !IsProcessing,
parent(),
573 !Check->areDiagsSelfContained()) {
574 const llvm::SaveAndRestore RAII(IsProcessing,
true);
575 return Base::TraverseUnaryOperator(Op);
578 return Base::TraverseUnaryOperator(Op);
582 bool IsProcessing =
false;
584 SmallVector<Stmt *, 32> StmtStack;
591 IgnoreMacros(Options.get(
"IgnoreMacros", false)),
592 ChainedConditionalReturn(Options.get(
"ChainedConditionalReturn", false)),
593 ChainedConditionalAssignment(
594 Options.get(
"ChainedConditionalAssignment", false)),
595 SimplifyDeMorgan(Options.get(
"SimplifyDeMorgan", true)),
596 SimplifyDeMorganRelaxed(Options.get(
"SimplifyDeMorganRelaxed", false)) {
597 if (SimplifyDeMorganRelaxed && !SimplifyDeMorgan)
598 configurationDiag(
"%0: 'SimplifyDeMorganRelaxed' cannot be enabled "
599 "without 'SimplifyDeMorgan' enabled")
606 E = E->IgnoreParenImpCasts();
607 if (isa<CXXBoolLiteralExpr>(E))
609 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
612 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E))
617void SimplifyBooleanExprCheck::reportBinOp(
const ASTContext &Context,
618 const BinaryOperator *Op) {
619 const auto *LHS = Op->getLHS()->IgnoreParenImpCasts();
620 const auto *RHS = Op->getRHS()->IgnoreParenImpCasts();
622 const CXXBoolLiteralExpr *Bool =
nullptr;
623 const Expr *Other =
nullptr;
624 if ((Bool = dyn_cast<CXXBoolLiteralExpr>(LHS)) !=
nullptr)
626 else if ((Bool = dyn_cast<CXXBoolLiteralExpr>(RHS)) !=
nullptr)
631 if (Bool->getBeginLoc().isMacroID())
638 const bool BoolValue = Bool->getValue();
640 auto ReplaceWithExpression = [
this, &Context, LHS, RHS,
641 Bool](
const Expr *ReplaceWith,
bool Negated) {
642 const std::string Replacement =
644 const SourceRange Range(LHS->getBeginLoc(), RHS->getEndLoc());
649 switch (Op->getOpcode()) {
653 ReplaceWithExpression(Other,
false);
656 ReplaceWithExpression(Bool,
false);
661 ReplaceWithExpression(Bool,
false);
664 ReplaceWithExpression(Other,
false);
668 ReplaceWithExpression(Other, !BoolValue);
672 ReplaceWithExpression(Other, BoolValue);
680 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
681 Options.store(Opts,
"ChainedConditionalReturn", ChainedConditionalReturn);
682 Options.store(Opts,
"ChainedConditionalAssignment",
683 ChainedConditionalAssignment);
684 Options.store(Opts,
"SimplifyDeMorgan", SimplifyDeMorgan);
685 Options.store(Opts,
"SimplifyDeMorganRelaxed", SimplifyDeMorganRelaxed);
689 Finder->addMatcher(translationUnitDecl(),
this);
696bool SimplifyBooleanExprCheck::canBeBypassed(
const Stmt *S)
const {
697 return IgnoreMacros && S->getBeginLoc().isMacroID();
701bool SimplifyBooleanExprCheck::issueDiag(
const ASTContext &Context,
703 StringRef Description,
704 SourceRange ReplacementRange,
705 StringRef Replacement) {
706 const CharSourceRange CharRange =
707 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(ReplacementRange),
708 Context.getSourceManager(), getLangOpts());
710 const DiagnosticBuilder Diag = diag(Loc, Description);
713 Diag << FixItHint::CreateReplacement(CharRange, Replacement);
714 return HasReplacement;
717void SimplifyBooleanExprCheck::replaceWithThenStatement(
718 const ASTContext &Context,
const IfStmt *IfStatement,
719 const Expr *BoolLiteral) {
721 IfStatement->getSourceRange(),
722 getText(Context, *IfStatement->getThen()));
725void SimplifyBooleanExprCheck::replaceWithElseStatement(
726 const ASTContext &Context,
const IfStmt *IfStatement,
727 const Expr *BoolLiteral) {
728 const Stmt *ElseStatement = IfStatement->getElse();
730 IfStatement->getSourceRange(),
731 ElseStatement ?
getText(Context, *ElseStatement) :
"");
734void SimplifyBooleanExprCheck::replaceWithCondition(
735 const ASTContext &Context,
const ConditionalOperator *Ternary,
737 const std::string Replacement =
739 issueDiag(Context, Ternary->getTrueExpr()->getBeginLoc(),
740 "redundant boolean literal in ternary expression result",
741 Ternary->getSourceRange(), Replacement);
744void SimplifyBooleanExprCheck::replaceWithReturnCondition(
745 const ASTContext &Context,
const IfStmt *If,
const Expr *BoolLiteral,
747 const StringRef Terminator = isa<CompoundStmt>(If->getElse()) ?
";" :
"";
748 const std::string Condition =
750 const std::string Replacement = (
"return " + Condition + Terminator).str();
751 const SourceLocation Start = BoolLiteral->getBeginLoc();
753 const bool HasReplacement =
755 If->getSourceRange(), Replacement);
757 if (!HasReplacement) {
758 const SourceRange ConditionRange = If->getCond()->getSourceRange();
759 if (ConditionRange.isValid())
760 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
766void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
767 const ASTContext &Context,
const ReturnStmt *Ret,
bool Negated,
768 const IfStmt *If,
const Expr *ThenReturn) {
769 const std::string Replacement =
772 const bool HasReplacement = issueDiag(
774 SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
776 if (!HasReplacement) {
777 const SourceRange ConditionRange = If->getCond()->getSourceRange();
778 if (ConditionRange.isValid())
779 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
782 const SourceRange ReturnRange = Ret->getSourceRange();
783 if (ReturnRange.isValid())
784 diag(ReturnRange.getBegin(),
"return statement that can be simplified",
790void SimplifyBooleanExprCheck::replaceWithAssignment(
const ASTContext &Context,
791 const IfStmt *IfAssign,
795 const SourceRange Range = IfAssign->getSourceRange();
796 const StringRef VariableName =
getText(Context, *Var);
797 const StringRef Terminator =
798 isa<CompoundStmt>(IfAssign->getElse()) ?
";" :
"";
799 const std::string Condition =
801 const std::string Replacement =
802 (VariableName +
" = " + Condition + Terminator).str();
803 issueDiag(Context, Loc,
"redundant boolean literal in conditional assignment",
809 const BinaryOperator *BO) {
810 assert(BO->isLogicalOp());
811 if (BO->getOperatorLoc().isMacroID())
813 Output.push_back(FixItHint::CreateReplacement(
814 BO->getOperatorLoc(), BO->getOpcode() == BO_LAnd ?
"||" :
"&&"));
819 assert(BinaryOperator::isLogicalOp(BO));
820 return BO == BO_LAnd ? BO_LOr : BO_LAnd;
824 const ASTContext &Ctx,
const Expr *E,
825 std::optional<BinaryOperatorKind> OuterBO);
832 const ASTContext &Ctx,
const BinaryOperator *BinOp,
833 std::optional<BinaryOperatorKind> OuterBO,
834 const ParenExpr *Parens =
nullptr) {
835 switch (BinOp->getOpcode()) {
849 constexpr bool LogicalOpParentheses =
true;
850 if (((*OuterBO == NewOp) || (!LogicalOpParentheses &&
851 (*OuterBO == BO_LOr && NewOp == BO_LAnd))) &&
853 if (!Parens->getLParen().isMacroID() &&
854 !Parens->getRParen().isMacroID()) {
855 Fixes.push_back(FixItHint::CreateRemoval(Parens->getLParen()));
856 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
859 if (*OuterBO == BO_LAnd && NewOp == BO_LOr && !Parens) {
860 Fixes.push_back(FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"("));
861 Fixes.push_back(FixItHint::CreateInsertion(
862 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
863 Ctx.getSourceManager(),
880 if (BinOp->getOperatorLoc().isMacroID())
882 Fixes.push_back(FixItHint::CreateReplacement(
883 BinOp->getOperatorLoc(),
884 BinaryOperator::getOpcodeStr(
885 BinaryOperator::negateComparisonOp(BinOp->getOpcode()))));
891 if (Parens->getBeginLoc().isMacroID())
893 Fixes.push_back(FixItHint::CreateInsertion(Parens->getBeginLoc(),
"!"));
895 if (BinOp->getBeginLoc().isMacroID() || BinOp->getEndLoc().isMacroID())
897 Fixes.append({FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"!("),
898 FixItHint::CreateInsertion(
899 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
900 Ctx.getSourceManager(),
910 const ASTContext &Ctx,
const Expr *E,
911 std::optional<BinaryOperatorKind> OuterBO) {
912 if (isa<UnaryOperator>(E) && cast<UnaryOperator>(E)->getOpcode() == UO_LNot) {
914 if (cast<UnaryOperator>(E)->getOperatorLoc().
isMacroID())
917 FixItHint::CreateRemoval(cast<UnaryOperator>(E)->getOperatorLoc()));
920 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
922 if (
const auto *Paren = dyn_cast<ParenExpr>(E)) {
923 if (
const auto *BinOp = dyn_cast<BinaryOperator>(Paren->getSubExpr()))
927 if (E->getBeginLoc().isMacroID())
929 Fixes.push_back(FixItHint::CreateInsertion(E->getBeginLoc(),
"!"));
934 BinaryOperatorKind NewOuterBinary,
935 const ParenExpr *Parens) {
940 switch (Parent->getStmtClass()) {
941 case Stmt::BinaryOperatorClass: {
942 const auto *BO = cast<BinaryOperator>(Parent);
943 if (BO->isAssignmentOp())
947 if (BO->getOpcode() == NewOuterBinary)
951 case Stmt::UnaryOperatorClass:
952 case Stmt::CXXRewrittenBinaryOperatorClass:
959bool SimplifyBooleanExprCheck::reportDeMorgan(
const ASTContext &Context,
960 const UnaryOperator *Outer,
961 const BinaryOperator *Inner,
964 const ParenExpr *Parens) {
967 assert(Inner->isLogicalOp());
970 diag(Outer->getBeginLoc(),
971 "boolean expression can be simplified by DeMorgan's theorem");
972 Diag << Outer->getSourceRange();
976 if (Outer->getOperatorLoc().isMacroID())
978 SmallVector<FixItHint> Fixes;
981 Fixes.push_back(FixItHint::CreateRemoval(
982 SourceRange(Outer->getOperatorLoc(), Parens->getLParen())));
983 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
985 Fixes.push_back(FixItHint::CreateRemoval(Outer->getOperatorLoc()));
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
NodeAndBool< Decl > DeclAndBool
bool TraverseUnaryOperator(UnaryOperator *Op)
Visitor(SimplifyBooleanExprCheck *Check, ASTContext &Context)
bool isExpectedBinaryOp(const Expr *E)
bool VisitConditionalOperator(ConditionalOperator *Cond)
static bool shouldIgnore(Stmt *S)
bool isExpectedUnaryLNot(const Expr *E)
static std::optional< bool > getAsBoolLiteral(const Expr *E, bool FilterMacro)
bool dataTraverseStmtPost(Stmt *S)
bool VisitCompoundStmt(CompoundStmt *CS)
bool dataTraverseStmtPre(Stmt *S)
static bool checkEitherSide(const BinaryOperator *BO, Functor Func)
static auto checkSingleStatement(Stmt *S, Functor F) -> decltype(F(S))
If S is not a CompoundStmt, applies F on S, otherwise if there is only 1 statement in the CompoundStm...
NodeAndBool< Expr > ExprAndBool
bool nestedDemorgan(const Expr *E, unsigned NestingLevel)
bool VisitBinaryOperator(const BinaryOperator *Op) const
static ExprAndBool parseReturnLiteralBool(const Stmt *S)
Detect's return (true|false|!true|!false);.
bool VisitIfStmt(IfStmt *If)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
SimplifyBooleanExprCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static std::string replacementExpression(const ASTContext &Context, bool Negated, const Expr *E)
static bool needsZeroComparison(const Expr *E)
static bool containsBoolLiteral(const Expr *E)
static StringRef negatedOperator(const BinaryOperator *BinOp)
static bool shouldRemoveParens(const Stmt *Parent, BinaryOperatorKind NewOuterBinary, const ParenExpr *Parens)
static std::string compareExpressionToConstant(const ASTContext &Context, const Expr *E, bool Negated, const char *Constant)
static std::pair< BinaryOperatorKind, BinaryOperatorKind > Opposites[]
static bool needsParensAfterUnaryNegation(const Expr *E)
static bool containsDiscardedTokens(const ASTContext &Context, CharSourceRange CharRange)
static StringRef getOperatorName(OverloadedOperatorKind OpKind)
static constexpr char SimplifyConditionDiagnostic[]
static bool flipDemorganOperator(llvm::SmallVectorImpl< FixItHint > &Output, const BinaryOperator *BO)
Swaps a BinaryOperator opcode from && to || or vice-versa.
static bool needsNullPtrComparison(const Expr *E)
static constexpr char SimplifyConditionalReturnDiagnostic[]
static bool flipDemorganBinaryOperator(SmallVectorImpl< FixItHint > &Fixes, const ASTContext &Ctx, const BinaryOperator *BinOp, std::optional< BinaryOperatorKind > OuterBO, const ParenExpr *Parens=nullptr)
Inverts BinOp, Removing Parens if they exist and are safe to remove.
static constexpr char SimplifyOperatorDiagnostic[]
static bool flipDemorganSide(SmallVectorImpl< FixItHint > &Fixes, const ASTContext &Ctx, const Expr *E, std::optional< BinaryOperatorKind > OuterBO)
static StringRef getText(const ASTContext &Context, SourceRange Range)
static BinaryOperatorKind getDemorganFlippedOperator(BinaryOperatorKind BO)
static std::string asBool(StringRef Text, bool NeedsStaticCast)
static std::pair< OverloadedOperatorKind, OverloadedOperatorKind > OppositeOverloads[]
static std::string compareExpressionToZero(const ASTContext &Context, const Expr *E, bool Negated)
static bool isMacroID(SourceRange R)
static std::string compareExpressionToNullPtr(const ASTContext &Context, const Expr *E, bool Negated)
static std::pair< OverloadedOperatorKind, StringRef > OperatorNames[]
static bool needsStaticCast(const Expr *E)
llvm::StringMap< ClangTidyValue > OptionMap