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

New Lint: wildcard imports #5029

Merged
merged 10 commits into from
Feb 21, 2020
Merged

New Lint: wildcard imports #5029

merged 10 commits into from
Feb 21, 2020

Conversation

flip1995
Copy link
Member

@flip1995 flip1995 commented Jan 9, 2020

Fixes #1228

A few notes:

  • I put this lint in the pedantic group, even though in the issue restriction was mentioned.
  • Every fallout fix was automatically applied by cargo fix (This produced 3 unused_imports warnings) and are in commit 7e834c8. So reverting these changes wouldn't be a problem.

A few ideas:

  • A configuration to specify the amount of imported Items, where a * might be better.
  • A configuration to disable the lint for enums. Or just disable the lint for enums, since there is enum_glob_use I moved enum_glob_use into this lint in 12937f0

A few quotes from the issue:

Is there a way to ask the compiler about the modules or symbols that the current file is using?

Yes there is. I found it, once I was nearly finished implementing it myself. See 321d64a

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.

Yeah that was pretty hard, until I found the query for this. Queries are cool, but too hard to find.

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

And now, Clippy can do this too! 🎉


Your thoughts on the notes/ideas?

changelog: Add new lint [wildcard imports]. Add suggestion to [enum_glob_use]

@flip1995 flip1995 added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 9, 2020
Copy link
Member Author

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we could move the enum_glob_use lint into this one. It appears, that this lint catches use Enum::*s, that enum_glob_use doesn't catch. Also we could provide a suggestion for the enum_glob_use lint by moving it into this LintPass.

clippy_lints/src/assign_ops.rs Show resolved Hide resolved
@@ -153,8 +153,6 @@ impl Constant {

/// Parses a `LitKind` to a `Constant`.
pub fn lit_to_constant(lit: &LitKind, ty: Option<Ty<'_>>) -> Constant {
use syntax::ast::*;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This produced one unused_imports warning, since FloatTy and LitKind was imported twice after fixing it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding this to the Known Problems section?

clippy_dev/src/stderr_length_check.rs Outdated Show resolved Hide resolved
clippy_dev/src/lib.rs Outdated Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Jan 11, 2020

☔ The latest upstream changes (presumably #5040) made this pull request unmergeable. Please resolve the merge conflicts.

@flip1995 flip1995 force-pushed the wildcard_imports branch 4 times, most recently from b5ab922 to 719526f Compare January 14, 2020 12:58
@flip1995
Copy link
Member Author

Or maybe we could move the enum_glob_use lint into this one.

Done in 12937f0

@flip1995 flip1995 changed the title [WIP] New Lint: wildcard imports New Lint: wildcard imports Jan 14, 2020
@bors
Copy link
Collaborator

bors commented Jan 15, 2020

☔ The latest upstream changes (presumably #5034) made this pull request unmergeable. Please resolve the merge conflicts.

@llogiq
Copy link
Contributor

llogiq commented Jan 17, 2020

Is the import-use query overeager? The BufRead case may be because the respective method of Read and BufRead are both called read, so it may be that resolve is too limited to distinguish which is needed.

The FloatTy and Lit case looks like our filtering doesn't completely work, though. Or am I missing something?`

@flip1995
Copy link
Member Author

Is the import-use query overeager?

Yes it appears to be, but as long as it introduces only warnings, this should be ok.

The FloatTy and Lit case looks like our filtering doesn't completely work, though. Or am I missing something?`

FloatTy and LitKind was imported twice: once on the module level and once on the function level (with a *). This probably only happens by accident and should be fixed, by either only importing it at function level or at module level. But IMO the user has to decide where he wants this import. So signaling this by producing an unknown_imports warning seems fine to me.

@bors
Copy link
Collaborator

bors commented Feb 8, 2020

☔ The latest upstream changes (presumably #5140) made this pull request unmergeable. Please resolve the merge conflicts.

@ghost
Copy link

ghost commented Feb 8, 2020

IMO, this lint shouldn't warn about prelude modules since their only purpose is to be glob imported. Or if it does, it should resolve the re-exports. Suggesting something like use std::io::prelude::Read; looks really wrong to me.

@bors
Copy link
Collaborator

bors commented Feb 10, 2020

☔ The latest upstream changes (presumably #5148) made this pull request unmergeable. Please resolve the merge conflicts.

@flip1995 flip1995 force-pushed the wildcard_imports branch 2 times, most recently from c4b3534 to 0e8f8bd Compare February 10, 2020 09:09
@bors
Copy link
Collaborator

bors commented Feb 17, 2020

☔ The latest upstream changes (presumably #5182) made this pull request unmergeable. Please resolve the merge conflicts.

@phansch phansch self-requested a review February 20, 2020 13:25
Copy link
Member

@phansch phansch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some nits, apart from that it looks good to me 💯

@@ -153,8 +153,6 @@ impl Constant {

/// Parses a `LitKind` to a `Constant`.
pub fn lit_to_constant(lit: &LitKind, ty: Option<Ty<'_>>) -> Constant {
use syntax::ast::*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding this to the Known Problems section?

clippy_lints/src/wildcard_imports.rs Show resolved Hide resolved
clippy_lints/src/wildcard_imports.rs Outdated Show resolved Hide resolved
@flip1995 flip1995 force-pushed the wildcard_imports branch 2 times, most recently from c91a9fb to cc13da5 Compare February 21, 2020 09:30
@bors
Copy link
Collaborator

bors commented Feb 21, 2020

☔ The latest upstream changes (presumably #5202) made this pull request unmergeable. Please resolve the merge conflicts.

Queries are cool, but too hard to find.
@phansch
Copy link
Member

phansch commented Feb 21, 2020

@bors r+

@bors
Copy link
Collaborator

bors commented Feb 21, 2020

📌 Commit 4dd2252 has been approved by phansch

@bors
Copy link
Collaborator

bors commented Feb 21, 2020

⌛ Testing commit 4dd2252 with merge e342047...

@bors
Copy link
Collaborator

bors commented Feb 21, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: phansch
Pushing e342047 to master...

@bors bors merged commit e342047 into rust-lang:master Feb 21, 2020
@flip1995 flip1995 deleted the wildcard_imports branch February 21, 2020 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add lint for prevent global import
4 participants