Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
srawlins opened this issue Dec 30, 2020 · 7 comments
Open

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

srawlins opened this issue Dec 30, 2020 · 7 comments
Assignees
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-warning Issues with the analyzer's Warning codes P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@srawlins
Copy link
Member

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.

@srawlins srawlins added legacy-area-analyzer Use area-devexp instead. P3 A lower priority bug or feature request devexp-warning Issues with the analyzer's Warning codes labels Dec 30, 2020
@srawlins srawlins self-assigned this Dec 30, 2020
dart-bot pushed a commit that referenced this issue Dec 30, 2020
#44569

Change-Id: I0b5d237f532ab99c112247e9f24392ef317575dd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177222
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
dart-bot pushed a commit that referenced this issue Dec 30, 2020
TEST=Existing tests on existing bots

Bug: #44569
Change-Id: I0c8ee9afa85da3a1a39a983841e62956e76dd509
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177242
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Johnni Winther <[email protected]>
dart-bot pushed a commit that referenced this issue Dec 30, 2020
Bug: #44569
Change-Id: I75ca15c391e0c77840e65d27b26583f15b59eea4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177340
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
dart-bot pushed a commit that referenced this issue Dec 30, 2020
Bug: #44569
Change-Id: I621f187a7ec654235b21a77f28258bba7ef8c179
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177300
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
srawlins added a commit to srawlins/flutter that referenced this issue Jan 5, 2021
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this CL, we remove the imports which can be simply removed in favor of the
other already present imports.

See dart-lang/sdk#44569 for more information.
srawlins added a commit to srawlins/flutter that referenced this issue Jan 5, 2021
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this change, we remove the imports which can be simply removed in favor of
the other already present imports.

See dart-lang/sdk#44569 for more information.
dart-bot pushed a commit that referenced this issue Jan 5, 2021
Bug: #44569
Change-Id: Icad2908772eb42593c0149a99b8256d470490670
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177241
Reviewed-by: Sigmund Cherem <[email protected]>
Reviewed-by: Nicholas Shahan <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
srawlins added a commit to srawlins/flutter that referenced this issue Jan 5, 2021
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this change, we remove the imports which can be simply removed in favor of
the other already present imports.

See dart-lang/sdk#44569 for more information.
srawlins added a commit to srawlins/flutter that referenced this issue Jan 6, 2021
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this change, we remove the imports which can be simply removed in favor of
the other already present imports.

See dart-lang/sdk#44569 for more information.
srawlins added a commit to srawlins/flutter that referenced this issue Jan 7, 2021
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this change, we remove the imports which can be simply removed in favor of
the other already present imports.

See dart-lang/sdk#44569 for more information.
srawlins added a commit to srawlins/flutter that referenced this issue Jan 11, 2021
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this change, we remove the imports which can be simply removed in favor of
the other already present imports.

See dart-lang/sdk#44569 for more information.
dart-bot pushed a commit that referenced this issue Sep 9, 2021
TEST=N/A

Bug: #44569
Change-Id: I61adbdb6666fee9f34372a83d4c3383b636f76e9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212606
Auto-Submit: Samuel Rawlins <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Ben Konyi <[email protected]>
srawlins added a commit to srawlins/flutter that referenced this issue Sep 9, 2021
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this change, we remove the imports which can be simply removed in favor of
the other already present imports.

See dart-lang/sdk#44569 for more information.
srawlins added a commit to srawlins/flutter that referenced this issue Sep 14, 2021
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this change, we remove the imports which can be simply removed in favor of
the other already present imports.

Additionally, I add an ignore to analysis_options.yaml, so that
unnecessary_import can land in Dart. After it lands, we can clean up the
remaining cases in flutter.

See dart-lang/sdk#44569 for more information.
dart-bot pushed a commit that referenced this issue Sep 15, 2021
Bug: #44569
Change-Id: I646f5994b40f2007f8d8d4b5ef35e2c9ffbd2376
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213423
Auto-Submit: Samuel Rawlins <[email protected]>
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Johnni Winther <[email protected]>
dart-bot pushed a commit that referenced this issue Sep 16, 2021
Bug: #44569
Change-Id: I98e103a1af11794927fc1fe6794e69ec655debf6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213424
Auto-Submit: Samuel Rawlins <[email protected]>
Commit-Queue: Mayank Patke <[email protected]>
Reviewed-by: Mayank Patke <[email protected]>
copybara-service bot pushed a commit that referenced this issue Sep 20, 2021
Many of these are a redundancy between
    'package:analyzer/src/generated/source.dart' and
    'package:analyzer/src/generated/source_io.dart'

I chose to remove the latter, for no particular reason.

Bug: #44569
Change-Id: Ic69b09fd502c978cef9e4d3b8a0ef9acd92d5184
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213849
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
copybara-service bot pushed a commit that referenced this issue Sep 20, 2021
Each of these imports removed is redundant with
'package:kernel/ast.dart'.

Bug: #44569
Change-Id: Ia9bc08b4ffd863572c84fe8a4fa465d2e8e04f7c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213845
Reviewed-by: Jens Johansen <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
copybara-service bot pushed a commit that referenced this issue Sep 20, 2021
Very often there were two imports of the same URI, one of which also
had a `show` combinator. I opted to remove the other one.

Bug: #44569
Change-Id: I1263c892f94f0645fda6dee371dfc1fcddcd2344
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213846
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
copybara-service bot pushed a commit that referenced this issue Sep 20, 2021
Bug: #44569
Change-Id: Iab83d15b0f6c5448b75881fcba8c8ea177feab71
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213852
Auto-Submit: Samuel Rawlins <[email protected]>
Commit-Queue: Nicholas Shahan <[email protected]>
Reviewed-by: Nicholas Shahan <[email protected]>
copybara-service bot pushed a commit that referenced this issue Sep 20, 2021
Bug: #44569
Change-Id: I6fa385eddc76397ce4944674bd548a16755f265d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213850
Auto-Submit: Samuel Rawlins <[email protected]>
Commit-Queue: Riley Porter <[email protected]>
Reviewed-by: Riley Porter <[email protected]>
copybara-service bot pushed a commit that referenced this issue Sep 20, 2021
In many files, there is a redundancy in importing

    import 'package:compiler/src/common.dart'
    and
    import 'package:compiler/src/diagnostics/diagnostic_listener.dart'

I chose to remove the latter import.

Bug: #44569
Change-Id: Ia6c871080bf2d7c637694a4fe5cb1b84e540b789
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213851
Reviewed-by: Mayank Patke <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
@kevmoo
Copy link
Member

kevmoo commented Oct 15, 2021

@srawlins – can we close this?

@srawlins
Copy link
Member Author

We're not reporting redundant imports yet.

@kevmoo
Copy link
Member

kevmoo commented Oct 15, 2021 via email

@srawlins
Copy link
Member Author

Those should just be unnecessary imports.

@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 13, 2024
RobertSun123 pushed a commit to RobertSun123/engine that referenced this issue May 8, 2024
In each library where an import is removed, the library uses some elements
provided by the import, BUT there is another import which provides all of the
same elements, and at least one more which the library uses.

In this change, we remove the imports which can be simply removed in favor of
the other already present imports.

See dart-lang/sdk#44569 for more information.
@FMorschel
Copy link
Contributor

FMorschel commented Feb 11, 2025

What is missing from this issue @srawlins? Can this be closed?

@srawlins
Copy link
Member Author

Redundant imports are not yet reported.

@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-warning Issues with the analyzer's Warning codes P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants