Skip to content
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

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

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

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

srawlins opened this issue Dec 30, 2020 · 4 comments
Assignees
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. 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 area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request analyzer-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 <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
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 <srawlins@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
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 <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
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 <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
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 <sigmund@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
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.
srawlins added a commit to srawlins/engine that referenced this issue Jul 20, 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 7, 2021
Bug: #44569
Change-Id: Ied7a1ed105ac82cf9e0f38454bce8cb4a69e17e7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212605
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
dart-bot pushed a commit that referenced this issue Sep 8, 2021
Bug: #44569
Change-Id: I92e93777ee4f62e82f179338475828edeaac15dd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212608
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
dart-bot pushed a commit that referenced this issue Sep 8, 2021
Bug: #44569
Change-Id: I847603b5a1debb55b5c78e0d837ebcfa2d133d77
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212607
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
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 <srawlins@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Ben Konyi <bkonyi@google.com>
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 <srawlins@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
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 <srawlins@google.com>
Commit-Queue: Mayank Patke <fishythefish@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
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 <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
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 <jensj@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
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 <johnniwinther@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
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 <srawlins@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
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 <srawlins@google.com>
Commit-Queue: Riley Porter <rileyporter@google.com>
Reviewed-by: Riley Porter <rileyporter@google.com>
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 <fishythefish@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
@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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. 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

2 participants