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

add lint for prevent global import #1228

Closed
little-dude opened this issue Sep 15, 2016 · 6 comments · Fixed by #5029
Closed

add lint for prevent global import #1228

little-dude opened this issue Sep 15, 2016 · 6 comments · Fixed by #5029
Labels
good-first-issue These issues are a good way to get started with Clippy L-style Lint: Belongs in the style lint group T-AST Type: Requires working with the AST

Comments

@little-dude
Copy link

Clippy already wans when importing all the variants of an enum. But this is still allowed:

use crate::*;

This kind of global import makes the code more difficult to read since we don't know where symbols come from. It may also confuse IDEs. Could we add a lint to prevent this?

Link to the relevant reddit discussion

@oli-obk oli-obk added good-first-issue These issues are a good way to get started with Clippy T-AST Type: Requires working with the AST L-style Lint: Belongs in the style lint group labels Sep 16, 2016
@oli-obk
Copy link
Contributor

oli-obk commented Sep 16, 2016

this should be a restriction lint.

one hard optional feature would be to figure out what is currently used and add a suggestion to replace it with a full import list.

@kennytm
Copy link
Member

kennytm commented Oct 25, 2017

Note that prelude modules such as

use iron::prelude::*;

may use a different lint ID, so one could allow glob-import of the preludes but warn against other glob imports.

@felix91gr
Copy link
Contributor

I do not know the syntax by memory, so I have to ask: is this still an issue after the module changes made for Rust 2018 Edition?

@Manishearth
Copy link
Member

Manishearth commented Mar 2, 2019

Yep. Glob imports weren't changed.

@mati865
Copy link
Contributor

mati865 commented Mar 2, 2019

FWIW VS Code and Intellij IDEA both offer imports deglobbing which replace * with required imports.

@felix91gr
Copy link
Contributor

felix91gr commented Mar 3, 2019

FWIW VS Code and Intellij IDEA both offer imports deglobbing which replace * with required imports.

That's super awesome :D


Is there a way to ask the compiler about the modules or symbols that the current file is using? If so, I think I know how to write this lint:

  1. Check if there are any wildcard imports (I'm assuming this info is available to Clippy).
  2. If there are, ask about the modules/symbols used by the current file.
  3. Intersect (1) and (2)
  4. If the result of (3) is not empty, lint about it!

The fixit for this lint (something like what VS Code and Intellij IDEA do) would require a bit more work to make, but I don't think it's that far from the lint itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good-first-issue These issues are a good way to get started with Clippy L-style Lint: Belongs in the style lint group T-AST Type: Requires working with the AST
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants