Skip to content

Analyzer should report "unnecessary" and "redundant" imports #44569

@srawlins

Description

@srawlins

The analyzer should report when a library uses some elements which are provided by more than one import directive (with the same prefix).

If any such import directive provides fewer elements which are used by the library than any of the other relevant imports, then that import directive is unnecessary. Removing each unnecessary import is strictly safe.

If any such import directive provides the exact same elements which are used by the library as one or more of the other relevant imports, then it is redundant. Removing one redundant import is strictly safe, but removing all redundant imports is not safe, and the analyzer should use a heuristic to decide what action to recommend.

Examples:

Simple unnecessary import

import 'one.dart' show A, B;
import 'two.dart' show A;
A? a;
B? b;

The import of two.dart is unnecessary.

Simple redundant import

import 'one.dart' show A;
import 'two.dart' show A;
A? a;

Each import is redundant; neither is unnecessary.

More interesting unnecessary imports

import 'one.dart' show A;
import 'two.dart' show A, B;
import 'three.dart' show A, B, C;
A? a;
B? b;
C? c;

The imports of one.dart and two.dart are unnecessary.

More interesting redundant imports

import 'one.dart' show A, B;
import 'two.dart' show A, C;
import 'three.dart' show B, C;
A? a;
B? b;
C? c;

Each of the three imports is redundant, but ultimately, only one can be safely removed.

Metadata

Metadata

Assignees

Labels

P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-warningIssues with the analyzer's Warning codestype-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions