Rust-clippy: add lint for prevent global import

Created on 15 Sep 2016  路  6Comments  路  Source: rust-lang/rust-clippy

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

A-style T-AST good-first-issue hacktoberfest

Most helpful comment

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

All 6 comments

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.

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.

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?

Yep. Glob imports weren't changed.

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vitorenesduarte picture vitorenesduarte  路  3Comments

matthiaskrgr picture matthiaskrgr  路  3Comments

Riateche picture Riateche  路  3Comments

wareya picture wareya  路  3Comments

Luro02 picture Luro02  路  3Comments