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"
26StringRef getText(
const ASTContext &Context, SourceRange Range) {
27 return Lexer::getSourceText(CharSourceRange::getTokenRange(Range),
28 Context.getSourceManager(),
29 Context.getLangOpts());
32template <
typename T> StringRef getText(
const ASTContext &Context, T &Node) {
33 return getText(Context, Node.getSourceRange());
39 "redundant boolean literal supplied to boolean operator";
41 "redundant boolean literal in if statement condition";
43 "redundant boolean literal in conditional return statement";
46 E = E->IgnoreImpCasts();
47 if (isa<BinaryOperator>(E) || isa<ConditionalOperator>(E))
50 if (
const auto *Op = dyn_cast<CXXOperatorCallExpr>(E))
51 return Op->getNumArgs() == 2 && Op->getOperator() != OO_Call &&
52 Op->getOperator() != OO_Subscript;
57static std::pair<BinaryOperatorKind, BinaryOperatorKind>
Opposites[] = {
58 {BO_LT, BO_GE}, {BO_GT, BO_LE}, {BO_EQ, BO_NE}};
61 const BinaryOperatorKind Opcode = BinOp->getOpcode();
63 if (Opcode == NegatableOp.first)
64 return BinaryOperator::getOpcodeStr(NegatableOp.second);
65 if (Opcode == NegatableOp.second)
66 return BinaryOperator::getOpcodeStr(NegatableOp.first);
72 {OO_EqualEqual,
"=="}, {OO_ExclaimEqual,
"!="}, {OO_Less,
"<"},
73 {OO_GreaterEqual,
">="}, {OO_Greater,
">"}, {OO_LessEqual,
"<="}};
77 if (Name.first == OpKind)
84static std::pair<OverloadedOperatorKind, OverloadedOperatorKind>
86 {OO_Less, OO_GreaterEqual},
87 {OO_Greater, OO_LessEqual}};
90 const OverloadedOperatorKind Opcode = OpCall->getOperator();
92 if (Opcode == NegatableOp.first)
94 if (Opcode == NegatableOp.second)
100static std::string
asBool(StringRef Text,
bool NeedsStaticCast) {
102 return (
"static_cast<bool>(" + Text +
")").str();
104 return std::string(Text);
108 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E))
109 return ImpCast->getCastKind() == CK_PointerToBoolean ||
110 ImpCast->getCastKind() == CK_MemberPointerToBoolean;
116 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E))
117 return ImpCast->getCastKind() == CK_IntegralToBoolean;
123 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
124 if (ImpCast->getCastKind() == CK_UserDefinedConversion &&
125 ImpCast->getSubExpr()->getType()->isBooleanType()) {
126 if (
const auto *MemCall =
127 dyn_cast<CXXMemberCallExpr>(ImpCast->getSubExpr())) {
128 if (
const auto *MemDecl =
129 dyn_cast<CXXConversionDecl>(MemCall->getMethodDecl())) {
130 if (MemDecl->isExplicit())
137 E = E->IgnoreImpCasts();
138 return !E->getType()->isBooleanType();
142 const Expr *E,
bool Negated,
143 const char *Constant) {
144 E = E->IgnoreImpCasts();
145 const std::string ExprText =
146 (isa<BinaryOperator>(E) ? (
"(" + getText(Context, *E) +
")")
147 : getText(Context, *E))
149 return ExprText +
" " + (Negated ?
"!=" :
"==") +
" " + Constant;
153 const Expr *E,
bool Negated) {
154 const char *NullPtr = Context.getLangOpts().CPlusPlus11 ?
"nullptr" :
"NULL";
159 const Expr *E,
bool Negated) {
164 bool Negated,
const Expr *E) {
165 E = E->IgnoreParenBaseCasts();
166 if (
const auto *EC = dyn_cast<ExprWithCleanups>(E))
167 E = EC->getSubExpr();
171 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E)) {
172 if (UnOp->getOpcode() == UO_LNot) {
189 StringRef NegatedOperator;
190 const Expr *LHS =
nullptr;
191 const Expr *RHS =
nullptr;
192 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
194 LHS = BinOp->getLHS();
195 RHS = BinOp->getRHS();
196 }
else if (
const auto *OpExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
197 if (OpExpr->getNumArgs() == 2) {
199 LHS = OpExpr->getArg(0);
200 RHS = OpExpr->getArg(1);
203 if (!NegatedOperator.empty() && LHS && RHS)
204 return (
asBool((getText(Context, *LHS) +
" " + NegatedOperator +
" " +
205 getText(Context, *RHS))
209 StringRef Text = getText(Context, *E);
211 return (
"!(" + Text +
")").str();
219 return (
"!" +
asBool(Text, NeedsStaticCast));
222 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E)) {
223 if (UnOp->getOpcode() == UO_LNot) {
238 return asBool(getText(Context, *E), NeedsStaticCast);
242 CharSourceRange CharRange) {
243 std::string ReplacementText =
244 Lexer::getSourceText(CharRange, Context.getSourceManager(),
245 Context.getLangOpts())
247 Lexer Lex(CharRange.getBegin(), Context.getLangOpts(), ReplacementText.data(),
248 ReplacementText.data(),
249 ReplacementText.data() + ReplacementText.size());
250 Lex.SetCommentRetentionState(
true);
253 while (!Lex.LexFromRawLexer(Tok)) {
254 if (Tok.is(tok::TokenKind::comment) || Tok.is(tok::TokenKind::hash))
262 using Base = RecursiveASTVisitor<Visitor>;
266 : Check(Check), Context(Context) {}
271 switch (S->getStmtClass()) {
272 case Stmt::ImplicitCastExprClass:
273 case Stmt::MaterializeTemporaryExprClass:
274 case Stmt::CXXBindTemporaryExprClass:
285 if (Check->canBeBypassed(S))
288 StmtStack.push_back(S);
294 assert(StmtStack.back() == S);
295 StmtStack.pop_back();
301 Check->reportBinOp(Context, Op);
307 if (
const auto *Bool = dyn_cast<CXXBoolLiteralExpr>(E)) {
308 if (FilterMacro && Bool->getBeginLoc().isMacroID())
310 return Bool->getValue();
312 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E)) {
313 if (FilterMacro && UnaryOp->getBeginLoc().isMacroID())
315 if (UnaryOp->getOpcode() == UO_LNot)
317 UnaryOp->getSubExpr()->IgnoreImplicit(), FilterMacro))
327 operator bool()
const {
return Item !=
nullptr; }
335 const auto *RS = dyn_cast<ReturnStmt>(S);
336 if (!RS || !RS->getRetValue())
338 if (std::optional<bool> Ret =
340 return {RS->getRetValue(), *Ret};
348 template <
typename Functor>
350 if (
auto *CS = dyn_cast<CompoundStmt>(S)) {
352 return F(CS->body_front());
359 return StmtStack.size() < 2 ? nullptr : StmtStack[StmtStack.size() - 2];
365 if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval())
372 Expr *Cond = If->getCond()->IgnoreImplicit();
375 Check->replaceWithThenStatement(Context, If, Cond);
377 Check->replaceWithElseStatement(Context, If, Cond);
389 if (ElseReturnBool && ThenReturnBool.
Bool != ElseReturnBool.
Bool) {
390 if (Check->ChainedConditionalReturn ||
391 !isa_and_nonnull<IfStmt>(
parent())) {
392 Check->replaceWithReturnCondition(Context, If, ThenReturnBool.Item,
393 ElseReturnBool.
Bool);
403 auto VarBoolAssignmentMatcher = [&Var,
405 const auto *BO = dyn_cast<BinaryOperator>(S);
406 if (!BO || BO->getOpcode() != BO_Assign)
408 std::optional<bool> RightasBool =
412 Expr *IgnImp = BO->getLHS()->IgnoreImplicit();
415 Loc = BO->getRHS()->getBeginLoc();
418 if (
auto *DRE = dyn_cast<DeclRefExpr>(IgnImp))
419 return {DRE->getDecl(), *RightasBool};
420 if (
auto *ME = dyn_cast<MemberExpr>(IgnImp))
421 return {ME->getMemberDecl(), *RightasBool};
428 if (ElseAssignment.
Item == ThenAssignment.Item &&
429 ElseAssignment.
Bool != ThenAssignment.Bool) {
430 if (Check->ChainedConditionalAssignment ||
431 !isa_and_nonnull<IfStmt>(
parent())) {
432 Check->replaceWithAssignment(Context, If, Var, Loc,
433 ElseAssignment.
Bool);
447 if (std::optional<bool> Then =
449 if (std::optional<bool> Else =
452 Check->replaceWithCondition(Context, Cond, *Else);
461 bool CurIf =
false, PrevIf =
false;
462 for (
auto First = CS->body_begin(), Second = std::next(First),
463 End = CS->body_end();
464 Second != End; ++Second, ++First) {
466 CurIf = isa<IfStmt>(*First);
468 if (!TrailingReturnBool)
476 auto *If = cast<IfStmt>(*First);
477 if (!If->hasInitStorage() && !If->hasVarStorage() &&
478 !If->isConsteval()) {
481 if (ThenReturnBool &&
482 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
483 if ((Check->ChainedConditionalReturn || !PrevIf) &&
484 If->getElse() ==
nullptr) {
485 Check->replaceCompoundReturnWithCondition(
486 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
487 If, ThenReturnBool.
Item);
491 }
else if (isa<LabelStmt, CaseStmt, DefaultStmt>(*First)) {
497 isa<LabelStmt>(*First) ? cast<LabelStmt>(*First)->getSubStmt()
498 : isa<CaseStmt>(*First) ? cast<CaseStmt>(*First)->getSubStmt()
499 : cast<DefaultStmt>(*First)->getSubStmt();
500 auto *SubIf = dyn_cast<IfStmt>(SubStmt);
501 if (SubIf && !SubIf->getElse() && !SubIf->hasInitStorage() &&
502 !SubIf->hasVarStorage() && !SubIf->isConsteval()) {
505 if (ThenReturnBool &&
506 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
507 Check->replaceCompoundReturnWithCondition(
508 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
509 SubIf, ThenReturnBool.
Item);
518 return !Check->canBeBypassed(E) && isa<UnaryOperator>(E) &&
519 cast<UnaryOperator>(E)->getOpcode() == UO_LNot;
523 const auto *BinaryOp = dyn_cast<BinaryOperator>(E);
524 return !Check->canBeBypassed(E) && BinaryOp && BinaryOp->isLogicalOp() &&
525 BinaryOp->getType()->isBooleanType();
528 template <
typename Functor>
530 return Func(BO->getLHS()) || Func(BO->getRHS());
534 const auto *BO = dyn_cast<BinaryOperator>(E->IgnoreUnlessSpelledInSource());
537 if (!BO->getType()->isBooleanType())
539 switch (BO->getOpcode()) {
562 if (!Check->SimplifyDeMorgan || Op->getOpcode() != UO_LNot)
563 return Base::TraverseUnaryOperator(Op);
564 const Expr *SubImp = Op->getSubExpr()->IgnoreImplicit();
565 const auto *Parens = dyn_cast<ParenExpr>(SubImp);
566 const Expr *SubExpr =
567 Parens ? Parens->getSubExpr()->IgnoreImplicit() : SubImp;
569 return Base::TraverseUnaryOperator(Op);
570 const auto *BinaryOp = cast<BinaryOperator>(SubExpr);
571 if (Check->SimplifyDeMorganRelaxed ||
576 BinaryOp, [
this](
const Expr *E) {
return nestedDemorgan(E, 1); })) {
577 if (Check->reportDeMorgan(Context, Op, BinaryOp, !IsProcessing,
parent(),
579 !Check->areDiagsSelfContained()) {
580 llvm::SaveAndRestore RAII(IsProcessing,
true);
581 return Base::TraverseUnaryOperator(Op);
584 return Base::TraverseUnaryOperator(Op);
588 bool IsProcessing =
false;
590 SmallVector<Stmt *, 32> StmtStack;
597 IgnoreMacros(Options.get(
"IgnoreMacros", false)),
598 ChainedConditionalReturn(Options.get(
"ChainedConditionalReturn", false)),
599 ChainedConditionalAssignment(
600 Options.get(
"ChainedConditionalAssignment", false)),
601 SimplifyDeMorgan(Options.get(
"SimplifyDeMorgan", true)),
602 SimplifyDeMorganRelaxed(Options.get(
"SimplifyDeMorganRelaxed", false)) {
603 if (SimplifyDeMorganRelaxed && !SimplifyDeMorgan)
604 configurationDiag(
"%0: 'SimplifyDeMorganRelaxed' cannot be enabled "
605 "without 'SimplifyDeMorgan' enabled")
612 E = E->IgnoreParenImpCasts();
613 if (isa<CXXBoolLiteralExpr>(E))
615 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
618 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E))
623void SimplifyBooleanExprCheck::reportBinOp(
const ASTContext &Context,
624 const BinaryOperator *Op) {
625 const auto *LHS = Op->getLHS()->IgnoreParenImpCasts();
626 const auto *RHS = Op->getRHS()->IgnoreParenImpCasts();
628 const CXXBoolLiteralExpr *Bool =
nullptr;
629 const Expr *Other =
nullptr;
630 if ((Bool = dyn_cast<CXXBoolLiteralExpr>(LHS)) !=
nullptr)
632 else if ((Bool = dyn_cast<CXXBoolLiteralExpr>(RHS)) !=
nullptr)
637 if (Bool->getBeginLoc().isMacroID())
644 bool BoolValue = Bool->getValue();
646 auto ReplaceWithExpression = [
this, &Context, LHS, RHS,
647 Bool](
const Expr *ReplaceWith,
bool Negated) {
648 std::string Replacement =
650 SourceRange Range(LHS->getBeginLoc(), RHS->getEndLoc());
655 switch (Op->getOpcode()) {
659 ReplaceWithExpression(Other,
false);
662 ReplaceWithExpression(Bool,
false);
667 ReplaceWithExpression(Bool,
false);
670 ReplaceWithExpression(Other,
false);
674 ReplaceWithExpression(Other, !BoolValue);
678 ReplaceWithExpression(Other, BoolValue);
686 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
687 Options.store(Opts,
"ChainedConditionalReturn", ChainedConditionalReturn);
688 Options.store(Opts,
"ChainedConditionalAssignment",
689 ChainedConditionalAssignment);
690 Options.store(Opts,
"SimplifyDeMorgan", SimplifyDeMorgan);
691 Options.store(Opts,
"SimplifyDeMorganRelaxed", SimplifyDeMorganRelaxed);
695 Finder->addMatcher(translationUnitDecl(),
this);
702bool SimplifyBooleanExprCheck::canBeBypassed(
const Stmt *S)
const {
703 return IgnoreMacros && S->getBeginLoc().isMacroID();
707bool SimplifyBooleanExprCheck::issueDiag(
const ASTContext &Context,
709 StringRef Description,
710 SourceRange ReplacementRange,
711 StringRef Replacement) {
712 CharSourceRange CharRange =
713 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(ReplacementRange),
714 Context.getSourceManager(), getLangOpts());
716 DiagnosticBuilder Diag = diag(Loc, Description);
719 Diag << FixItHint::CreateReplacement(CharRange, Replacement);
720 return HasReplacement;
723void SimplifyBooleanExprCheck::replaceWithThenStatement(
724 const ASTContext &Context,
const IfStmt *IfStatement,
725 const Expr *BoolLiteral) {
727 IfStatement->getSourceRange(),
728 getText(Context, *IfStatement->getThen()));
731void SimplifyBooleanExprCheck::replaceWithElseStatement(
732 const ASTContext &Context,
const IfStmt *IfStatement,
733 const Expr *BoolLiteral) {
734 const Stmt *ElseStatement = IfStatement->getElse();
736 IfStatement->getSourceRange(),
737 ElseStatement ? getText(Context, *ElseStatement) :
"");
740void SimplifyBooleanExprCheck::replaceWithCondition(
741 const ASTContext &Context,
const ConditionalOperator *Ternary,
743 std::string Replacement =
745 issueDiag(Context, Ternary->getTrueExpr()->getBeginLoc(),
746 "redundant boolean literal in ternary expression result",
747 Ternary->getSourceRange(), Replacement);
750void SimplifyBooleanExprCheck::replaceWithReturnCondition(
751 const ASTContext &Context,
const IfStmt *If,
const Expr *BoolLiteral,
753 StringRef Terminator = isa<CompoundStmt>(If->getElse()) ?
";" :
"";
754 std::string Condition =
756 std::string Replacement = (
"return " + Condition + Terminator).str();
757 SourceLocation Start = BoolLiteral->getBeginLoc();
759 const bool HasReplacement =
761 If->getSourceRange(), Replacement);
763 if (!HasReplacement) {
764 const SourceRange ConditionRange = If->getCond()->getSourceRange();
765 if (ConditionRange.isValid())
766 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
772void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
773 const ASTContext &Context,
const ReturnStmt *Ret,
bool Negated,
774 const IfStmt *If,
const Expr *ThenReturn) {
775 const std::string Replacement =
778 const bool HasReplacement = issueDiag(
780 SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
782 if (!HasReplacement) {
783 const SourceRange ConditionRange = If->getCond()->getSourceRange();
784 if (ConditionRange.isValid())
785 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
788 const SourceRange ReturnRange = Ret->getSourceRange();
789 if (ReturnRange.isValid())
790 diag(ReturnRange.getBegin(),
"return statement that can be simplified",
796void SimplifyBooleanExprCheck::replaceWithAssignment(
const ASTContext &Context,
797 const IfStmt *IfAssign,
801 SourceRange Range = IfAssign->getSourceRange();
802 StringRef VariableName = getText(Context, *Var);
803 StringRef Terminator = isa<CompoundStmt>(IfAssign->getElse()) ?
";" :
"";
804 std::string Condition =
806 std::string Replacement =
807 (VariableName +
" = " + Condition + Terminator).str();
808 issueDiag(Context, Loc,
"redundant boolean literal in conditional assignment",
814 const BinaryOperator *BO) {
815 assert(BO->isLogicalOp());
816 if (BO->getOperatorLoc().isMacroID())
818 Output.push_back(FixItHint::CreateReplacement(
819 BO->getOperatorLoc(), BO->getOpcode() == BO_LAnd ?
"||" :
"&&"));
824 assert(BinaryOperator::isLogicalOp(BO));
825 return BO == BO_LAnd ? BO_LOr : BO_LAnd;
829 const ASTContext &Ctx,
const Expr *E,
830 std::optional<BinaryOperatorKind> OuterBO);
837 const ASTContext &Ctx,
const BinaryOperator *BinOp,
838 std::optional<BinaryOperatorKind> OuterBO,
839 const ParenExpr *Parens =
nullptr) {
840 switch (BinOp->getOpcode()) {
854 constexpr bool LogicalOpParentheses =
true;
855 if (((*OuterBO == NewOp) || (!LogicalOpParentheses &&
856 (*OuterBO == BO_LOr && NewOp == BO_LAnd))) &&
858 if (!Parens->getLParen().isMacroID() &&
859 !Parens->getRParen().isMacroID()) {
860 Fixes.push_back(FixItHint::CreateRemoval(Parens->getLParen()));
861 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
864 if (*OuterBO == BO_LAnd && NewOp == BO_LOr && !Parens) {
865 Fixes.push_back(FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"("));
866 Fixes.push_back(FixItHint::CreateInsertion(
867 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
868 Ctx.getSourceManager(),
885 if (BinOp->getOperatorLoc().isMacroID())
887 Fixes.push_back(FixItHint::CreateReplacement(
888 BinOp->getOperatorLoc(),
889 BinaryOperator::getOpcodeStr(
890 BinaryOperator::negateComparisonOp(BinOp->getOpcode()))));
896 if (Parens->getBeginLoc().isMacroID())
898 Fixes.push_back(FixItHint::CreateInsertion(Parens->getBeginLoc(),
"!"));
900 if (BinOp->getBeginLoc().isMacroID() || BinOp->getEndLoc().isMacroID())
902 Fixes.append({FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"!("),
903 FixItHint::CreateInsertion(
904 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
905 Ctx.getSourceManager(),
915 const ASTContext &Ctx,
const Expr *E,
916 std::optional<BinaryOperatorKind> OuterBO) {
917 if (isa<UnaryOperator>(E) && cast<UnaryOperator>(E)->getOpcode() == UO_LNot) {
919 if (cast<UnaryOperator>(E)->getOperatorLoc().
isMacroID())
922 FixItHint::CreateRemoval(cast<UnaryOperator>(E)->getOperatorLoc()));
925 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
928 if (
const auto *Paren = dyn_cast<ParenExpr>(E)) {
929 if (
const auto *BinOp = dyn_cast<BinaryOperator>(Paren->getSubExpr())) {
934 if (E->getBeginLoc().isMacroID())
936 Fixes.push_back(FixItHint::CreateInsertion(E->getBeginLoc(),
"!"));
941 BinaryOperatorKind NewOuterBinary,
942 const ParenExpr *Parens) {
947 switch (Parent->getStmtClass()) {
948 case Stmt::BinaryOperatorClass: {
949 const auto *BO = cast<BinaryOperator>(Parent);
950 if (BO->isAssignmentOp())
954 if (BO->getOpcode() == NewOuterBinary)
958 case Stmt::UnaryOperatorClass:
959 case Stmt::CXXRewrittenBinaryOperatorClass:
966bool SimplifyBooleanExprCheck::reportDeMorgan(
const ASTContext &Context,
967 const UnaryOperator *Outer,
968 const BinaryOperator *Inner,
971 const ParenExpr *Parens) {
974 assert(Inner->isLogicalOp());
977 diag(Outer->getBeginLoc(),
978 "boolean expression can be simplified by DeMorgan's theorem");
979 Diag << Outer->getSourceRange();
983 if (Outer->getOperatorLoc().isMacroID())
985 SmallVector<FixItHint> Fixes;
988 Fixes.push_back(FixItHint::CreateRemoval(
989 SourceRange(Outer->getOperatorLoc(), Parens->getLParen())));
990 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
992 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 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