rust-lang / rust-lang/rust-clippy
single_component_path_imports with macros and $crate::
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
In exported macros, it is often necessary to write things like $crate::some_other_crate::its_thing, and arrange to reexport some_other_crate, to avoid imposing strange import requirements on other crates that use the macro.
If one writes a macro that is currently not exported, but one might want to export in the future, it is a good idea to do this, because nothing will spot a failure to do it later when the macro becomes exported.
But, then, one gets a single_component_path_imports false positive due to the single element path import of some_other_crate, which is necessary for the $crate to work, but which also ought to be private.
I found #7168 which is a complaint about the same kind of situation, but in that case arguably the code ought to be changed. I think in my case the code is fine as it is.
Lint Name
single_component_path_imports
Reproducer
I tried this code:
#![warn(clippy::all)]
// Without this line:
// $crate::rand::random()
// ^^^^ unresolved import
//
// With this line:
// warning: this import is redundant
// help: remove it entirely
use rand;
mod m {
// At some future point, we plan to make this an exported macro,
// so let's do the $crate::... thing.
macro_rules! use_rand { {} => {
$crate::rand::random()
} }
pub fn mk() -> u32 {
use_rand!()
}
}
fn main() {
println!("{}", m::mk());
}
I saw this happen:
warning: this import is redundant
--> src/main.rs:10:1
|
10 | use rand;
| ^^^^^^^^^ help: remove it entirely
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports
note: the lint level is defined here
--> src/main.rs:1:9
|
1 | #![warn(clippy::all)]
| ^^^^^^^^^^^
= note: `#[warn(clippy::single_component_path_imports)]` implied by `#[warn(clippy::all)]`
I expected to see this happen:
No complaint.
Version
1.64.0 stable in the playground, 2018 edition.
The bug does not seem to appear in the playground with 2021 edition.
Additional Labels
@rustbot label +suggestion-causes-error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the single_component_path_imports lint and reproducing the Rust 2018 example from the issue, then compare its behavior with the noted Rust 2021 result. The change is done when the valid $crate::rand::random() pattern no longer produces a redundant-import warning, with regression coverage for the reproducer.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100