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();
165 const bool NeedsStaticCast =
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 const 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))
258 using Base = RecursiveASTVisitor<Visitor>;
262 : Check(Check), Context(Context) {}
267 switch (S->getStmtClass()) {
268 case Stmt::ImplicitCastExprClass:
269 case Stmt::MaterializeTemporaryExprClass:
270 case Stmt::CXXBindTemporaryExprClass:
280 if (Check->canBeBypassed(S))
283 StmtStack.push_back(S);
289 assert(StmtStack.back() == S);
290 StmtStack.pop_back();
296 Check->reportBinOp(Context, Op);
302 if (
const auto *Bool = dyn_cast<CXXBoolLiteralExpr>(E)) {
303 if (FilterMacro && Bool->getBeginLoc().isMacroID())
305 return Bool->getValue();
307 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E)) {
308 if (FilterMacro && UnaryOp->getBeginLoc().isMacroID())
310 if (UnaryOp->getOpcode() == UO_LNot)
312 UnaryOp->getSubExpr()->IgnoreImplicit(), FilterMacro))
322 operator bool()
const {
return Item !=
nullptr; }
330 const auto *RS = dyn_cast<ReturnStmt>(S);
331 if (!RS || !RS->getRetValue())
333 if (std::optional<bool> Ret =
335 return {RS->getRetValue(), *Ret};
343 template <
typename Functor>
345 if (
auto *CS = dyn_cast<CompoundStmt>(S)) {
347 return F(CS->body_front());
354 return StmtStack.size() < 2 ? nullptr : StmtStack[StmtStack.size() - 2];
360 if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval())
367 const Expr *Cond = If->getCond()->IgnoreImplicit();
370 Check->replaceWithThenStatement(Context, If, Cond);
372 Check->replaceWithElseStatement(Context, If, Cond);
384 if (ElseReturnBool && ThenReturnBool.
Bool != ElseReturnBool.
Bool) {
385 if (Check->ChainedConditionalReturn ||
386 !isa_and_nonnull<IfStmt>(
parent())) {
387 Check->replaceWithReturnCondition(Context, If, ThenReturnBool.Item,
388 ElseReturnBool.
Bool);
398 auto VarBoolAssignmentMatcher = [&Var,
400 const auto *BO = dyn_cast<BinaryOperator>(S);
401 if (!BO || BO->getOpcode() != BO_Assign)
403 std::optional<bool> RightasBool =
407 Expr *IgnImp = BO->getLHS()->IgnoreImplicit();
410 Loc = BO->getRHS()->getBeginLoc();
413 if (
auto *DRE = dyn_cast<DeclRefExpr>(IgnImp))
414 return {DRE->getDecl(), *RightasBool};
415 if (
auto *ME = dyn_cast<MemberExpr>(IgnImp))
416 return {ME->getMemberDecl(), *RightasBool};
423 if (ElseAssignment.
Item == ThenAssignment.Item &&
424 ElseAssignment.
Bool != ThenAssignment.Bool) {
425 if (Check->ChainedConditionalAssignment ||
426 !isa_and_nonnull<IfStmt>(
parent())) {
427 Check->replaceWithAssignment(Context, If, Var, Loc,
428 ElseAssignment.
Bool);
442 if (std::optional<bool> Then =
444 if (std::optional<bool> Else =
447 Check->replaceWithCondition(Context, Cond, *Else);
456 bool CurIf =
false, PrevIf =
false;
457 for (
auto First = CS->body_begin(), Second = std::next(First),
458 End = CS->body_end();
459 Second != End; ++Second, ++First) {
461 CurIf = isa<IfStmt>(*First);
463 if (!TrailingReturnBool)
471 auto *If = cast<IfStmt>(*First);
472 if (!If->hasInitStorage() && !If->hasVarStorage() &&
473 !If->isConsteval()) {
476 if (ThenReturnBool &&
477 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
478 if ((Check->ChainedConditionalReturn || !PrevIf) &&
479 If->getElse() ==
nullptr) {
480 Check->replaceCompoundReturnWithCondition(
481 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
482 If, ThenReturnBool.
Item);
486 }
else if (isa<LabelStmt, CaseStmt, DefaultStmt>(*First)) {
492 isa<LabelStmt>(*First) ? cast<LabelStmt>(*First)->getSubStmt()
493 : isa<CaseStmt>(*First) ? cast<CaseStmt>(*First)->getSubStmt()
494 : cast<DefaultStmt>(*First)->getSubStmt();
495 auto *SubIf = dyn_cast<IfStmt>(SubStmt);
496 if (SubIf && !SubIf->getElse() && !SubIf->hasInitStorage() &&
497 !SubIf->hasVarStorage() && !SubIf->isConsteval()) {
500 if (ThenReturnBool &&
501 ThenReturnBool.
Bool != TrailingReturnBool.
Bool) {
502 Check->replaceCompoundReturnWithCondition(
503 Context, cast<ReturnStmt>(*Second), TrailingReturnBool.
Bool,
504 SubIf, ThenReturnBool.
Item);
513 return !Check->canBeBypassed(E) && isa<UnaryOperator>(E) &&
514 cast<UnaryOperator>(E)->getOpcode() == UO_LNot;
518 const auto *BinaryOp = dyn_cast<BinaryOperator>(E);
519 return !Check->canBeBypassed(E) && BinaryOp && BinaryOp->isLogicalOp() &&
520 BinaryOp->getType()->isBooleanType();
523 template <
typename Functor>
525 return Func(BO->getLHS()) || Func(BO->getRHS());
529 const auto *BO = dyn_cast<BinaryOperator>(E->IgnoreUnlessSpelledInSource());
532 if (!BO->getType()->isBooleanType())
534 switch (BO->getOpcode()) {
557 if (!Check->SimplifyDeMorgan || Op->getOpcode() != UO_LNot)
558 return Base::TraverseUnaryOperator(Op);
559 const Expr *SubImp = Op->getSubExpr()->IgnoreImplicit();
560 const auto *Parens = dyn_cast<ParenExpr>(SubImp);
561 const Expr *SubExpr =
562 Parens ? Parens->getSubExpr()->IgnoreImplicit() : SubImp;
564 return Base::TraverseUnaryOperator(Op);
565 const auto *BinaryOp = cast<BinaryOperator>(SubExpr);
566 if (Check->SimplifyDeMorganRelaxed ||
571 BinaryOp, [
this](
const Expr *E) {
return nestedDemorgan(E, 1); })) {
572 if (Check->reportDeMorgan(Context, Op, BinaryOp, !IsProcessing,
parent(),
574 !Check->areDiagsSelfContained()) {
575 const llvm::SaveAndRestore RAII(IsProcessing,
true);
576 return Base::TraverseUnaryOperator(Op);
579 return Base::TraverseUnaryOperator(Op);
583 bool IsProcessing =
false;
592 IgnoreMacros(Options.get(
"IgnoreMacros", false)),
593 ChainedConditionalReturn(Options.get(
"ChainedConditionalReturn", false)),
594 ChainedConditionalAssignment(
595 Options.get(
"ChainedConditionalAssignment", false)),
596 SimplifyDeMorgan(Options.get(
"SimplifyDeMorgan", true)),
597 SimplifyDeMorganRelaxed(Options.get(
"SimplifyDeMorganRelaxed", false)) {
598 if (SimplifyDeMorganRelaxed && !SimplifyDeMorgan)
599 configurationDiag(
"%0: 'SimplifyDeMorganRelaxed' cannot be enabled "
600 "without 'SimplifyDeMorgan' enabled")
607 E = E->IgnoreParenImpCasts();
608 if (isa<CXXBoolLiteralExpr>(E))
610 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
613 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E))
618void SimplifyBooleanExprCheck::reportBinOp(
const ASTContext &Context,
619 const BinaryOperator *Op) {
620 const auto *LHS = Op->getLHS()->IgnoreParenImpCasts();
621 const auto *RHS = Op->getRHS()->IgnoreParenImpCasts();
623 const CXXBoolLiteralExpr *Bool =
nullptr;
624 const Expr *Other =
nullptr;
625 if ((Bool = dyn_cast<CXXBoolLiteralExpr>(LHS)) !=
nullptr)
627 else if ((Bool = dyn_cast<CXXBoolLiteralExpr>(RHS)) !=
nullptr)
632 if (Bool->getBeginLoc().isMacroID())
639 const bool BoolValue = Bool->getValue();
641 auto ReplaceWithExpression = [
this, &Context, LHS, RHS,
642 Bool](
const Expr *ReplaceWith,
bool Negated) {
643 const std::string Replacement =
645 const SourceRange Range(LHS->getBeginLoc(), RHS->getEndLoc());
650 switch (Op->getOpcode()) {
654 ReplaceWithExpression(Other,
false);
657 ReplaceWithExpression(Bool,
false);
662 ReplaceWithExpression(Bool,
false);
665 ReplaceWithExpression(Other,
false);
669 ReplaceWithExpression(Other, !BoolValue);
673 ReplaceWithExpression(Other, BoolValue);
681 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
682 Options.store(Opts,
"ChainedConditionalReturn", ChainedConditionalReturn);
683 Options.store(Opts,
"ChainedConditionalAssignment",
684 ChainedConditionalAssignment);
685 Options.store(Opts,
"SimplifyDeMorgan", SimplifyDeMorgan);
686 Options.store(Opts,
"SimplifyDeMorganRelaxed", SimplifyDeMorganRelaxed);
690 Finder->addMatcher(translationUnitDecl(),
this);
697bool SimplifyBooleanExprCheck::canBeBypassed(
const Stmt *S)
const {
698 return IgnoreMacros && S->getBeginLoc().isMacroID();
702bool SimplifyBooleanExprCheck::issueDiag(
const ASTContext &Context,
704 StringRef Description,
705 SourceRange ReplacementRange,
706 StringRef Replacement) {
707 const CharSourceRange CharRange =
708 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(ReplacementRange),
709 Context.getSourceManager(), getLangOpts());
711 const DiagnosticBuilder Diag = diag(Loc, Description);
714 Diag << FixItHint::CreateReplacement(CharRange, Replacement);
715 return HasReplacement;
718void SimplifyBooleanExprCheck::replaceWithThenStatement(
719 const ASTContext &Context,
const IfStmt *IfStatement,
720 const Expr *BoolLiteral) {
722 IfStatement->getSourceRange(),
723 getText(Context, *IfStatement->getThen()));
726void SimplifyBooleanExprCheck::replaceWithElseStatement(
727 const ASTContext &Context,
const IfStmt *IfStatement,
728 const Expr *BoolLiteral) {
729 const Stmt *ElseStatement = IfStatement->getElse();
731 IfStatement->getSourceRange(),
732 ElseStatement ?
getText(Context, *ElseStatement) :
"");
735void SimplifyBooleanExprCheck::replaceWithCondition(
736 const ASTContext &Context,
const ConditionalOperator *Ternary,
738 const std::string Replacement =
740 issueDiag(Context, Ternary->getTrueExpr()->getBeginLoc(),
741 "redundant boolean literal in ternary expression result",
742 Ternary->getSourceRange(), Replacement);
745void SimplifyBooleanExprCheck::replaceWithReturnCondition(
746 const ASTContext &Context,
const IfStmt *If,
const Expr *BoolLiteral,
748 const StringRef Terminator = isa<CompoundStmt>(If->getElse()) ?
";" :
"";
749 const std::string Condition =
751 const std::string Replacement = (
"return " + Condition + Terminator).str();
752 const SourceLocation Start = BoolLiteral->getBeginLoc();
754 const bool HasReplacement =
756 If->getSourceRange(), Replacement);
758 if (!HasReplacement) {
759 const SourceRange ConditionRange = If->getCond()->getSourceRange();
760 if (ConditionRange.isValid())
761 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
767void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
768 const ASTContext &Context,
const ReturnStmt *Ret,
bool Negated,
769 const IfStmt *If,
const Expr *ThenReturn) {
770 const std::string Replacement =
773 const bool HasReplacement = issueDiag(
775 SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
777 if (!HasReplacement) {
778 const SourceRange ConditionRange = If->getCond()->getSourceRange();
779 if (ConditionRange.isValid())
780 diag(ConditionRange.getBegin(),
"conditions that can be simplified",
783 const SourceRange ReturnRange = Ret->getSourceRange();
784 if (ReturnRange.isValid())
785 diag(ReturnRange.getBegin(),
"return statement that can be simplified",
791void SimplifyBooleanExprCheck::replaceWithAssignment(
const ASTContext &Context,
792 const IfStmt *IfAssign,
796 const SourceRange Range = IfAssign->getSourceRange();
797 const StringRef VariableName =
getText(Context, *Var);
798 const StringRef Terminator =
799 isa<CompoundStmt>(IfAssign->getElse()) ?
";" :
"";
800 const std::string Condition =
802 const std::string Replacement =
803 (VariableName +
" = " + Condition + Terminator).str();
804 issueDiag(Context, Loc,
"redundant boolean literal in conditional assignment",
810 const BinaryOperator *BO) {
811 assert(BO->isLogicalOp());
812 if (BO->getOperatorLoc().isMacroID())
814 Output.push_back(FixItHint::CreateReplacement(
815 BO->getOperatorLoc(), BO->getOpcode() == BO_LAnd ?
"||" :
"&&"));
820 assert(BinaryOperator::isLogicalOp(BO));
821 return BO == BO_LAnd ? BO_LOr : BO_LAnd;
825 const ASTContext &Ctx,
const Expr *E,
826 std::optional<BinaryOperatorKind> OuterBO);
833 const ASTContext &Ctx,
const BinaryOperator *BinOp,
834 std::optional<BinaryOperatorKind> OuterBO,
835 const ParenExpr *Parens =
nullptr) {
836 switch (BinOp->getOpcode()) {
850 constexpr bool LogicalOpParentheses =
true;
851 if (((*OuterBO == NewOp) || (!LogicalOpParentheses &&
852 (*OuterBO == BO_LOr && NewOp == BO_LAnd))) &&
854 if (!Parens->getLParen().isMacroID() &&
855 !Parens->getRParen().isMacroID()) {
856 Fixes.push_back(FixItHint::CreateRemoval(Parens->getLParen()));
857 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
860 if (*OuterBO == BO_LAnd && NewOp == BO_LOr && !Parens) {
861 Fixes.push_back(FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"("));
862 Fixes.push_back(FixItHint::CreateInsertion(
863 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
864 Ctx.getSourceManager(),
881 if (BinOp->getOperatorLoc().isMacroID())
883 Fixes.push_back(FixItHint::CreateReplacement(
884 BinOp->getOperatorLoc(),
885 BinaryOperator::getOpcodeStr(
886 BinaryOperator::negateComparisonOp(BinOp->getOpcode()))));
892 if (Parens->getBeginLoc().isMacroID())
894 Fixes.push_back(FixItHint::CreateInsertion(Parens->getBeginLoc(),
"!"));
896 if (BinOp->getBeginLoc().isMacroID() || BinOp->getEndLoc().isMacroID())
898 Fixes.append({FixItHint::CreateInsertion(BinOp->getBeginLoc(),
"!("),
899 FixItHint::CreateInsertion(
900 Lexer::getLocForEndOfToken(BinOp->getEndLoc(), 0,
901 Ctx.getSourceManager(),
911 const ASTContext &Ctx,
const Expr *E,
912 std::optional<BinaryOperatorKind> OuterBO) {
913 if (isa<UnaryOperator>(E) && cast<UnaryOperator>(E)->getOpcode() == UO_LNot) {
915 if (cast<UnaryOperator>(E)->getOperatorLoc().
isMacroID())
918 FixItHint::CreateRemoval(cast<UnaryOperator>(E)->getOperatorLoc()));
921 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
923 if (
const auto *Paren = dyn_cast<ParenExpr>(E)) {
924 if (
const auto *BinOp = dyn_cast<BinaryOperator>(Paren->getSubExpr()))
928 if (E->getBeginLoc().isMacroID())
930 Fixes.push_back(FixItHint::CreateInsertion(E->getBeginLoc(),
"!"));
935 BinaryOperatorKind NewOuterBinary,
936 const ParenExpr *Parens) {
941 switch (Parent->getStmtClass()) {
942 case Stmt::BinaryOperatorClass: {
943 const auto *BO = cast<BinaryOperator>(Parent);
944 if (BO->isAssignmentOp())
948 if (BO->getOpcode() == NewOuterBinary)
952 case Stmt::UnaryOperatorClass:
953 case Stmt::CXXRewrittenBinaryOperatorClass:
960bool SimplifyBooleanExprCheck::reportDeMorgan(
const ASTContext &Context,
961 const UnaryOperator *Outer,
962 const BinaryOperator *Inner,
965 const ParenExpr *Parens) {
968 assert(Inner->isLogicalOp());
971 diag(Outer->getBeginLoc(),
972 "boolean expression can be simplified by DeMorgan's theorem");
973 Diag << Outer->getSourceRange();
977 if (Outer->getOperatorLoc().isMacroID())
982 Fixes.push_back(FixItHint::CreateRemoval(
983 SourceRange(Outer->getOperatorLoc(), Parens->getLParen())));
984 Fixes.push_back(FixItHint::CreateRemoval(Parens->getRParen()));
986 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