Skip to content

Commit 25e29e4

Browse files
committed
Refine TLoopCounterMutatedInLoopBody
This refined definition can handle more cases than the previous one that only looked into the loop body, and better matches the description in the comment above.
1 parent d37bc70 commit 25e29e4

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

cpp/misra/src/rules/RULE-9-5-1/LegacyForStatementsShouldBeSimple.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ private newtype TAlertType =
170170
/* 3-1. The loop counter is mutated somewhere other than its update expression. */
171171
TLoopCounterMutatedInLoopBody(ForStmt forLoop, Variable loopCounterVariable) {
172172
loopCounterVariable = getDeclaredVariableInForLoop(forLoop) and
173-
variableModifiedInExpression(forLoop.getStmt().getChildStmt().getAChild*(),
174-
loopCounterVariable.getAnAccess())
173+
exists(Expr mutatingExpr | not mutatingExpr = forLoop.getUpdate().getAChild*() |
174+
variableModifiedInExpression(mutatingExpr, loopCounterVariable.getAnAccess())
175+
)
175176
} or
176177
/* 3-2. The loop counter is not updated using either of `++`, `--`, `+=`, or `-=`. */
177178
TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr(

0 commit comments

Comments
 (0)