-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
single_component_path_imports
: ignore pub(crate) use some_macro;
#7120
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg}; | ||
use clippy_utils::in_macro; | ||
use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind, UseTreeKind}; | ||
use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind, VisibilityKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
@@ -60,8 +60,21 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) { | |
// ``` | ||
let mut single_use_usages = Vec::new(); | ||
|
||
// keep track of macros defined in the module as we don't want it to trigger on this (#7106) | ||
// ```rust,ignore | ||
// macro_rules! foo { () => {} }; | ||
// pub(crate) use foo; | ||
// ``` | ||
let mut macros = Vec::new(); | ||
|
||
for item in items { | ||
track_uses(cx, &item, &mut imports_reused_with_self, &mut single_use_usages); | ||
track_uses( | ||
cx, | ||
&item, | ||
&mut imports_reused_with_self, | ||
&mut single_use_usages, | ||
&mut macros, | ||
); | ||
} | ||
|
||
for single_use in &single_use_usages { | ||
|
@@ -96,6 +109,7 @@ fn track_uses( | |
item: &Item, | ||
imports_reused_with_self: &mut Vec<Symbol>, | ||
single_use_usages: &mut Vec<(Symbol, Span, bool)>, | ||
macros: &mut Vec<Symbol>, | ||
) { | ||
if in_macro(item.span) || item.vis.kind.is_pub() { | ||
return; | ||
|
@@ -105,14 +119,22 @@ fn track_uses( | |
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => { | ||
check_mod(cx, &items); | ||
}, | ||
ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => { | ||
macros.push(item.ident.name); | ||
}, | ||
ItemKind::Use(use_tree) => { | ||
let segments = &use_tree.prefix.segments; | ||
|
||
let should_report = | ||
|name: &Symbol| !macros.contains(name) || matches!(item.vis.kind, VisibilityKind::Inherited); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I remove the error[E0597]: `name` does not live long enough
--> clippy_lints/src/single_component_path_imports.rs:150:50
|
150 | ... if should_report(&name) {
| ------------- ^^^^^ borrowed value does not live long enough
| |
| borrow later used here
...
153 | ... }
| - `name` dropped here while still borrowed |
||
|
||
// keep track of `use some_module;` usages | ||
if segments.len() == 1 { | ||
if let UseTreeKind::Simple(None, _, _) = use_tree.kind { | ||
let ident = &segments[0].ident; | ||
single_use_usages.push((ident.name, item.span, true)); | ||
let name = segments[0].ident.name; | ||
if should_report(&name) { | ||
single_use_usages.push((name, item.span, true)); | ||
} | ||
} | ||
return; | ||
} | ||
|
@@ -124,8 +146,10 @@ fn track_uses( | |
let segments = &tree.0.prefix.segments; | ||
if segments.len() == 1 { | ||
if let UseTreeKind::Simple(None, _, _) = tree.0.kind { | ||
let ident = &segments[0].ident; | ||
single_use_usages.push((ident.name, tree.0.span, false)); | ||
let name = segments[0].ident.name; | ||
if should_report(&name) { | ||
single_use_usages.push((name, tree.0.span, false)); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// run-rustfix | ||
// edition:2018 | ||
#![warn(clippy::single_component_path_imports)] | ||
#![allow(unused_imports)] | ||
|
||
// #7106: use statements exporting a macro within a crate should not trigger lint | ||
|
||
macro_rules! m1 { | ||
() => {}; | ||
} | ||
pub(crate) use m1; // ok | ||
|
||
macro_rules! m2 { | ||
() => {}; | ||
} | ||
// fail | ||
|
||
fn main() { | ||
m1!(); | ||
m2!(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// run-rustfix | ||
// edition:2018 | ||
#![warn(clippy::single_component_path_imports)] | ||
#![allow(unused_imports)] | ||
|
||
// #7106: use statements exporting a macro within a crate should not trigger lint | ||
|
||
macro_rules! m1 { | ||
() => {}; | ||
} | ||
pub(crate) use m1; // ok | ||
|
||
macro_rules! m2 { | ||
() => {}; | ||
} | ||
use m2; // fail | ||
|
||
fn main() { | ||
m1!(); | ||
m2!(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: this import is redundant | ||
--> $DIR/single_component_path_imports_macro.rs:16:1 | ||
| | ||
LL | use m2; // fail | ||
| ^^^^^^^ help: remove it entirely | ||
| | ||
= note: `-D clippy::single-component-path-imports` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a brief look at the macros 2.0 RFC, and it seems like it will introduce a better way of declaring a macro's visibility:
That's why I've included
macro_rules: true
so this only applies tomacro_rules!
macros, but I'm not too sure about this.