/
invariant_booleans.dart
343 lines (298 loc) · 10.3 KB
/
invariant_booleans.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library linter.src.rules.invariant_booleans;
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:linter/src/analyzer.dart';
import 'package:linter/src/util/boolean_expression_utilities.dart';
import 'package:linter/src/util/dart_type_utilities.dart';
import 'package:linter/src/util/tested_expressions.dart';
const _desc =
r'Conditions should not unconditionally evaluate to "TRUE" or to "FALSE"';
const _details = r'''
**DON'T** test for conditions that can be inferred at compile time or test the
same condition twice.
Conditional statements using a condition which cannot be anything but FALSE have
the effect of making blocks of code non-functional. If the condition cannot
evaluate to anything but TRUE, the conditional statement is completely
redundant, and makes the code less readable.
It is quite likely that the code does not match the programmer's intent.
Either the condition should be removed or it should be updated so that it does
not always evaluate to TRUE or FALSE and does not perform redundant tests.
This rule will hint to the test conflicting with the linted one.
**BAD:**
```
//foo can't be both equal and not equal to bar in the same expression
if(foo == bar && something && foo != bar) {...}
```
**BAD:**
```
void compute(int foo) {
if (foo == 4) {
doSomething();
// We know foo is equal to 4 at this point, so the next condition is always false
if (foo > 4) {...}
...
}
...
}
```
**BAD:**
```
void compute(bool foo) {
if (foo) {
return;
}
doSomething();
// foo is always false here
if (foo){...}
...
}
```
**GOOD:**
```
void nestedOK() {
if (foo == bar) {
foo = baz;
if (foo != bar) {...}
}
}
```
**GOOD:**
```
void nestedOk2() {
if (foo == bar) {
return;
}
foo = baz;
if (foo == bar) {...} // OK
}
```
**GOOD:**
```
void nestedOk5() {
if (foo != null) {
if (bar != null) {
return;
}
}
if (bar != null) {...} // OK
}
```
_**WARNING:** this lint is comparatively expensive as, in general, calculating
[boolean satisfiability](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem) is hard.
Performance improvements are planned ([#434](https://github.com/dart-lang/linter/issues/434)) but
in the meantime, this lint should be sparingly enabled in large projects or when lint performance
is a concern._
''';
Set<Expression> _findConditionsCausingReturns(
Expression node, Iterable<AstNode> nodesInDFS) =>
nodesInDFS
.where(_isAnalyzedNode)
.where(_isConditionalStatementWithReturn(nodesInDFS))
.takeWhile((n) => n != node.parent)
.where((_noFurtherAssignmentInvalidatingCondition(node, nodesInDFS)))
.fold(<Expression>[], (List<Expression> previous, AstNode statement) {
previous.add(_getCondition(statement));
return previous;
}).toSet();
Set<Expression> _findConditionsOfElseStatementAncestor(
Statement statement, Iterable<AstNode> nodesInDFS) {
return _findConditionsUnderStatementBranch(
statement,
(n) =>
n is IfStatement &&
statement.getAncestor((a) => a == n.elseStatement) != null,
nodesInDFS);
}
Set<Expression> _findConditionsOfStatementAncestor(
Statement statement, Iterable<AstNode> nodesInDFS) {
return _findConditionsUnderStatementBranch(
statement,
(n) =>
_isAnalyzedNode(n) &&
(n is! IfStatement ||
statement.getAncestor(
(a) => a == (n as IfStatement).thenStatement) !=
null),
nodesInDFS);
}
Set<Expression> _findConditionsUnderStatementBranch(Statement statement,
AstNodePredicate predicate, Iterable<AstNode> nodesInDFS) {
Expression condition = _getCondition(statement);
AstNodePredicate noFurtherAssignmentInvalidatingCondition =
_noFurtherAssignmentInvalidatingCondition(condition, nodesInDFS);
return nodesInDFS
.where((n) => n == statement.getAncestor((a) => a == n && a != statement))
.where(_isAnalyzedNode)
.where(predicate)
.where(noFurtherAssignmentInvalidatingCondition)
.fold(<Expression>[], (List<Expression> previous, AstNode statement) {
previous.add(_getCondition(statement));
return previous;
}).toSet();
}
TestedExpressions _findPreviousTestedExpressions(Expression node) {
Block block = node.getAncestor((a) => a is Block && a.parent is FunctionBody);
Iterable<AstNode> nodesInDFS = DartTypeUtilities.traverseNodesInDFS(block,
excludeCriteria: (n) => n is FunctionDeclarationStatement);
Iterable<Expression> conjunctions =
_findConditionsOfStatementAncestor(node.parent, nodesInDFS)
.map(_splitConjunctions)
.expand((iterable) => iterable)
.toSet();
Iterable<Expression> negations = (_findConditionsCausingReturns(
node, nodesInDFS)
..addAll(
_findConditionsOfElseStatementAncestor(node.parent, nodesInDFS)))
.toSet();
return new TestedExpressions(node, conjunctions, negations);
}
Set<Identifier> _findStatementIdentifiers(Statement statement) =>
DartTypeUtilities
.traverseNodesInDFS(statement)
.where((n) => n is Identifier)
.toSet();
Expression _getCondition(Statement statement) {
if (statement is IfStatement) {
return statement.condition;
}
if (statement is DoStatement) {
return statement.condition;
}
if (statement is ForStatement) {
return statement.condition;
}
if (statement is WhileStatement) {
return statement.condition;
}
return null;
}
bool _isAnalyzedNode(AstNode node) =>
node is IfStatement ||
node is DoStatement ||
node is ForStatement ||
node is WhileStatement;
AstNodePredicate _isConditionalStatementWithReturn(
Iterable<AstNode> blockNodes) =>
(AstNode node) {
Block block =
node.getAncestor((a) => a is Block && a.parent is BlockFunctionBody);
Iterable<AstNode> nodesInDFS = DartTypeUtilities.traverseNodesInDFS(node);
return nodesInDFS.any((n) => n is ReturnStatement) &&
// Ignore nested if statements.
!nodesInDFS.any(_isAnalyzedNode) &&
node.getAncestor((n) =>
n != node &&
_isAnalyzedNode(n) &&
n.getAncestor((a) => a == block) == block) ==
null;
};
AstNodePredicate _noFurtherAssignmentInvalidatingCondition(
Expression node, Iterable<AstNode> nodesInDFS) {
Set<Identifier> identifiers = _findStatementIdentifiers(node.parent);
return (AstNode statement) {
bool isMutation(AstNode n) {
if (n is AssignmentExpression) {
return !identifiers.contains(n.leftHandSide);
} else if (n is PostfixExpression) {
TokenType type = n.operator.type;
return (type == TokenType.PLUS_PLUS || type == TokenType.MINUS_MINUS) &&
!identifiers.contains(n.operand);
} else if (n is PrefixExpression) {
TokenType type = n.operator.type;
return (type == TokenType.PLUS_PLUS || type == TokenType.MINUS_MINUS) &&
!identifiers.contains(n.operand);
}
return false;
}
return nodesInDFS
.skipWhile((n) => n != statement)
.takeWhile((n) => n != node)
.where(isMutation)
.isEmpty;
};
}
List<Expression> _splitConjunctions(Expression expression) {
if (expression is BinaryExpression &&
expression.operator.type == TokenType.AMPERSAND_AMPERSAND) {
return _splitConjunctions(expression.leftOperand)
..addAll(_splitConjunctions(expression.rightOperand));
}
return [expression];
}
class InvariantBooleans extends LintRule {
_Visitor _visitor;
InvariantBooleans()
: super(
name: 'invariant_booleans',
description: _desc,
details: _details,
group: Group.errors,
maturity: Maturity.stable) {
_visitor = new _Visitor(this);
}
@override
AstVisitor getVisitor() => _visitor;
}
/// The only purpose of this rule is to report the second node on a contradictory
/// comparison indicating the first node as the cause of the inconsistency.
class _ContradictionReportRule extends LintRule {
_ContradictionReportRule(ContradictoryComparisons comparisons)
: super(
name: 'invariant_booleans',
description: _desc + ' verify: ${comparisons.first}.',
details: _details,
group: Group.errors,
maturity: Maturity.stable);
}
class _Visitor extends SimpleAstVisitor {
final LintRule rule;
_Visitor(this.rule);
@override
visitDoStatement(DoStatement node) {
_reportExpressionIfConstantValue(node.condition);
}
@override
visitForStatement(ForStatement node) {
_reportExpressionIfConstantValue(node.condition);
}
@override
visitIfStatement(IfStatement node) {
_reportExpressionIfConstantValue(node.condition);
}
@override
visitWhileStatement(WhileStatement node) {
_reportExpressionIfConstantValue(node.condition);
}
_reportExpressionIfConstantValue(Expression node) {
// Right part discards reporting a subexpression already reported.
if (node == null ||
resolutionMap.bestTypeForExpression(node).name != 'bool' ||
!_isAnalyzedNode(node.parent)) {
return;
}
TestedExpressions testedNodes = _findPreviousTestedExpressions(node);
testedNodes.evaluateInvariant().forEach((ContradictoryComparisons e) {
final reportRule = new _ContradictionReportRule(e);
reportRule
..reporter = rule.reporter
..reportLint(e.second);
});
// In dart booleanVariable == true is a valid comparison since the variable
// can be null.
final BinaryExpression binaryExpression =
node is BinaryExpression ? node : null;
if (binaryExpression != null &&
!BooleanExpressionUtilities.EQUALITY_OPERATIONS
.contains(binaryExpression.operator.type) &&
(binaryExpression.leftOperand is BooleanLiteral ||
binaryExpression.rightOperand is BooleanLiteral) &&
binaryExpression.operator.type != TokenType.QUESTION_QUESTION) {
rule.reportLint(node);
}
}
}