rust-lang / rust-lang/rust-clippy
Clippy lint `redundant_closure_for_method_calls` incorrectly handles raw identifiers when applying fixes
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
When applying fixes to a codebase containing raw identifiers, code produced by clippy fix includes a path with a keyword.
In particular, it converted:
tpe.1.all_types() .filter_map(|tpe| tpe.dotnet_refs())
into
tpe.1.all_types() .filter_map(super::r#type::r#type::Type::dotnet_refs)
when it should have converted it into:
tpe.1.all_types() .filter_map(super::type::type::Type::dotnet_refs)
the clippy lint message also contained an invalid path:
warning: redundant closure
--> src/assembly.rs:678:33
|
678 | .filter_map(|tpe| tpe.dotnet_refs())
| ^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `super::type::type::Type::dotnet_refs`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
The path should be super::r#type::r#type::Type::dotnet_refs instead.
Reproducer
This short snippet is enough to trigger the issue:
mod r#type {
pub struct Type;
impl Type {
pub fn f(&self) -> u8 {
0
}
}
}
fn main() {
let types = [r#type::Type];
let _fs: Vec<_> = types.iter().map(|tpe| tpe.f()).collect();
}
Version
rustc 1.79.0-nightly (c9f8f3438 2024-03-27)
binary: rustc
commit-hash: c9f8f3438a8134a413aa5d4903e0196e44e37bbc
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2
Additional Labels
No response
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 at the redundant_closure_for_method_calls lint and reproduce the issue with the raw-identifier snippet in the report. Trace how the lint builds its suggested method path, then add a regression test covering raw identifiers. Done means both the diagnostic and clippy fix preserve the required r# syntax.
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