rust-lang / rust-lang/rust-clippy
FP single_match_else (lifetimes)
Open
Nobody has claimed this yet.
C-bug
I-false-positive
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
.
Lint Name
single_match_else
Reproducer
I tried this code:
#![warn(clippy::single_match_else)]
use std::marker::PhantomData;
struct Inv<'a>(PhantomData<*mut &'a ()>);
impl PartialEq for Inv<'static> {
fn eq(&self, _: &Inv<'static>) -> bool {
true
}
}
impl<'a> Inv<'a> {
const NOT_STATIC: Option<Self> = None;
}
fn foo<'a>(x: Option<Inv<'a>>) {
match x {
Inv::<'a>::NOT_STATIC => (),
Some(_) => panic!()
}
}
fn main() {
foo(None)
}
I saw this happen:
warning: you seem to be trying to use `match` for an equality check. Consider using `if`
--> src/main.rs:17:5
|
17 | / match x {
18 | | Inv::<'a>::NOT_STATIC => (),
19 | | Some(_) => panic!()
20 | | }
| |_____^ help: try: `if x == Inv::<'a>::NOT_STATIC { () } else { panic!() }`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match_else
note: the lint level is defined here
--> src/main.rs:1:9
|
1 | #![warn(clippy::single_match_else)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
suggested code:
#![warn(clippy::single_match_else)]
use std::marker::PhantomData;
struct Inv<'a>(PhantomData<*mut &'a ()>);
impl PartialEq for Inv<'static> {
fn eq(&self, _: &Inv<'static>) -> bool {
true
}
}
impl<'a> Inv<'a> {
const NOT_STATIC: Option<Self> = None;
}
fn foo<'a>(x: Option<Inv<'a>>) {
if x == Inv::<'a>::NOT_STATIC { () } else { panic!() }
}
fn main() {
foo(None)
}
does not compile:
error: lifetime may not live long enough
--> src/main.rs:17:8
|
16 | fn foo<'a>(x: Option<Inv<'a>>) {
| -- lifetime `'a` defined here
17 | if x == Inv::<'a>::NOT_STATIC { () } else { panic!() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
error: could not compile `f` (bin "f") due to 1 p
Version
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 by locating the single_match_else lint entry point and the code that generates its suggested replacement, then reproduce the issue with the Rust snippet in the report. Done means the lint no longer emits a suggestion that fails to compile for this lifetime case, with a regression test covering the reproducer.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100