Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 9bf625c

Browse files
Merge pull request #412 from alexeieleusis/337_postfixExpression
invariant_boolean: When searching for references reassignment include PostfixExpression and similar, e.g. variable++.
2 parents 7bdebfd + 57ad3cc commit 9bf625c

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

lib/src/rules/invariant_booleans.dart

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,29 @@ AstNodePredicate _isConditionalStatementWithReturn(
216216
AstNodePredicate _noFurtherAssignmentInvalidatingCondition(
217217
Expression node, Iterable<AstNode> nodesInDFS) {
218218
Set<Identifier> identifiers = _findStatementIdentifiers(node.parent);
219-
return (AstNode statement) =>
220-
nodesInDFS
221-
.skipWhile((n) => n != statement)
222-
.takeWhile((n) => n != node)
223-
.where((n) =>
224-
n is AssignmentExpression &&
225-
!identifiers.contains(n.leftHandSide))
226-
.length ==
227-
0;
219+
return (AstNode statement) {
220+
bool isMutation(AstNode n) {
221+
if (n is AssignmentExpression) {
222+
return !identifiers.contains(n.leftHandSide);
223+
} else if (n is PostfixExpression) {
224+
TokenType type = n.operator.type;
225+
return (type == TokenType.PLUS_PLUS || type == TokenType.MINUS_MINUS) &&
226+
!identifiers.contains(n.operand);
227+
} else if (n is PrefixExpression) {
228+
TokenType type = n.operator.type;
229+
return (type == TokenType.PLUS_PLUS || type == TokenType.MINUS_MINUS) &&
230+
!identifiers.contains(n.operand);
231+
}
232+
233+
return false;
234+
}
235+
236+
return nodesInDFS
237+
.skipWhile((n) => n != statement)
238+
.takeWhile((n) => n != node)
239+
.where(isMutation)
240+
.isEmpty;
241+
};
228242
}
229243

230244
List<Expression> _splitConjunctions(Expression expression) {

test/rules/invariant_booleans.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,56 @@ void bug372(bool foo) {
237237
// doSomethingElse();
238238
}
239239
}
240+
241+
void bug337_1(int offset, int length) {
242+
if (offset >= length) {
243+
return;
244+
}
245+
246+
offset++;
247+
if (offset >= length) { // OK
248+
}
249+
}
250+
251+
void bug337_2(int offset, int length) {
252+
if (offset >= length) {
253+
return;
254+
}
255+
256+
offset--;
257+
if (offset >= length) { // OK
258+
}
259+
}
260+
261+
void bug337_3(int offset, int length) {
262+
if (offset >= length) {
263+
return;
264+
}
265+
266+
++offset;
267+
if (offset >= length) { // OK
268+
}
269+
}
270+
271+
void bug337_4(int offset, int length) {
272+
if (offset >= length) {
273+
return;
274+
}
275+
276+
--offset;
277+
if (offset >= length) { // OK
278+
}
279+
}
280+
281+
void test337_5() {
282+
int b = 2;
283+
if (b > 0) {
284+
b--;
285+
if (b == 0) {
286+
return;
287+
}
288+
if (b > 0) {
289+
return;
290+
}
291+
}
292+
}

0 commit comments

Comments
 (0)