Skip to content

Commit d5e3c03

Browse files
committed
[clang-tidy] Fix bugprone-unchecked-optional-access in RedundantBranchConditionCheck
1 parent b1eeb05 commit d5e3c03

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,18 @@ void RedundantBranchConditionCheck::check(
132132

133133
// If the other side has side effects then keep it.
134134
if (OtherSide && OtherSide->HasSideEffects(*Result.Context)) {
135-
const SourceLocation BeforeOtherSide =
136-
OtherSide->getBeginLoc().getLocWithOffset(-1);
137-
const SourceLocation AfterOtherSide =
138-
Lexer::findNextToken(OtherSide->getEndLoc(), *Result.SourceManager,
139-
getLangOpts())
140-
->getLocation();
141-
Diag << FixItHint::CreateRemoval(
142-
CharSourceRange::getTokenRange(IfBegin, BeforeOtherSide))
143-
<< FixItHint::CreateInsertion(AfterOtherSide, ";")
144-
<< FixItHint::CreateRemoval(
145-
CharSourceRange::getTokenRange(AfterOtherSide, IfEnd));
135+
auto NextToken = Lexer::findNextToken(
136+
OtherSide->getEndLoc(), *Result.SourceManager, getLangOpts());
137+
if (NextToken) {
138+
const SourceLocation BeforeOtherSide =
139+
OtherSide->getBeginLoc().getLocWithOffset(-1);
140+
const SourceLocation AfterOtherSide = NextToken->getLocation();
141+
Diag << FixItHint::CreateRemoval(
142+
CharSourceRange::getTokenRange(IfBegin, BeforeOtherSide))
143+
<< FixItHint::CreateInsertion(AfterOtherSide, ";")
144+
<< FixItHint::CreateRemoval(
145+
CharSourceRange::getTokenRange(AfterOtherSide, IfEnd));
146+
}
146147
} else {
147148
Diag << FixItHint::CreateRemoval(
148149
CharSourceRange::getTokenRange(IfBegin, IfEnd));
@@ -166,12 +167,13 @@ void RedundantBranchConditionCheck::check(
166167
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
167168
CondOp->getLHS()->getBeginLoc(), BeforeRHS));
168169
} else {
169-
const SourceLocation AfterLHS =
170-
Lexer::findNextToken(CondOp->getLHS()->getEndLoc(),
171-
*Result.SourceManager, getLangOpts())
172-
->getLocation();
173-
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
174-
AfterLHS, CondOp->getRHS()->getEndLoc()));
170+
auto NextToken = Lexer::findNextToken(
171+
CondOp->getLHS()->getEndLoc(), *Result.SourceManager, getLangOpts());
172+
if (NextToken) {
173+
const SourceLocation AfterLHS = NextToken->getLocation();
174+
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
175+
AfterLHS, CondOp->getRHS()->getEndLoc()));
176+
}
175177
}
176178
}
177179
}

0 commit comments

Comments
 (0)