Lints, analysis option fix, deprecations (#63)
* Rename analysis_options
* lints
* stop using deprecated `throws` matcher
* Eliminate most implicit casts
diff --git a/.analysis_options b/.analysis_options
deleted file mode 100644
index a10d4c5..0000000
--- a/.analysis_options
+++ /dev/null
@@ -1,2 +0,0 @@
-analyzer:
- strong-mode: true
diff --git a/analysis_options.yaml b/analysis_options.yaml
new file mode 100644
index 0000000..5b5eb96
--- /dev/null
+++ b/analysis_options.yaml
@@ -0,0 +1,37 @@
+analyzer:
+ strong-mode: true
+ errors:
+ unused_element: error
+ unused_import: error
+ unused_local_variable: error
+ dead_code: error
+linter:
+ rules:
+ # Errors
+ - avoid_empty_else
+ - control_flow_in_finally
+ - empty_statements
+ - hash_and_equals
+ - implementation_imports
+ - test_types_in_equals
+ - throw_in_finally
+ - unrelated_type_equality_checks
+ - valid_regexps
+
+ # Style
+ - avoid_init_to_null
+ - avoid_return_types_on_setters
+ - await_only_futures
+ - camel_case_types
+ - directives_ordering
+ - empty_catches
+ - empty_constructor_bodies
+ - library_names
+ - library_prefixes
+ - non_constant_identifier_names
+ - only_throw_errors
+ - prefer_final_fields
+ - prefer_is_not_empty
+ - prefer_single_quotes
+ - slash_for_doc_comments
+ - type_init_formals
diff --git a/lib/src/equality.dart b/lib/src/equality.dart
index 2c6c272..be7162b 100644
--- a/lib/src/equality.dart
+++ b/lib/src/equality.dart
@@ -274,12 +274,10 @@
7 * equality._valueEquality.hash(value)) &
_HASH_MASK;
- bool operator ==(Object other) {
- if (other is! _MapEntry) return false;
- _MapEntry otherEntry = other;
- return equality._keyEquality.equals(key, otherEntry.key) &&
- equality._valueEquality.equals(value, otherEntry.value);
- }
+ bool operator ==(Object other) =>
+ other is _MapEntry &&
+ equality._keyEquality.equals(key, other.key) &&
+ equality._valueEquality.equals(value, other.value);
}
/// Equality on maps.
@@ -411,26 +409,23 @@
bool equals(e1, e2) {
if (e1 is Set) {
- if (e2 is! Set) return false;
- return new SetEquality(this).equals(e1, e2);
+ return e2 is Set && new SetEquality(this).equals(e1, e2);
}
if (e1 is Map) {
- if (e2 is! Map) return false;
- return new MapEquality(keys: this, values: this).equals(e1, e2);
+ return e2 is Map &&
+ new MapEquality(keys: this, values: this).equals(e1, e2);
}
if (!_unordered) {
if (e1 is List) {
- if (e2 is! List) return false;
- return new ListEquality(this).equals(e1, e2);
+ return e2 is List && new ListEquality(this).equals(e1, e2);
}
if (e1 is Iterable) {
- if (e2 is! Iterable) return false;
- return new IterableEquality(this).equals(e1, e2);
+ return e2 is Iterable && new IterableEquality(this).equals(e1, e2);
}
} else if (e1 is Iterable) {
- if (e2 is! Iterable) return false;
if (e1 is List != e2 is List) return false;
- return new UnorderedIterableEquality(this).equals(e1, e2);
+ return e2 is Iterable &&
+ new UnorderedIterableEquality(this).equals(e1, e2);
}
return _base.equals(e1, e2);
}
diff --git a/lib/src/functions.dart b/lib/src/functions.dart
index 933de82..b4ff9a9 100644
--- a/lib/src/functions.dart
+++ b/lib/src/functions.dart
@@ -2,8 +2,8 @@
// 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.
-import 'dart:math' as math;
import 'dart:collection';
+import 'dart:math' as math;
import 'utils.dart';
diff --git a/lib/src/queue_list.dart b/lib/src/queue_list.dart
index 73c1f66..7f10261 100644
--- a/lib/src/queue_list.dart
+++ b/lib/src/queue_list.dart
@@ -114,7 +114,7 @@
int get length => (_tail - _head) & (_table.length - 1);
- void set length(int value) {
+ set length(int value) {
if (value < 0) throw new RangeError("Length $value may not be negative.");
int delta = value - length;
diff --git a/lib/src/typed_wrappers.dart b/lib/src/typed_wrappers.dart
index 242dc17..118a614 100644
--- a/lib/src/typed_wrappers.dart
+++ b/lib/src/typed_wrappers.dart
@@ -150,7 +150,7 @@
int lastIndexOf(E element, [int start]) =>
_listBase.lastIndexOf(element, start);
- void set length(int newLength) {
+ set length(int newLength) {
_listBase.length = newLength;
}
diff --git a/lib/src/unmodifiable_wrappers.dart b/lib/src/unmodifiable_wrappers.dart
index 1b4d2e4..070ab78 100644
--- a/lib/src/unmodifiable_wrappers.dart
+++ b/lib/src/unmodifiable_wrappers.dart
@@ -2,10 +2,10 @@
// 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.
-export "dart:collection" show UnmodifiableListView, UnmodifiableMapView;
-
-import 'wrappers.dart';
import 'empty_unmodifiable_set.dart';
+import 'wrappers.dart';
+
+export "dart:collection" show UnmodifiableListView, UnmodifiableMapView;
/// A fixed-length list.
///
@@ -33,7 +33,7 @@
/// Throws an [UnsupportedError];
/// operations that change the length of the list are disallowed.
- void set length(int newLength) => _throw();
+ set length(int newLength) => _throw();
/// Throws an [UnsupportedError];
/// operations that change the length of the list are disallowed.
diff --git a/lib/src/wrappers.dart b/lib/src/wrappers.dart
index 555296d..c3f6438 100644
--- a/lib/src/wrappers.dart
+++ b/lib/src/wrappers.dart
@@ -159,14 +159,14 @@
_listBase.insert(index, element);
}
- void insertAll(int index, Iterable<E> iterable) {
+ insertAll(int index, Iterable<E> iterable) {
_listBase.insertAll(index, iterable);
}
int lastIndexOf(E element, [int start]) =>
_listBase.lastIndexOf(element, start);
- void set length(int newLength) {
+ set length(int newLength) {
_listBase.length = newLength;
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 5d88529..9bf346e 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: collection
-version: 1.14.3
+version: 1.14.4-dev
author: Dart Team <misc@dartlang.org>
description: Collections and utilities functions and classes related to collections.
homepage: https://www.github.com/dart-lang/collection
diff --git a/test/algorithms_test.dart b/test/algorithms_test.dart
index dbd2633..c7f5af1 100644
--- a/test/algorithms_test.dart
+++ b/test/algorithms_test.dart
@@ -3,10 +3,10 @@
// BSD-style license that can be found in the LICENSE file.
/// Tests algorithm utilities.
+import 'dart:math';
import "package:collection/collection.dart";
import "package:test/test.dart";
-import 'dart:math';
void main() {
void testShuffle(List list) {
@@ -212,7 +212,7 @@
// larger case where the internal moving insertion sort is used
// larger cases with multiple splittings, numbers just around a power of 2.
for (int size in [8, 50, 511, 512, 513]) {
- List list = new List(size);
+ var list = new List<OC>(size);
// Class OC compares using id.
// With size elements with id's in the range 0..size/4, a number of
// collisions are guaranteed. These should be sorted so that the "order"
@@ -234,7 +234,8 @@
List copy = list.toList();
int min = size >> 2;
int max = size - min;
- mergeSort(list, start: min, end: max, compare: (a, b) => b.compareTo(a));
+ mergeSort<OC>(list,
+ start: min, end: max, compare: (a, b) => b.compareTo(a));
prev = list[min];
for (int i = min + 1; i < max; i++) {
OC next = list[i];
diff --git a/test/canonicalized_map_test.dart b/test/canonicalized_map_test.dart
index 437e44f..716e738 100644
--- a/test/canonicalized_map_test.dart
+++ b/test/canonicalized_map_test.dart
@@ -10,7 +10,7 @@
var map;
setUp(() {
map = new CanonicalizedMap<int, String, String>(int.parse,
- isValidKey: (s) => new RegExp(r"^\d+$").hasMatch(s));
+ isValidKey: (s) => new RegExp(r"^\d+$").hasMatch(s as String));
});
test("canonicalizes keys on set and get", () {
@@ -61,8 +61,8 @@
test("canonicalizes keys for putIfAbsent", () {
map["1"] = "value";
- expect(
- map.putIfAbsent("01", () => throw "shouldn't run"), equals("value"));
+ expect(map.putIfAbsent("01", () => throw new Exception("shouldn't run")),
+ equals("value"));
expect(map.putIfAbsent("2", () => "new value"), equals("new value"));
});
diff --git a/test/combined_wrapper/list_test.dart b/test/combined_wrapper/list_test.dart
index 1a00f32..9e90925 100644
--- a/test/combined_wrapper/list_test.dart
+++ b/test/combined_wrapper/list_test.dart
@@ -11,7 +11,7 @@
var list1 = [1, 2, 3];
var list2 = [4, 5, 6];
var list3 = [7, 8, 9];
- var concat = []..addAll(list1)..addAll(list2)..addAll(list3);
+ var concat = <int>[]..addAll(list1)..addAll(list2)..addAll(list3);
// In every way possible this should test the same as an UnmodifiableListView.
common.testUnmodifiableList(
diff --git a/test/combined_wrapper/map_test.dart b/test/combined_wrapper/map_test.dart
index 832fe36..ecafb4b 100644
--- a/test/combined_wrapper/map_test.dart
+++ b/test/combined_wrapper/map_test.dart
@@ -11,7 +11,7 @@
var map1 = const {1: 1, 2: 2, 3: 3};
var map2 = const {4: 4, 5: 5, 6: 6};
var map3 = const {7: 7, 8: 8, 9: 9};
- var concat = {}..addAll(map1)..addAll(map2)..addAll(map3);
+ var concat = <int, int>{}..addAll(map1)..addAll(map2)..addAll(map3);
// In every way possible this should test the same as an UnmodifiableMapView.
common.testReadMap(
diff --git a/test/comparators_test.dart b/test/comparators_test.dart
index aa868d9..4762d17 100644
--- a/test/comparators_test.dart
+++ b/test/comparators_test.dart
@@ -57,7 +57,7 @@
"~"
];
- sortedBy(compare) => strings.toList()
+ void sortedBy(int compare(String a, String b)) => strings.toList()
..shuffle()
..sort(compare);
diff --git a/test/equality_test.dart b/test/equality_test.dart
index 63399c1..ffc8655 100644
--- a/test/equality_test.dart
+++ b/test/equality_test.dart
@@ -9,7 +9,7 @@
import "package:test/test.dart";
main() {
- o(id) => new Element(id);
+ o(Comparable id) => new Element(id);
// Lists that are point-wise equal, but not identical.
var list1 = [o(1), o(2), o(3), o(4), o(5)];
diff --git a/test/functions_test.dart b/test/functions_test.dart
index d75f8cf..a1e8f08 100644
--- a/test/functions_test.dart
+++ b/test/functions_test.dart
@@ -109,7 +109,7 @@
test("uses a custom comparator if provided", () {
expect(
- minBy([
+ minBy<Map<String, int>, Map<String, int>>([
{"foo": 3},
{"foo": 5},
{"foo": 4},
@@ -145,7 +145,7 @@
test("uses a custom comparator if provided", () {
expect(
- maxBy([
+ maxBy<Map<String, int>, Map<String, int>>([
{"foo": 3},
{"foo": 5},
{"foo": 4},
diff --git a/test/ignore_ascii_case_test.dart b/test/ignore_ascii_case_test.dart
index 6aee455..5e3ee4f 100644
--- a/test/ignore_ascii_case_test.dart
+++ b/test/ignore_ascii_case_test.dart
@@ -42,7 +42,7 @@
var lowerCaseLetters = "@`ABCDEFGHIJKLMNOPQRSTUVWXYZ[{åÅ";
expect(equalsIgnoreAsciiCase(upperCaseLetters, lowerCaseLetters), true);
- testChars(char1, char2, areEqual) {
+ testChars(String char1, String char2, bool areEqual) {
expect(equalsIgnoreAsciiCase(char1, char2), areEqual,
reason: "$char1 ${areEqual ? "=" : "!"}= $char2");
}
diff --git a/test/iterable_zip_test.dart b/test/iterable_zip_test.dart
index d4d5484..7b6db73 100644
--- a/test/iterable_zip_test.dart
+++ b/test/iterable_zip_test.dart
@@ -10,6 +10,7 @@
/// Iterable like [base] except that it throws when value equals [errorValue].
Iterable iterError(Iterable base, int errorValue) {
+ // ignore: only_throw_errors
return base.map((x) => x == errorValue ? throw "BAD" : x);
}
diff --git a/test/priority_queue_test.dart b/test/priority_queue_test.dart
index 0e493e5..bcb892f 100644
--- a/test/priority_queue_test.dart
+++ b/test/priority_queue_test.dart
@@ -30,7 +30,7 @@
}
}
-void testCustom(PriorityQueue<C> create(comparator)) {
+void testCustom(PriorityQueue<C> create(int comparator(C a, C b))) {
for (int count in [1, 5, 127, 128]) {
testQueue("Custom:$count/null", () => create(null),
new List<C>.generate(count, (x) => new C(x)), new C(count));
@@ -73,7 +73,7 @@
expect(q.contains(notElement), isFalse);
List all = [];
- while (!q.isEmpty) {
+ while (q.isNotEmpty) {
var expected = q.first;
var actual = q.removeFirst();
expect(actual, same(expected));
diff --git a/test/typed_wrapper/list_test.dart b/test/typed_wrapper/list_test.dart
index e39375e..7876dbe 100644
--- a/test/typed_wrapper/list_test.dart
+++ b/test/typed_wrapper/list_test.dart
@@ -222,7 +222,7 @@
});
group("with invalid types", () {
- var inner;
+ List inner;
var wrapper;
setUp(() {
inner = <Object>["foo", "bar", "baz"];
diff --git a/test/typed_wrapper/map_test.dart b/test/typed_wrapper/map_test.dart
index b81f430..75078bd 100644
--- a/test/typed_wrapper/map_test.dart
+++ b/test/typed_wrapper/map_test.dart
@@ -128,7 +128,7 @@
});
group("with invalid key types", () {
- var inner;
+ Map inner;
var wrapper;
setUp(() {
inner = <Object, Object>{1: 1, 2: 2, 3: 3, 4: 4};
@@ -226,7 +226,7 @@
}, skip: "Re-enable this when test can run DDC (test#414).");
group("with invalid value types", () {
- var inner;
+ Map inner;
var wrapper;
setUp(() {
inner = <Object, Object>{"foo": "bar", "baz": "bang"};
diff --git a/test/typed_wrapper/queue_test.dart b/test/typed_wrapper/queue_test.dart
index ba16e52..e9ffb3a 100644
--- a/test/typed_wrapper/queue_test.dart
+++ b/test/typed_wrapper/queue_test.dart
@@ -81,7 +81,7 @@
});
group("with invalid types", () {
- var inner;
+ Queue inner;
var wrapper;
setUp(() {
inner = new Queue<Object>.from(["foo", "bar", "baz"]);
diff --git a/test/typed_wrapper/set_test.dart b/test/typed_wrapper/set_test.dart
index 2cc96be..d7eed5f 100644
--- a/test/typed_wrapper/set_test.dart
+++ b/test/typed_wrapper/set_test.dart
@@ -87,7 +87,7 @@
});
group("with invalid types", () {
- var inner;
+ Set inner;
var wrapper;
setUp(() {
inner = new Set<Object>.from(["foo", "bar", "baz"]);
diff --git a/test/union_set_controller_test.dart b/test/union_set_controller_test.dart
index aa8c1c9..366b781 100644
--- a/test/union_set_controller_test.dart
+++ b/test/union_set_controller_test.dart
@@ -8,7 +8,7 @@
void main() {
var controller;
- var innerSet;
+ Set<int> innerSet;
setUp(() {
innerSet = new Set.from([1, 2, 3]);
controller = new UnionSetController<int>()..add(innerSet);
diff --git a/test/unmodifiable_collection_test.dart b/test/unmodifiable_collection_test.dart
index 91ad8ad..a6705f6 100644
--- a/test/unmodifiable_collection_test.dart
+++ b/test/unmodifiable_collection_test.dart
@@ -11,7 +11,7 @@
// an throw on the ones that aren't without affecting the original.
main() {
- List list = [];
+ var list = <int>[];
testUnmodifiableList(list, new UnmodifiableListView(list), "empty");
list = [42];
testUnmodifiableList(list, new UnmodifiableListView(list), "single-42");
@@ -33,7 +33,7 @@
list = [1, 7, 10];
testNonGrowableList(list, new NonGrowableListView(list), "three!42");
- Set aSet = new Set();
+ var aSet = new Set<int>();
testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "empty");
aSet = new Set();
testUnmodifiableSet(aSet, const UnmodifiableSetView.empty(), "const empty");
@@ -47,7 +47,7 @@
testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "three!42");
}
-void testUnmodifiableList(List original, List wrapped, String name) {
+void testUnmodifiableList(List<int> original, List<int> wrapped, String name) {
name = "unmodifiable-list-$name";
testIterable(original, wrapped, name);
testReadList(original, wrapped, name);
@@ -55,7 +55,7 @@
testNoChangeLengthList(original, wrapped, name);
}
-void testNonGrowableList(List original, List wrapped, String name) {
+void testNonGrowableList(List<int> original, List<int> wrapped, String name) {
name = "nongrowable-list-$name";
testIterable(original, wrapped, name);
testReadList(original, wrapped, name);
@@ -63,14 +63,14 @@
testNoChangeLengthList(original, wrapped, name);
}
-void testUnmodifiableSet(Set original, Set wrapped, String name) {
+void testUnmodifiableSet(Set<int> original, Set<int> wrapped, String name) {
name = "unmodifiable-set-$name";
testIterable(original, wrapped, name);
testReadSet(original, wrapped, name);
testNoChangeSet(original, wrapped, name);
}
-void testIterable(Iterable original, Iterable wrapped, String name) {
+void testIterable(Iterable<int> original, Iterable<int> wrapped, String name) {
test("$name - any", () {
expect(wrapped.any((x) => true), equals(original.any((x) => true)));
expect(wrapped.any((x) => false), equals(original.any((x) => false)));
@@ -82,7 +82,7 @@
test("$name - elementAt", () {
if (original.isEmpty) {
- expect(() => wrapped.elementAt(0), throws);
+ expect(() => wrapped.elementAt(0), throwsRangeError);
} else {
expect(wrapped.elementAt(0), equals(original.elementAt(0)));
}
@@ -100,7 +100,7 @@
test("$name - first", () {
if (original.isEmpty) {
- expect(() => wrapped.first, throws);
+ expect(() => wrapped.first, throwsStateError);
} else {
expect(wrapped.first, equals(original.first));
}
@@ -108,12 +108,12 @@
test("$name - firstWhere", () {
if (original.isEmpty) {
- expect(() => wrapped.firstWhere((_) => true), throws);
+ expect(() => wrapped.firstWhere((_) => true), throwsStateError);
} else {
expect(wrapped.firstWhere((_) => true),
equals(original.firstWhere((_) => true)));
}
- expect(() => wrapped.firstWhere((_) => false), throws);
+ expect(() => wrapped.firstWhere((_) => false), throwsStateError);
});
test("$name - fold", () {
@@ -158,7 +158,7 @@
test("$name - last", () {
if (original.isEmpty) {
- expect(() => wrapped.last, throws);
+ expect(() => wrapped.last, throwsStateError);
} else {
expect(wrapped.last, equals(original.last));
}
@@ -166,12 +166,12 @@
test("$name - lastWhere", () {
if (original.isEmpty) {
- expect(() => wrapped.lastWhere((_) => true), throws);
+ expect(() => wrapped.lastWhere((_) => true), throwsStateError);
} else {
expect(wrapped.lastWhere((_) => true),
equals(original.lastWhere((_) => true)));
}
- expect(() => wrapped.lastWhere((_) => false), throws);
+ expect(() => wrapped.lastWhere((_) => false), throwsStateError);
});
test("$name - length", () {
@@ -184,7 +184,7 @@
test("$name - reduce", () {
if (original.isEmpty) {
- expect(() => wrapped.reduce((x, y) => x + y), throws);
+ expect(() => wrapped.reduce((x, y) => x + y), throwsStateError);
} else {
expect(wrapped.reduce((x, y) => x + y),
equals(original.reduce((x, y) => x + y)));
@@ -193,7 +193,7 @@
test("$name - single", () {
if (original.length != 1) {
- expect(() => wrapped.single, throws);
+ expect(() => wrapped.single, throwsStateError);
} else {
expect(wrapped.single, equals(original.single));
}
@@ -201,12 +201,12 @@
test("$name - singleWhere", () {
if (original.length != 1) {
- expect(() => wrapped.singleWhere((_) => true), throws);
+ expect(() => wrapped.singleWhere((_) => true), throwsStateError);
} else {
expect(wrapped.singleWhere((_) => true),
equals(original.singleWhere((_) => true)));
}
- expect(() => wrapped.singleWhere((_) => false), throws);
+ expect(() => wrapped.singleWhere((_) => false), throwsStateError);
});
test("$name - skip", () {
@@ -344,8 +344,8 @@
});
}
-void testWriteList(List original, List wrapped, String name) {
- List copy = new List.from(original);
+void testWriteList(List<int> original, List wrapped, String name) {
+ var copy = new List<int>.from(original);
test("$name - []=", () {
if (original.isNotEmpty) {
@@ -356,7 +356,7 @@
} else {
expect(() {
wrapped[0] = 42;
- }, throws);
+ }, throwsRangeError);
}
});
@@ -394,7 +394,7 @@
void testNoChangeLengthList(List original, List wrapped, String name) {
List copy = new List.from(original);
- testThrows(name, thunk) {
+ void testThrows(String name, thunk) {
test(name, () {
expect(thunk, throwsUnsupportedError);
// No modifications happened.
@@ -539,7 +539,7 @@
});
}
-void testReadMap(Map original, Map wrapped, String name) {
+void testReadMap(Map<int, int> original, Map<int, int> wrapped, String name) {
test("$name length", () {
expect(wrapped.length, equals(original.length));
});
diff --git a/test/wrapper_test.dart b/test/wrapper_test.dart
index 9067426..b19b49b 100644
--- a/test/wrapper_test.dart
+++ b/test/wrapper_test.dart
@@ -86,7 +86,7 @@
// argument to DelegatingIterable/Set/List.
class IterableNSM extends NSM implements Iterable, Set, List, Queue {
IterableNSM(action(Invocation i)) : super(action);
- toString() => super.noSuchMethod(TO_STRING_INVOCATION);
+ toString() => super.noSuchMethod(TO_STRING_INVOCATION) as String;
}
// Expector that wraps in DelegatingIterable.
@@ -120,7 +120,7 @@
// Like NSM but implements Map to allow as argument for DelegatingMap.
class MapNSM extends NSM implements Map {
MapNSM(action(Invocation i)) : super(action);
- toString() => super.noSuchMethod(TO_STRING_INVOCATION);
+ toString() => super.noSuchMethod(TO_STRING_INVOCATION) as String;
}
// Expector that wraps in DelegatingMap.
@@ -458,8 +458,8 @@
});
group("MapKeySet", () {
- var map;
- var set;
+ Map<String, dynamic> map;
+ Set<String> set;
setUp(() {
map = new Map<String, int>();
@@ -526,8 +526,8 @@
});
group("MapValueSet", () {
- var map;
- var set;
+ Map<String, String> map;
+ Set<String> set;
setUp(() {
map = new Map<String, String>();