Add avoid_direct_collection_equality_checks lint - #546
Conversation
|
|
||
| await builder.addDartFileEdit(file, (builder) { | ||
| builder | ||
| ..importLibraryElement(.parse('package:collection/collection.dart')) |
There was a problem hiding this comment.
This fix should only be offered if the package containing the analyzed file depends on package:collection
There was a problem hiding this comment.
God point, added this check.
Current implementation only checks for a direct dependency in the pubspec.yaml of the package. There is still an edge case when other direct dependency can itself export collection package, but I don't think it's worth covering.
There was a problem hiding this comment.
I don't think we should be checking pubspec.yaml. Get the package config instead
There was a problem hiding this comment.
Updated to something similar to what what depend_on_referenced_packages is doing
There was a problem hiding this comment.
Add identical() as "good"
| // `Map` is checked first because it is not an `Iterable`, while `Set` and | ||
| // `List` both are. | ||
| const map = TypeChecker.fromName('Map', packageName: 'dart:core'); | ||
| const set = TypeChecker.fromName('Set', packageName: 'dart:core'); | ||
| const list = TypeChecker.fromName('List', packageName: 'dart:core'); |
There was a problem hiding this comment.
You could use isDartCore{type} getters (e.g. https://pub.dev/documentation/analyzer/latest/dart_element_type/DartType/isDartCoreMap.html)
|
|
||
| await builder.addDartFileEdit(file, (builder) { | ||
| builder | ||
| ..importLibraryElement(.parse('package:flutter/foundation.dart')) |
There was a problem hiding this comment.
Similar to package:collection – this should only be available when Flutter is available
|
|
||
| await builder.addDartFileEdit(file, (builder) { | ||
| builder | ||
| ..importLibraryElement(.parse('package:collection/collection.dart')) |
There was a problem hiding this comment.
I don't think we should be checking pubspec.yaml. Get the package config instead
Comparing collections with ==/!= in Dart checks identity, not contents — so [1, 2] == [1, 2] is false. It's an easy mistake to make and a painful one to debug, since the code reads like it should work.
This adds a lint that flags direct ==/!= comparisons between List, Set, or Map values (and their subtypes) when both sides are the same kind of collection.
Each warning comes with two quick fixes:
listEquals(a, b)/setEquals/mapEqualsfrom package:flutter/foundation.dartListEquality<int>().equals(a, b)/SetEquality/MapEqualityfrom package:collectionThe messages and fix names adapt to the collection type (e.g. it suggests listEquals for a list, mapEquals for a map), and the package:collection fix fills in the type argument so you don't get an inference warning afterwards.
Screen.Recording.2026-07-15.at.16.30.20.mov