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();
59 for (
const auto NegatableOp :
Opposites) {
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 ImpCast && 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 MemDecl && MemDecl->isExplicit())
131 E = E->IgnoreImpCasts();
132 return !E->getType()->isBooleanType();
136 const Expr *E,
bool Negated,
137 const char *Constant) {
138 E = E->IgnoreImpCasts();
139 const std::string ExprText =
140 (isa<BinaryOperator>(E) ? (
"(" +
getText(Context, *E) +
")")
143 return ExprText +
" " + (Negated ?
"!=" :
"==") +
" " + Constant;
147 const Expr *E,
bool Negated) {
148 const char *NullPtr = Context.getLangOpts().CPlusPlus11 ?
"nullptr" :
"NULL";
153 const Expr *E,
bool Negated) {
158 bool Negated,
const Expr *E) {
159 E = E->IgnoreParenBaseCasts();
160 if (
const auto *EC = dyn_cast<ExprWithCleanups>(E))
161 E = EC->getSubExpr();
163 const bool NeedsStaticCast =
166 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E);
167 UnOp && UnOp->getOpcode() == UO_LNot) {
183 StringRef NegatedOperator;
184 const Expr *LHS =
nullptr;
185 const Expr *RHS =
nullptr;
186 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
188 LHS = BinOp->getLHS();
189 RHS = BinOp->getRHS();
190 }
else if (
const auto *OpExpr = dyn_cast<CXXOperatorCallExpr>(E);
191 OpExpr && OpExpr->getNumArgs() == 2) {
193 LHS = OpExpr->getArg(0);
194 RHS = OpExpr->getArg(1);
197 if (!NegatedOperator.empty() && LHS && RHS)
198 return (
asBool((
getText(Context, *LHS) +
" " + NegatedOperator +
" " +
203 const StringRef Text =
getText(Context, *E);
205 return (
"!(" + Text +
")").str();
213 return (
"!" +
asBool(Text, NeedsStaticCast));
216 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E);
217 UnOp && UnOp->getOpcode() == UO_LNot) {
235 CharSourceRange CharRange) {
236 std::string ReplacementText =
237 Lexer::getSourceText(CharRange, Context.getSourceManager(),
238 Context.getLangOpts())
240 Lexer Lex(CharRange.getBegin(), Context.getLangOpts(), ReplacementText.data(),
241 ReplacementText.data(),
242 ReplacementText.data() + ReplacementText.size());
243 Lex.SetCommentRetentionState(
true);
246 while (!Lex.LexFromRawLexer(Tok))
247 if (Tok.is(tok::TokenKind::comment) || Tok.is(tok::TokenKind::hash))
254 using Base = RecursiveASTVisitor<Visitor>;
258 : Check(Check), Context(Context) {}
263 switch (S->getStmtClass()) {
264 case Stmt::ImplicitCastExprClass:
265 case Stmt::MaterializeTemporaryExprClass:
266 case Stmt::CXXBindTemporaryExprClass:
276 if (Check->canBeBypassed(S))
279 StmtStack.push_back(S);
285 assert(StmtStack.back() == S);
286 StmtStack.pop_back();
292 Check->reportBinOp(Context, Op);
298 if (
const auto *Bool = dyn_cast<CXXBoolLiteralExpr>(E)) {
299 if (FilterMacro && Bool->getBeginLoc().isMacroID())
301 return Bool->getValue();
303 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E)) {
304 if (FilterMacro && UnaryOp->getBeginLoc().isMacroID())
306 if (UnaryOp->getOpcode() == UO_LNot)
308 UnaryOp->getSubExpr()->IgnoreImplicit(), FilterMacro))
318 operator bool()
const {
return Item !=
nullptr; }
326 const auto *RS = dyn_cast<ReturnStmt>(S);
327 if (!RS || !RS->getRetValue())
329 if (std::optional<bool> Ret =
331 return {RS->getRetValue(), *Ret};
339 template <
typename Functor>
341 if (
auto *CS = dyn_cast<CompoundStmt>(S)) {
343 return F(CS->body_front());
350 return StmtStack.size() < 2 ? nullptr : StmtStack[StmtStack.size() - 2];
356 if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval())
363 const Expr *Cond = If->getCond()->IgnoreImplicit();
366 Check->replaceWithThenStatement(Context, If, Cond);
368 Check->replaceWithElseStatement(Context, If, Cond);
380 if (ElseReturnBool && ThenReturnBool.
Bool != ElseReturnBool.
Bool) {
381 if (Check->ChainedConditionalReturn ||
382 !isa_and_nonnull<IfStmt>(
parent())) {
383 Check->replaceWithReturnCondition(Context, If, ThenReturnBool.Item,
384 ElseReturnBool.
Bool);
394 const auto VarBoolAssignmentMatcher =
396 const auto *BO = dyn_cast<BinaryOperator>(S);
397 if (!BO || BO->getOpcode() != BO_Assign)
399 std::optional<bool> RightasBool =
403 Expr *IgnImp = BO->getLHS()->IgnoreImplicit();
406 Loc = BO->getRHS()->getBeginLoc();
409 if (
auto *DRE = dyn_cast<DeclRefExpr>(IgnImp))
410 return {DRE->getDecl(), *RightasBool};
411 if (
const auto *ME = dyn_cast<MemberExpr>(IgnImp))
412 return {ME->getMemberDecl(), *RightasBool};
419 if (ElseAssignment.
Item == ThenAssignment.Item &&
420 ElseAssignment.
Bool != ThenAssignment.Bool &&
421 (Check->ChainedConditionalAssignment ||
422 !isa_and_nonnull<IfStmt>(
parent()))) {
423 Check->replaceWithAssignment(Context, If, Var, Loc,
424 ElseAssignment.
Bool);
437 if (std::optional<bool> Then =
439 if (std::optional<bool> Else =
442 Check->replaceWithCondition(Context, Cond, *Else);
451 bool CurIf =
false, PrevIf =
false;
452 for (
auto First = CS->body_begin(), Second = std::next(First),
453 End = CS->body_end();
454 Second != End; ++Second, ++First) {
456 CurIf = isa<IfStmt>(*First);
458 if (!TrailingReturnBool)
466 auto *If = cast<IfStmt>(*First);
467 if (!If->hasInitStorage() && !If->hasVarStorage() &&
468 !If->isConsteval()) {
471 if (ThenReturnBool &&
472 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
473 if ((Check->ChainedConditionalReturn || !PrevIf) &&
474 If->getElse() ==
nullptr) {
475 Check->replaceCompoundReturnWithCondition(
476 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
477 If, ThenReturnBool.
Item);
481 }
else if (isa<LabelStmt, CaseStmt, DefaultStmt>(*First)) {
487 isa<LabelStmt>(*First) ? cast<LabelStmt>(*First)->getSubStmt()
488 : isa<CaseStmt>(*First) ? cast<CaseStmt>(*First)->getSubStmt()
489 : cast<DefaultStmt>(*First)->getSubStmt();
490 auto *SubIf = dyn_cast<IfStmt>(SubStmt);
491 if (SubIf && !SubIf->getElse() && !SubIf->hasInitStorage() &&
492 !SubIf->hasVarStorage() && !SubIf->isConsteval()) {
495 if (ThenReturnBool &&
496 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
497 Check->replaceCompoundReturnWithCondition(
498 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
499 SubIf, ThenReturnBool.
Item);
508 return !Check->canBeBypassed(E) && isa<UnaryOperator>(E) &&
509 cast<UnaryOperator>(E)->getOpcode() == UO_LNot;
513 const auto *BinaryOp = dyn_cast<BinaryOperator>(E);
514 return !Check->canBeBypassed(E) && BinaryOp && BinaryOp->isLogicalOp() &&
515 BinaryOp->getType()->isBooleanType();
518 template <
typename Functor>
520 return Func(BO->getLHS()) || Func(BO->getRHS());
524 const auto *BO = dyn_cast<BinaryOperator>(E->IgnoreUnlessSpelledInSource());
527 if (!BO->getType()->isBooleanType())
529 switch (BO->getOpcode()) {
552 if (!Check->SimplifyDeMorgan || Op->getOpcode() != UO_LNot)
553 return Base::TraverseUnaryOperator(Op);
554 const Expr *SubImp = Op->getSubExpr()->IgnoreImplicit();
555 const auto *Parens = dyn_cast<ParenExpr>(SubImp);
556 const Expr *SubExpr =
557 Parens ? Parens->getSubExpr()->IgnoreImplicit() : SubImp;
559 return Base::TraverseUnaryOperator(Op);
560 const auto *BinaryOp = cast<BinaryOperator>(SubExpr);
561 if ((Check->SimplifyDeMorganRelaxed ||
568 Check->reportDeMorgan(Context, Op, BinaryOp, !IsProcessing,
parent(),
570 !Check->areDiagsSelfContained()) {
571 const llvm::SaveAndRestore RAII(IsProcessing,
true);
572 return Base::TraverseUnaryOperator(Op);
575 return Base::TraverseUnaryOperator(Op);
579 bool IsProcessing =
false;
588 IgnoreMacros(Options.get(
"IgnoreMacros", false)),
589 ChainedConditionalReturn(Options.get(
"ChainedConditionalReturn", false)),
590 ChainedConditionalAssignment(
591 Options.get(
"ChainedConditionalAssignment", false)),
592 SimplifyDeMorgan(Options.get(
"SimplifyDeMorgan", true)),
593 SimplifyDeMorganRelaxed(Options.get(
"SimplifyDeMorganRelaxed", false)) {
594 if (SimplifyDeMorganRelaxed && !SimplifyDeMorgan)
595 configurationDiag(
"%0: 'SimplifyDeMorganRelaxed' cannot be enabled "
596 "without 'SimplifyDeMorgan' enabled")
603 E = E->IgnoreParenImpCasts();
604 if (isa<CXXBoolLiteralExpr>(E))
606 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
609 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E))
614void SimplifyBooleanExprCheck::reportBinOp(
const ASTContext &Context,
615 const BinaryOperator *Op) {
616 const auto *LHS = Op->getLHS()->IgnoreParenImpCasts();
617 const auto *RHS = Op->getRHS()->IgnoreParenImpCasts();
619 const CXXBoolLiteralExpr *Bool =
nullptr;
620 const Expr *Other =
nullptr;
621 if ((Bool = dyn_cast<CXXBoolLiteralExpr>(LHS)) !=
nullptr)
623 else if ((Bool = dyn_cast<CXXBoolLiteralExpr>(RHS)) !=
nullptr)
628 if (Bool->getBeginLoc().isMacroID())
635 const bool BoolValue = Bool->getValue();
637 const auto ReplaceWithExpression = [
this, &Context, LHS, RHS,
638 Bool](
const Expr *ReplaceWith,
640 const std::string Replacement =
642 const SourceRange Range(LHS->getBeginLoc(), RHS->getEndLoc());
647 switch (Op->getOpcode()) {
651 ReplaceWithExpression(Other,
false);
654 ReplaceWithExpression(Bool,
false);
659 ReplaceWithExpression(Bool,
false);
662 ReplaceWithExpression(Other,
false);
666 ReplaceWithExpression(Other, !BoolValue);
670 ReplaceWithExpression(Other, BoolValue);
678 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
679 Options.store(Opts,
"ChainedConditionalReturn", ChainedConditionalReturn);
680 Options.store(Opts,
"ChainedConditionalAssignment",
681 ChainedConditionalAssignment);
682 Options.store(Opts,
"SimplifyDeMorgan", SimplifyDeMorgan);
683 Options.store(Opts,
"SimplifyDeMorganRelaxed", SimplifyDeMorganRelaxed);
687 Finder->addMatcher(translationUnitDecl(),
this);
694bool SimplifyBooleanExprCheck::canBeBypassed(
const Stmt *S)
const {
695 return IgnoreMacros && S->getBeginLoc().isMacroID();
699bool SimplifyBooleanExprCheck::issueDiag(
const ASTContext &Context,
701 StringRef Description,
702 SourceRange ReplacementRange,
703 StringRef Replacement) {
704 const CharSourceRange CharRange =
705 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(ReplacementRange),
706 Context.getSourceManager(), getLangOpts());
708 const DiagnosticBuilder Diag = diag(Loc, Description);
711 Diag << FixItHint::CreateReplacement(CharRange, Replacement);
712 return HasReplacement;
715void SimplifyBooleanExprCheck::replaceWithThenStatement(
716 const ASTContext &Context,
const IfStmt *IfStatement,
717 const Expr *BoolLiteral) {
719 IfStatement->getSourceRange(),
720 getText(Context, *IfStatement->getThen()));
723void SimplifyBooleanExprCheck::replaceWithElseStatement(
724 const ASTContext &Context,
const IfStmt *IfStatement,
725 const Expr *BoolLiteral) {
726 const Stmt *ElseStatement = IfStatement->getElse();
728 IfStatement->getSourceRange(),
729 ElseStatement ?
getText(Context, *ElseStatement) :
"");
732void SimplifyBooleanExprCheck::replaceWithCondition(
733 const ASTContext &Context,
const ConditionalOperator *Ternary,
735 const std::string Replacement =
737 issueDiag(Context, Ternary->getTrueExpr()->getBeginLoc(),
738 "redundant boolean literal in ternary expression result",
739 Ternary->getSourceRange(), Replacement);
742void SimplifyBooleanExprCheck::replaceWithReturnCondition(
743 const ASTContext &Context,
const IfStmt *If,
const Expr *BoolLiteral,
745 const StringRef Terminator = isa<CompoundStmt>(If->getElse()) ?
";" :
"";
746 const std::string Condition =
748 const std::string Replacement = (
"return " + Condition + Terminator).str();
749 const SourceLocation Start = BoolLiteral->getBeginLoc();
751 const bool HasReplacement =
753 If->getSourceRange(), Replacement);
755 if (!HasReplacement) {
756 const SourceRange ConditionRange = If->getCond()->getSourceRange();
757 if (ConditionRange.isValid())
758 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
764void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
765 const ASTContext &Context,
const ReturnStmt *Ret,
bool Negated,
766 const IfStmt *If,
const Expr *ThenReturn) {
767 const std::string Replacement =
770 const bool HasReplacement = issueDiag(
772 SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
774 if (!HasReplacement) {
775 const SourceRange ConditionRange = If->getCond()->getSourceRange();
776 if (ConditionRange.isValid())
777 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
780 const SourceRange ReturnRange = Ret->getSourceRange();
781 if (ReturnRange.isValid())
782 diag(ReturnRange.getBegin(),
"return statement that can be simplified",
788void SimplifyBooleanExprCheck::replaceWithAssignment(
const ASTContext &Context,
789 const IfStmt *IfAssign,
793 const SourceRange Range = IfAssign->getSourceRange();
794 const StringRef VariableName =
getText(Context, *Var);
795 const StringRef Terminator =
796 isa<CompoundStmt>(IfAssign->getElse()) ?
";" :
"";
797 const std::string Condition =
799 const std::string Replacement =
800 (VariableName +
" = " + Condition + Terminator).str();
801 issueDiag(Context, Loc,
"redundant boolean literal in conditional assignment",
807 const BinaryOperator *BO) {
808 assert(BO->isLogicalOp());
809 if (BO->getOperatorLoc().isMacroID())
811 Output.push_back(FixItHint::CreateReplacement(
812 BO->getOperatorLoc(), BO->getOpcode() == BO_LAnd ?
"||" :
"&&"));
817 assert(BinaryOperator::isLogicalOp(BO));
818 return BO == BO_LAnd ? BO_LOr : BO_LAnd;
822 const ASTContext &Ctx,
const Expr *E,
823 std::optional<BinaryOperatorKind> OuterBO);
830 const ASTContext &Ctx,
const BinaryOperator *BinOp,
831 std::optional<BinaryOperatorKind> OuterBO,
832 const ParenExpr *Parens =
nullptr) {
833 switch (BinOp->getOpcode()) {
847 constexpr bool LogicalOpParentheses =
true;
848 if (((*OuterBO == NewOp) || (!LogicalOpParentheses &&
849 (*OuterBO == BO_LOr && NewOp == BO_LAnd))) &&
850 Parens && !Parens->getLParen().isMacroID() &&
851 !Parens->getRParen().isMacroID()) {
852 Fixes.push_back(FixItHint::CreateRemoval(Parens->getLParen()));
853 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
856 if (*OuterBO == BO_LAnd && NewOp == BO_LOr && !Parens) {
857 Fixes.push_back(FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"("));
858 Fixes.push_back(FixItHint::CreateInsertion(
859 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
860 Ctx.getSourceManager(),
877 if (BinOp->getOperatorLoc().isMacroID())
879 Fixes.push_back(FixItHint::CreateReplacement(
880 BinOp->getOperatorLoc(),
881 BinaryOperator::getOpcodeStr(
882 BinaryOperator::negateComparisonOp(BinOp->getOpcode()))));
888 if (Parens->getBeginLoc().isMacroID())
890 Fixes.push_back(FixItHint::CreateInsertion(Parens->getBeginLoc(),
"!"));
892 if (BinOp->getBeginLoc().isMacroID() || BinOp->getEndLoc().isMacroID())
894 Fixes.append({FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"!("),
895 FixItHint::CreateInsertion(
896 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
897 Ctx.getSourceManager(),
907 const ASTContext &Ctx,
const Expr *E,
908 std::optional<BinaryOperatorKind> OuterBO) {
909 if (isa<UnaryOperator>(E) && cast<UnaryOperator>(E)->getOpcode() == UO_LNot) {
911 if (cast<UnaryOperator>(E)->getOperatorLoc().
isMacroID())
914 FixItHint::CreateRemoval(cast<UnaryOperator>(E)->getOperatorLoc()));
917 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
919 if (
const auto *Paren = dyn_cast<ParenExpr>(E)) {
920 if (
const auto *BinOp = dyn_cast<BinaryOperator>(Paren->getSubExpr()))
924 if (E->getBeginLoc().isMacroID())
926 Fixes.push_back(FixItHint::CreateInsertion(E->getBeginLoc(),
"!"));
931 BinaryOperatorKind NewOuterBinary,
932 const ParenExpr *Parens) {
937 switch (Parent->getStmtClass()) {
938 case Stmt::BinaryOperatorClass: {
939 const auto *BO = cast<BinaryOperator>(Parent);
940 if (BO->isAssignmentOp())
944 if (BO->getOpcode() == NewOuterBinary)
948 case Stmt::UnaryOperatorClass:
949 case Stmt::CXXRewrittenBinaryOperatorClass:
956bool SimplifyBooleanExprCheck::reportDeMorgan(
const ASTContext &Context,
957 const UnaryOperator *Outer,
958 const BinaryOperator *Inner,
961 const ParenExpr *Parens) {
964 assert(Inner->isLogicalOp());
967 diag(Outer->getBeginLoc(),
968 "boolean expression can be simplified by DeMorgan's theorem");
969 Diag << Outer->getSourceRange();
973 if (Outer->getOperatorLoc().isMacroID())
978 Fixes.push_back(FixItHint::CreateRemoval(
979 SourceRange(Outer->getOperatorLoc(), Parens->getLParen())));
980 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
982 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 needsNullPtrComparison(const Expr *E)
static constexpr char SimplifyConditionalReturnDiagnostic[]
static bool flipDemorganOperator(SmallVectorImpl< FixItHint > &Output, const BinaryOperator *BO)
Swaps a BinaryOperator opcode from && to || or vice-versa.
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