rust-lang / rust-lang/rust-clippy
`redundant_closure_for_method_calls` does not respect certain lifetime requirements
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Clippy wrongly identifies a closure as redundant when higher-ranked lifetimes are involved.
Lint Name
redundant_closure_for_method_calls
Reproducer
I tried this code:
#![deny(clippy::redundant_closure_for_method_calls)]
#![allow(dead_code)]
fn f() {
g(|s| s.m())
}
fn g(_: impl FnOnce(&mut S<'_>)) {}
struct S<'a>(&'a ());
impl S<'_> {
fn m(&mut self) {}
}
Clippy suggests me to replace |s| s.m() with S::m:
error: redundant closure
--> src/lib.rs:5:7
|
5 | g(|s| s.m())
| ^^^^^^^^^ help: replace the closure with the method itself: `S::m`
|
note: the lint level is defined here
--> src/lib.rs:1:9
|
1 | #![deny(clippy::redundant_closure_for_method_calls)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
error: could not compile `playground` due to previous error
However, following clippy's advice leads rustc to emit the following error:
error[E0308]: mismatched types
--> src/lib.rs:5:5
|
5 | g(S::m)
| ^ one type is more general than the other
|
= note: expected trait `for<'r, 's> FnOnce<(&'r mut S<'s>,)>`
found trait `for<'r> FnOnce<(&'r mut S<'_>,)>`
note: the lifetime requirement is introduced here
--> src/lib.rs:8:14
|
8 | fn g(_: impl FnOnce(&mut S<'_>)) {}
| ^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
I expect clippy to not emit any errors.
Version
This happens both on stable and on nightly.
Nightly (My Local Setup)
rustc 1.65.0-nightly (29e4a9ee0 2022-08-10)
binary: rustc
commit-hash: 29e4a9ee0253cd39e552a77f51f11f9a5f1c41e6
commit-date: 2022-08-10
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 14.0.6
clippy 0.1.64 (29e4a9e 2022-08-10)
Stable (The Playground)
rustc 1.63.0
clippy 0.1.65 (2022-08-13 75b7e52)
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 higher-ranked-lifetime example from the issue. Trace how the lint compares the closure with S::m, then verify that the lint no longer suggests the replacement when it produces the shown rustc type error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100