Skip to content

Add avoid_direct_collection_equality_checks lint - #546

Open
cupofme wants to merge 5 commits into
masterfrom
feature/avoid_direct_collection_equality_checks-lint
Open

Add avoid_direct_collection_equality_checks lint#546
cupofme wants to merge 5 commits into
masterfrom
feature/avoid_direct_collection_equality_checks-lint

Conversation

@cupofme

@cupofme cupofme commented Jul 15, 2026

Copy link
Copy Markdown

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 / mapEquals from package:flutter/foundation.dart
  • const ListEquality<int>().equals(a, b) / SetEquality / MapEquality from package:collection

The 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

@cupofme
cupofme requested a review from mchudy as a code owner July 15, 2026 14:28
@github-actions github-actions Bot added the p: leancode_lint Related to the leancode_lint package label Jul 15, 2026

await builder.addDartFileEdit(file, (builder) {
builder
..importLibraryElement(.parse('package:collection/collection.dart'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix should only be offered if the package containing the analyzed file depends on package:collection

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be checking pubspec.yaml. Get the package config instead

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to something similar to what what depend_on_referenced_packages is doing

Comment thread packages/leancode_lint/lib/src/lints/avoid_direct_collection_equality_checks.dart Outdated
Comment thread packages/leancode_lint/lib/src/lints/avoid_direct_collection_equality_checks.dart Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add identical() as "good"

Comment on lines +102 to +106
// `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');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


await builder.addDartFileEdit(file, (builder) {
builder
..importLibraryElement(.parse('package:flutter/foundation.dart'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be checking pubspec.yaml. Get the package config instead

@cupofme
cupofme requested a review from PiotrRogulski July 27, 2026 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: leancode_lint Related to the leancode_lint package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants