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

special case assert initializers and const contexts in prefer_is_empty #2122

Merged
merged 5 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/src/rules/prefer_is_empty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/ast/ast.dart' show ExpressionImpl;
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/type.dart';
Expand Down Expand Up @@ -129,6 +131,24 @@ class _Visitor extends SimpleAstVisitor<void> {
}
final binaryExpression = search as BinaryExpression;

// Don't lint if we're in a const constructor initializer.
final constructorInitializer =
search.parent.thisOrAncestorOfType<ConstructorInitializer>();
if (constructorInitializer != null) {
final constructorDecl =
constructorInitializer.parent as ConstructorDeclaration;
if (constructorDecl.constKeyword != null) {
return;
}
}

// Or in a const context.
// See: https://github.com/dart-lang/linter/issues/1719
final impl = binaryExpression as ExpressionImpl;
if (impl.inConstantContext) {
return;
}

final operator = binaryExpression.operator;

// Comparing constants with length.
Expand Down
49 changes: 43 additions & 6 deletions test/rules/prefer_is_empty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,54 @@

// test w/ `pub run test -N prefer_is_empty`

const l = '';
const bool empty = l.length == 0; //OK

class A {
final List<String> a;
const A(this.a) : assert(a.length > 0); //OK
}

class B {
final String b;
const B(this.b) : assert(b.length > 0); //OK
}

class C {
final bool empty;
const C(dynamic l) : empty = l.length == 0; //OK
}

class D {
final bool emptyString;
D(String s) : emptyString = s.length == 0; //LINT
}

class E {
final bool empty;
const E(dynamic l) : empty = l.length == 0; // OK
const E.a(this.empty);
const E.b(dynamic l) : this.a(l.length == 0); // OK
}

class F {
// ignore: avoid_positional_boolean_parameters
const F(bool b);
}

class G extends F {
const G(dynamic l) : super(l.length == 0); // OK
}

const int zero = 0;
Iterable<int> list = [];
Map map = {};

Iterable get iterable => [];

typedef Iterable F();
typedef Fun = Iterable Function();

F a() {
return () => [];
}
Fun a() => () => [];

bool le = list.length > 0; //LINT
bool le2 = [].length > 0; //LINT
Expand All @@ -30,14 +67,14 @@ bool mixed = list.length + map.length > 0; //OK
Iterable length = [];
bool ok = length.first > 0; // OK

condition() {
void condition() {
final int a = list.length > 0 ? list.first : 0; //LINT
list..length;
}

bool le7 = [].length > 1; //OK

testOperators() {
void testOperators() {
[].length == 0; // LINT
[].length != 0; // LINT
[].length > 0; // LINT
Expand Down