101 const MatchFinder::MatchResult &Result) {
102 const auto *OuterIf = Result.Nodes.getNodeAs<IfStmt>(
OuterIfStr);
103 const auto *InnerIf = Result.Nodes.getNodeAs<IfStmt>(
InnerIfStr);
104 const auto *CondVar = Result.Nodes.getNodeAs<VarDecl>(
CondVarStr);
105 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(
FuncStr);
107 const DeclRefExpr *OuterIfVar =
nullptr, *InnerIfVar =
nullptr;
108 if (
const auto *Inner = Result.Nodes.getNodeAs<DeclRefExpr>(
InnerIfVar1Str))
112 if (
const auto *Outer = Result.Nodes.getNodeAs<DeclRefExpr>(
OuterIfVar1Str))
117 if (OuterIfVar && InnerIfVar) {
118 if (
isChangedBefore(OuterIf->getThen(), InnerIfVar, OuterIfVar, CondVar,
122 if (
isChangedBefore(OuterIf->getCond(), InnerIfVar, OuterIfVar, CondVar,
131 ExprMutationAnalyzer(*Loop, *Result.Context).findMutation(CondVar))
140 const auto Diag = diag(InnerIf->getBeginLoc(),
"redundant condition %0")
145 const auto *BinOpCond =
146 dyn_cast<BinaryOperator>(InnerIf->getCond()->IgnoreParenImpCasts());
148 if (isa<DeclRefExpr>(InnerIf->getCond()->IgnoreParenImpCasts()) ||
149 (BinOpCond && BinOpCond->getOpcode() == BO_LOr)) {
150 const SourceLocation IfBegin = InnerIf->getBeginLoc();
151 const Stmt *Body = InnerIf->getThen();
152 const Expr *OtherSide =
nullptr;
154 const auto *LeftDRE =
155 dyn_cast<DeclRefExpr>(BinOpCond->getLHS()->IgnoreParenImpCasts());
156 if (LeftDRE && LeftDRE->getDecl() == CondVar)
157 OtherSide = BinOpCond->getRHS();
159 OtherSide = BinOpCond->getLHS();
162 SourceLocation IfEnd = Body->getBeginLoc().getLocWithOffset(-1);
165 if (isa<CompoundStmt>(Body))
166 IfEnd = Body->getBeginLoc();
169 if (OtherSide && OtherSide->HasSideEffects(*Result.Context)) {
170 const SourceLocation BeforeOtherSide =
171 OtherSide->getBeginLoc().getLocWithOffset(-1);
173 OtherSide->getEndLoc(), *Result.SourceManager, getLangOpts())) {
174 const SourceLocation AfterOtherSide = NextToken->getLocation();
175 Diag << FixItHint::CreateRemoval(
176 CharSourceRange::getTokenRange(IfBegin, BeforeOtherSide))
177 << FixItHint::CreateInsertion(AfterOtherSide,
";")
178 << FixItHint::CreateRemoval(
179 CharSourceRange::getTokenRange(AfterOtherSide, IfEnd));
182 Diag << FixItHint::CreateRemoval(
183 CharSourceRange::getTokenRange(IfBegin, IfEnd));
187 if (isa<CompoundStmt>(Body))
188 Diag << FixItHint::CreateRemoval(
189 CharSourceRange::getTokenRange(Body->getEndLoc(), Body->getEndLoc()));
195 cast<BinaryOperator>(InnerIf->getCond()->IgnoreParenImpCasts());
196 const auto *LeftDRE =
197 dyn_cast<DeclRefExpr>(CondOp->getLHS()->IgnoreParenImpCasts());
198 if (LeftDRE && LeftDRE->getDecl() == CondVar) {
199 const SourceLocation BeforeRHS =
200 CondOp->getRHS()->getBeginLoc().getLocWithOffset(-1);
201 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
202 CondOp->getLHS()->getBeginLoc(), BeforeRHS));
203 }
else if (
const auto NextToken =
205 CondOp->getLHS()->getEndLoc(), *Result.SourceManager,
207 const SourceLocation AfterLHS = NextToken->getLocation();
208 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
209 AfterLHS, CondOp->getRHS()->getEndLoc()));