Skip to content

Analyzer doesn't give unnecessary_import feedback if element used only from 1 export #48038

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

Closed
SuperPenguin opened this issue Jan 2, 2022 · 3 comments
Labels
legacy-area-analyzer Use area-devexp instead. P3 A lower priority bug or feature request

Comments

@SuperPenguin
Copy link

SuperPenguin commented Jan 2, 2022

I'm doing a barrel export and notice when using only elements from 1 export files it doesn't give unnecessary_import at all.

For example if I have
a.dart

class A {}

class A2 {}

b.dart

class B {}

c.dart

class C {}

alphabet.dart

export 'a.dart';
export 'b.dart';
export 'c.dart';

and used it like so, the analyzer give feedback unnecessary_import for all a, b, c dart files

import 'package:hmmm/src/alphabet/a.dart'; // unnecessary_import
import 'package:hmmm/src/alphabet/b.dart'; // unnecessary_import
import 'package:hmmm/src/alphabet/c.dart'; // unnecessary_import
import 'package:hmmm/src/alphabet/alphabet.dart';

void something() {
  final a = A();
  final a2 = A2();
  final b = B();
  final c = C();
}

but if only element used from a.dart, it doesn't give unnecessary_import to a, only unused_import for b and c.

import 'package:hmmm/src/alphabet/a.dart';
import 'package:hmmm/src/alphabet/b.dart'; // unused_import
import 'package:hmmm/src/alphabet/c.dart'; // unused_import
import 'package:hmmm/src/alphabet/alphabet.dart';

void something() {
  final a = A();
  final a2 = A2();
  // final b = B();
  // final c = C();
}

dart --version
Dart SDK version: 2.15.1 (stable) (Tue Dec 14 13:32:21 2021 +0100) on "windows_x64"

IDE
vscode

@lrhn lrhn added the legacy-area-analyzer Use area-devexp instead. label Jan 3, 2022
@scheglov scheglov added the P3 A lower priority bug or feature request label Jan 3, 2022
@scheglov
Copy link
Contributor

scheglov commented Jan 3, 2022

@srawlins

@srawlins
Copy link
Member

srawlins commented Jan 5, 2022

In the second case you describe, alphabet.dart and a.dart provide the same set of "used" elements (A and A2). We consider these "redundant" imports, rather than "unnecessary."

We have not yet landed the check for redundant imports. The check is more complicated, because when two import directives provide the same set of used elements, we want to be careful about recommending which import directive to remove.

@srawlins
Copy link
Member

Follow support for redundant imports here: #44569.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead. P3 A lower priority bug or feature request
Projects
None yet
Development

No branches or pull requests

4 participants