This repository was archived by the owner on Nov 20, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +63
-6
lines changed Expand file tree Collapse file tree 2 files changed +63
-6
lines changed Original file line number Diff line number Diff line change 3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'package:analyzer/dart/ast/ast.dart' ;
6
+ // ignore: implementation_imports
7
+ import 'package:analyzer/src/dart/ast/ast.dart' show ExpressionImpl;
6
8
import 'package:analyzer/dart/ast/token.dart' ;
7
9
import 'package:analyzer/dart/ast/visitor.dart' ;
8
10
import 'package:analyzer/dart/element/type.dart' ;
@@ -129,6 +131,24 @@ class _Visitor extends SimpleAstVisitor<void> {
129
131
}
130
132
final binaryExpression = search as BinaryExpression ;
131
133
134
+ // Don't lint if we're in a const constructor initializer.
135
+ final constructorInitializer =
136
+ search.parent.thisOrAncestorOfType <ConstructorInitializer >();
137
+ if (constructorInitializer != null ) {
138
+ final constructorDecl =
139
+ constructorInitializer.parent as ConstructorDeclaration ;
140
+ if (constructorDecl.constKeyword != null ) {
141
+ return ;
142
+ }
143
+ }
144
+
145
+ // Or in a const context.
146
+ // See: https://github.com/dart-lang/linter/issues/1719
147
+ final impl = binaryExpression as ExpressionImpl ;
148
+ if (impl.inConstantContext) {
149
+ return ;
150
+ }
151
+
132
152
final operator = binaryExpression.operator ;
133
153
134
154
// Comparing constants with length.
Original file line number Diff line number Diff line change 4
4
5
5
// test w/ `pub run test -N prefer_is_empty`
6
6
7
+ const l = '' ;
8
+ const bool empty = l.length == 0 ; //OK
9
+
10
+ class A {
11
+ final List <String > a;
12
+ const A (this .a) : assert (a.length > 0 ); //OK
13
+ }
14
+
15
+ class B {
16
+ final String b;
17
+ const B (this .b) : assert (b.length > 0 ); //OK
18
+ }
19
+
20
+ class C {
21
+ final bool empty;
22
+ const C (dynamic l) : empty = l.length == 0 ; //OK
23
+ }
24
+
25
+ class D {
26
+ final bool emptyString;
27
+ D (String s) : emptyString = s.length == 0 ; //LINT
28
+ }
29
+
30
+ class E {
31
+ final bool empty;
32
+ const E (dynamic l) : empty = l.length == 0 ; // OK
33
+ const E .a (this .empty);
34
+ const E .b (dynamic l) : this .a (l.length == 0 ); // OK
35
+ }
36
+
37
+ class F {
38
+ // ignore: avoid_positional_boolean_parameters
39
+ const F (bool b);
40
+ }
41
+
42
+ class G extends F {
43
+ const G (dynamic l) : super (l.length == 0 ); // OK
44
+ }
45
+
7
46
const int zero = 0 ;
8
47
Iterable <int > list = [];
9
48
Map map = {};
10
49
11
50
Iterable get iterable => [];
12
51
13
- typedef Iterable F ();
52
+ typedef Fun = Iterable Function ();
14
53
15
- F a () {
16
- return () => [];
17
- }
54
+ Fun a () => () => [];
18
55
19
56
bool le = list.length > 0 ; //LINT
20
57
bool le2 = [].length > 0 ; //LINT
@@ -30,14 +67,14 @@ bool mixed = list.length + map.length > 0; //OK
30
67
Iterable length = [];
31
68
bool ok = length.first > 0 ; // OK
32
69
33
- condition () {
70
+ void condition () {
34
71
final int a = list.length > 0 ? list.first : 0 ; //LINT
35
72
list..length;
36
73
}
37
74
38
75
bool le7 = [].length > 1 ; //OK
39
76
40
- testOperators () {
77
+ void testOperators () {
41
78
[].length == 0 ; // LINT
42
79
[].length != 0 ; // LINT
43
80
[].length > 0 ; // LINT
You can’t perform that action at this time.
0 commit comments