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)
81static std::pair<OverloadedOperatorKind, OverloadedOperatorKind>
83 {OO_Less, OO_GreaterEqual},
84 {OO_Greater, OO_LessEqual}};
87 const OverloadedOperatorKind Opcode = OpCall->getOperator();
89 if (Opcode == NegatableOp.first)
91 if (Opcode == NegatableOp.second)
97static std::string
asBool(StringRef Text,
bool NeedsStaticCast) {
99 return (
"static_cast<bool>(" + Text +
")").str();
101 return std::string(Text);
105 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E))
106 return ImpCast->getCastKind() == CK_PointerToBoolean ||
107 ImpCast->getCastKind() == CK_MemberPointerToBoolean;
113 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E))
114 return ImpCast->getCastKind() == CK_IntegralToBoolean;
120 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
121 if (ImpCast->getCastKind() == CK_UserDefinedConversion &&
122 ImpCast->getSubExpr()->getType()->isBooleanType()) {
123 if (
const auto *MemCall =
124 dyn_cast<CXXMemberCallExpr>(ImpCast->getSubExpr())) {
125 if (
const auto *MemDecl =
126 dyn_cast<CXXConversionDecl>(MemCall->getMethodDecl())) {
127 if (MemDecl->isExplicit())
134 E = E->IgnoreImpCasts();
135 return !E->getType()->isBooleanType();
139 const Expr *E,
bool Negated,
140 const char *Constant) {
141 E = E->IgnoreImpCasts();
142 const std::string ExprText =
143 (isa<BinaryOperator>(E) ? (
"(" +
getText(Context, *E) +
")")
146 return ExprText +
" " + (Negated ?
"!=" :
"==") +
" " + Constant;
150 const Expr *E,
bool Negated) {
151 const char *NullPtr = Context.getLangOpts().CPlusPlus11 ?
"nullptr" :
"NULL";
156 const Expr *E,
bool Negated) {
161 bool Negated,
const Expr *E) {
162 E = E->IgnoreParenBaseCasts();
163 if (
const auto *EC = dyn_cast<ExprWithCleanups>(E))
164 E = EC->getSubExpr();
168 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E)) {
169 if (UnOp->getOpcode() == UO_LNot) {
186 StringRef NegatedOperator;
187 const Expr *LHS =
nullptr;
188 const Expr *RHS =
nullptr;
189 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
191 LHS = BinOp->getLHS();
192 RHS = BinOp->getRHS();
193 }
else if (
const auto *OpExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
194 if (OpExpr->getNumArgs() == 2) {
196 LHS = OpExpr->getArg(0);
197 RHS = OpExpr->getArg(1);
200 if (!NegatedOperator.empty() && LHS && RHS)
201 return (
asBool((
getText(Context, *LHS) +
" " + NegatedOperator +
" " +
206 StringRef Text =
getText(Context, *E);
208 return (
"!(" + Text +
")").str();
216 return (
"!" +
asBool(Text, NeedsStaticCast));
219 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E)) {
220 if (UnOp->getOpcode() == UO_LNot) {
239 CharSourceRange CharRange) {
240 std::string ReplacementText =
241 Lexer::getSourceText(CharRange, Context.getSourceManager(),
242 Context.getLangOpts())
244 Lexer Lex(CharRange.getBegin(), Context.getLangOpts(), ReplacementText.data(),
245 ReplacementText.data(),
246 ReplacementText.data() + ReplacementText.size());
247 Lex.SetCommentRetentionState(
true);
250 while (!Lex.LexFromRawLexer(Tok)) {
251 if (Tok.is(tok::TokenKind::comment) || Tok.is(tok::TokenKind::hash))
259 using Base = RecursiveASTVisitor<Visitor>;
263 : Check(Check), Context(Context) {}
268 switch (S->getStmtClass()) {
269 case Stmt::ImplicitCastExprClass:
270 case Stmt::MaterializeTemporaryExprClass:
271 case Stmt::CXXBindTemporaryExprClass:
282 if (Check->canBeBypassed(S))
285 StmtStack.push_back(S);
291 assert(StmtStack.back() == S);
292 StmtStack.pop_back();
298 Check->reportBinOp(Context, Op);
304 if (
const auto *Bool = dyn_cast<CXXBoolLiteralExpr>(E)) {
305 if (FilterMacro && Bool->getBeginLoc().isMacroID())
307 return Bool->getValue();
309 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E)) {
310 if (FilterMacro && UnaryOp->getBeginLoc().isMacroID())
312 if (UnaryOp->getOpcode() == UO_LNot)
314 UnaryOp->getSubExpr()->IgnoreImplicit(), FilterMacro))
324 operator bool()
const {
return Item !=
nullptr; }
332 const auto *RS = dyn_cast<ReturnStmt>(S);
333 if (!RS || !RS->getRetValue())
335 if (std::optional<bool> Ret =
337 return {RS->getRetValue(), *Ret};
345 template <
typename Functor>
347 if (
auto *CS = dyn_cast<CompoundStmt>(S)) {
349 return F(CS->body_front());
356 return StmtStack.size() < 2 ? nullptr : StmtStack[StmtStack.size() - 2];
362 if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval())
369 Expr *Cond = If->getCond()->IgnoreImplicit();
372 Check->replaceWithThenStatement(Context, If, Cond);
374 Check->replaceWithElseStatement(Context, If, Cond);
386 if (ElseReturnBool && ThenReturnBool.
Bool != ElseReturnBool.
Bool) {
387 if (Check->ChainedConditionalReturn ||
388 !isa_and_nonnull<IfStmt>(
parent())) {
389 Check->replaceWithReturnCondition(Context, If, ThenReturnBool.Item,
390 ElseReturnBool.
Bool);
400 auto VarBoolAssignmentMatcher = [&Var,
402 const auto *BO = dyn_cast<BinaryOperator>(S);
403 if (!BO || BO->getOpcode() != BO_Assign)
405 std::optional<bool> RightasBool =
409 Expr *IgnImp = BO->getLHS()->IgnoreImplicit();
412 Loc = BO->getRHS()->getBeginLoc();
415 if (
auto *DRE = dyn_cast<DeclRefExpr>(IgnImp))
416 return {DRE->getDecl(), *RightasBool};
417 if (
auto *ME = dyn_cast<MemberExpr>(IgnImp))
418 return {ME->getMemberDecl(), *RightasBool};
425 if (ElseAssignment.
Item == ThenAssignment.Item &&
426 ElseAssignment.
Bool != ThenAssignment.Bool) {
427 if (Check->ChainedConditionalAssignment ||
428 !isa_and_nonnull<IfStmt>(
parent())) {
429 Check->replaceWithAssignment(Context, If, Var, Loc,
430 ElseAssignment.
Bool);
444 if (std::optional<bool> Then =
446 if (std::optional<bool> Else =
449 Check->replaceWithCondition(Context, Cond, *Else);
458 bool CurIf =
false, PrevIf =
false;
459 for (
auto First = CS->body_begin(), Second = std::next(First),
460 End = CS->body_end();
461 Second != End; ++Second, ++First) {
463 CurIf = isa<IfStmt>(*First);
465 if (!TrailingReturnBool)
473 auto *If = cast<IfStmt>(*First);
474 if (!If->hasInitStorage() && !If->hasVarStorage() &&
475 !If->isConsteval()) {
478 if (ThenReturnBool &&
479 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
480 if ((Check->ChainedConditionalReturn || !PrevIf) &&
481 If->getElse() ==
nullptr) {
482 Check->replaceCompoundReturnWithCondition(
483 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
484 If, ThenReturnBool.
Item);
488 }
else if (isa<LabelStmt, CaseStmt, DefaultStmt>(*First)) {
494 isa<LabelStmt>(*First) ? cast<LabelStmt>(*First)->getSubStmt()
495 : isa<CaseStmt>(*First) ? cast<CaseStmt>(*First)->getSubStmt()
496 : cast<DefaultStmt>(*First)->getSubStmt();
497 auto *SubIf = dyn_cast<IfStmt>(SubStmt);
498 if (SubIf && !SubIf->getElse() && !SubIf->hasInitStorage() &&
499 !SubIf->hasVarStorage() && !SubIf->isConsteval()) {
502 if (ThenReturnBool &&
503 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
504 Check->replaceCompoundReturnWithCondition(
505 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
506 SubIf, ThenReturnBool.
Item);
515 return !Check->canBeBypassed(E) && isa<UnaryOperator>(E) &&
516 cast<UnaryOperator>(E)->getOpcode() == UO_LNot;
520 const auto *BinaryOp = dyn_cast<BinaryOperator>(E);
521 return !Check->canBeBypassed(E) && BinaryOp && BinaryOp->isLogicalOp() &&
522 BinaryOp->getType()->isBooleanType();
525 template <
typename Functor>
527 return Func(BO->getLHS()) || Func(BO->getRHS());
531 const auto *BO = dyn_cast<BinaryOperator>(E->IgnoreUnlessSpelledInSource());
534 if (!BO->getType()->isBooleanType())
536 switch (BO->getOpcode()) {
559 if (!Check->SimplifyDeMorgan || Op->getOpcode() != UO_LNot)
560 return Base::TraverseUnaryOperator(Op);
561 const Expr *SubImp = Op->getSubExpr()->IgnoreImplicit();
562 const auto *Parens = dyn_cast<ParenExpr>(SubImp);
563 const Expr *SubExpr =
564 Parens ? Parens->getSubExpr()->IgnoreImplicit() : SubImp;
566 return Base::TraverseUnaryOperator(Op);
567 const auto *BinaryOp = cast<BinaryOperator>(SubExpr);
568 if (Check->SimplifyDeMorganRelaxed ||
573 BinaryOp, [
this](
const Expr *E) {
return nestedDemorgan(E, 1); })) {
574 if (Check->reportDeMorgan(Context, Op, BinaryOp, !IsProcessing,
parent(),
576 !Check->areDiagsSelfContained()) {
577 llvm::SaveAndRestore RAII(IsProcessing,
true);
578 return Base::TraverseUnaryOperator(Op);
581 return Base::TraverseUnaryOperator(Op);
585 bool IsProcessing =
false;
587 SmallVector<Stmt *, 32> StmtStack;
594 IgnoreMacros(Options.get(
"IgnoreMacros", false)),
595 ChainedConditionalReturn(Options.get(
"ChainedConditionalReturn", false)),
596 ChainedConditionalAssignment(
597 Options.get(
"ChainedConditionalAssignment", false)),
598 SimplifyDeMorgan(Options.get(
"SimplifyDeMorgan", true)),
599 SimplifyDeMorganRelaxed(Options.get(
"SimplifyDeMorganRelaxed", false)) {
600 if (SimplifyDeMorganRelaxed && !SimplifyDeMorgan)
601 configurationDiag(
"%0: 'SimplifyDeMorganRelaxed' cannot be enabled "
602 "without 'SimplifyDeMorgan' enabled")
609 E = E->IgnoreParenImpCasts();
610 if (isa<CXXBoolLiteralExpr>(E))
612 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
615 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E))
620void SimplifyBooleanExprCheck::reportBinOp(
const ASTContext &Context,
621 const BinaryOperator *Op) {
622 const auto *LHS = Op->getLHS()->IgnoreParenImpCasts();
623 const auto *RHS = Op->getRHS()->IgnoreParenImpCasts();
625 const CXXBoolLiteralExpr *Bool =
nullptr;
626 const Expr *Other =
nullptr;
627 if ((Bool = dyn_cast<CXXBoolLiteralExpr>(LHS)) !=
nullptr)
629 else if ((Bool = dyn_cast<CXXBoolLiteralExpr>(RHS)) !=
nullptr)
634 if (Bool->getBeginLoc().isMacroID())
641 bool BoolValue = Bool->getValue();
643 auto ReplaceWithExpression = [
this, &Context, LHS, RHS,
644 Bool](
const Expr *ReplaceWith,
bool Negated) {
645 std::string Replacement =
647 SourceRange Range(LHS->getBeginLoc(), RHS->getEndLoc());
652 switch (Op->getOpcode()) {
656 ReplaceWithExpression(Other,
false);
659 ReplaceWithExpression(Bool,
false);
664 ReplaceWithExpression(Bool,
false);
667 ReplaceWithExpression(Other,
false);
671 ReplaceWithExpression(Other, !BoolValue);
675 ReplaceWithExpression(Other, BoolValue);
683 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
684 Options.store(Opts,
"ChainedConditionalReturn", ChainedConditionalReturn);
685 Options.store(Opts,
"ChainedConditionalAssignment",
686 ChainedConditionalAssignment);
687 Options.store(Opts,
"SimplifyDeMorgan", SimplifyDeMorgan);
688 Options.store(Opts,
"SimplifyDeMorganRelaxed", SimplifyDeMorganRelaxed);
692 Finder->addMatcher(translationUnitDecl(),
this);
699bool SimplifyBooleanExprCheck::canBeBypassed(
const Stmt *S)
const {
700 return IgnoreMacros && S->getBeginLoc().isMacroID();
704bool SimplifyBooleanExprCheck::issueDiag(
const ASTContext &Context,
706 StringRef Description,
707 SourceRange ReplacementRange,
708 StringRef Replacement) {
709 CharSourceRange CharRange =
710 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(ReplacementRange),
711 Context.getSourceManager(), getLangOpts());
713 DiagnosticBuilder Diag = diag(Loc, Description);
716 Diag << FixItHint::CreateReplacement(CharRange, Replacement);
717 return HasReplacement;
720void SimplifyBooleanExprCheck::replaceWithThenStatement(
721 const ASTContext &Context,
const IfStmt *IfStatement,
722 const Expr *BoolLiteral) {
724 IfStatement->getSourceRange(),
725 getText(Context, *IfStatement->getThen()));
728void SimplifyBooleanExprCheck::replaceWithElseStatement(
729 const ASTContext &Context,
const IfStmt *IfStatement,
730 const Expr *BoolLiteral) {
731 const Stmt *ElseStatement = IfStatement->getElse();
733 IfStatement->getSourceRange(),
734 ElseStatement ?
getText(Context, *ElseStatement) :
"");
737void SimplifyBooleanExprCheck::replaceWithCondition(
738 const ASTContext &Context,
const ConditionalOperator *Ternary,
740 std::string Replacement =
742 issueDiag(Context, Ternary->getTrueExpr()->getBeginLoc(),
743 "redundant boolean literal in ternary expression result",
744 Ternary->getSourceRange(), Replacement);
747void SimplifyBooleanExprCheck::replaceWithReturnCondition(
748 const ASTContext &Context,
const IfStmt *If,
const Expr *BoolLiteral,
750 StringRef Terminator = isa<CompoundStmt>(If->getElse()) ?
";" :
"";
751 std::string Condition =
753 std::string Replacement = (
"return " + Condition + Terminator).str();
754 SourceLocation Start = BoolLiteral->getBeginLoc();
756 const bool HasReplacement =
758 If->getSourceRange(), Replacement);
760 if (!HasReplacement) {
761 const SourceRange ConditionRange = If->getCond()->getSourceRange();
762 if (ConditionRange.isValid())
763 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
769void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
770 const ASTContext &Context,
const ReturnStmt *Ret,
bool Negated,
771 const IfStmt *If,
const Expr *ThenReturn) {
772 const std::string Replacement =
775 const bool HasReplacement = issueDiag(
777 SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
779 if (!HasReplacement) {
780 const SourceRange ConditionRange = If->getCond()->getSourceRange();
781 if (ConditionRange.isValid())
782 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
785 const SourceRange ReturnRange = Ret->getSourceRange();
786 if (ReturnRange.isValid())
787 diag(ReturnRange.getBegin(),
"return statement that can be simplified",
793void SimplifyBooleanExprCheck::replaceWithAssignment(
const ASTContext &Context,
794 const IfStmt *IfAssign,
798 SourceRange Range = IfAssign->getSourceRange();
799 StringRef VariableName =
getText(Context, *Var);
800 StringRef Terminator = isa<CompoundStmt>(IfAssign->getElse()) ?
";" :
"";
801 std::string Condition =
803 std::string Replacement =
804 (VariableName +
" = " + Condition + Terminator).str();
805 issueDiag(Context, Loc,
"redundant boolean literal in conditional assignment",
811 const BinaryOperator *BO) {
812 assert(BO->isLogicalOp());
813 if (BO->getOperatorLoc().isMacroID())
815 Output.push_back(FixItHint::CreateReplacement(
816 BO->getOperatorLoc(), BO->getOpcode() == BO_LAnd ?
"||" :
"&&"));
821 assert(BinaryOperator::isLogicalOp(BO));
822 return BO == BO_LAnd ? BO_LOr : BO_LAnd;
826 const ASTContext &Ctx,
const Expr *E,
827 std::optional<BinaryOperatorKind> OuterBO);
834 const ASTContext &Ctx,
const BinaryOperator *BinOp,
835 std::optional<BinaryOperatorKind> OuterBO,
836 const ParenExpr *Parens =
nullptr) {
837 switch (BinOp->getOpcode()) {
851 constexpr bool LogicalOpParentheses =
true;
852 if (((*OuterBO == NewOp) || (!LogicalOpParentheses &&
853 (*OuterBO == BO_LOr && NewOp == BO_LAnd))) &&
855 if (!Parens->getLParen().isMacroID() &&
856 !Parens->getRParen().isMacroID()) {
857 Fixes.push_back(FixItHint::CreateRemoval(Parens->getLParen()));
858 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
861 if (*OuterBO == BO_LAnd && NewOp == BO_LOr && !Parens) {
862 Fixes.push_back(FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"("));
863 Fixes.push_back(FixItHint::CreateInsertion(
864 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
865 Ctx.getSourceManager(),
882 if (BinOp->getOperatorLoc().isMacroID())
884 Fixes.push_back(FixItHint::CreateReplacement(
885 BinOp->getOperatorLoc(),
886 BinaryOperator::getOpcodeStr(
887 BinaryOperator::negateComparisonOp(BinOp->getOpcode()))));
893 if (Parens->getBeginLoc().isMacroID())
895 Fixes.push_back(FixItHint::CreateInsertion(Parens->getBeginLoc(),
"!"));
897 if (BinOp->getBeginLoc().isMacroID() || BinOp->getEndLoc().isMacroID())
899 Fixes.append({FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"!("),
900 FixItHint::CreateInsertion(
901 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
902 Ctx.getSourceManager(),
912 const ASTContext &Ctx,
const Expr *E,
913 std::optional<BinaryOperatorKind> OuterBO) {
914 if (isa<UnaryOperator>(E) && cast<UnaryOperator>(E)->getOpcode() == UO_LNot) {
916 if (cast<UnaryOperator>(E)->getOperatorLoc().
isMacroID())
919 FixItHint::CreateRemoval(cast<UnaryOperator>(E)->getOperatorLoc()));
922 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
925 if (
const auto *Paren = dyn_cast<ParenExpr>(E)) {
926 if (
const auto *BinOp = dyn_cast<BinaryOperator>(Paren->getSubExpr())) {
931 if (E->getBeginLoc().isMacroID())
933 Fixes.push_back(FixItHint::CreateInsertion(E->getBeginLoc(),
"!"));
938 BinaryOperatorKind NewOuterBinary,
939 const ParenExpr *Parens) {
944 switch (Parent->getStmtClass()) {
945 case Stmt::BinaryOperatorClass: {
946 const auto *BO = cast<BinaryOperator>(Parent);
947 if (BO->isAssignmentOp())
951 if (BO->getOpcode() == NewOuterBinary)
955 case Stmt::UnaryOperatorClass:
956 case Stmt::CXXRewrittenBinaryOperatorClass:
963bool SimplifyBooleanExprCheck::reportDeMorgan(
const ASTContext &Context,
964 const UnaryOperator *Outer,
965 const BinaryOperator *Inner,
968 const ParenExpr *Parens) {
971 assert(Inner->isLogicalOp());
974 diag(Outer->getBeginLoc(),
975 "boolean expression can be simplified by DeMorgan's theorem");
976 Diag << Outer->getSourceRange();
980 if (Outer->getOperatorLoc().isMacroID())
982 SmallVector<FixItHint> Fixes;
985 Fixes.push_back(FixItHint::CreateRemoval(
986 SourceRange(Outer->getOperatorLoc(), Parens->getLParen())));
987 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
989 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