Open
Description
Lint name: match_single_binding
I tried this code:
fn main() {
match format_args!("hello {}", "world") {
f => dbg!(f),
};
}
Clippy prints a warning:
warning: this match could be written as a `let` statement
With a proposal:
let f = format_args!("hello {}", "world");
dbg!(f);
But suggested code does not compile, because f
is bound to a temporary value and its lifetime ends before dbg!
.
Meta
cargo clippy -V
: 0.1.51 (2021-01-20 a4cbb44)rustc -Vv
:
rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-unknown-linux-gnu
release: 1.49.0
Metadata
Metadata
Assignees
Labels
Category: Clippy is not doing the correct thingCall for participation: This a hard problem and requires more experience or effort to work onIssue: The lint was triggered on code it shouldn't haveIssue: The suggestions provided by this Lint cause an ICE/error when appliedType: This lint will require working with the MIR
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
camsteffen commentedon Feb 5, 2021
I tried to fix this with
HIR
andScopeTree
to no avail. I think this will require MIR analysis. Of course we could special-caseformat_args!
but I think that is too rare of a use-case for that.Fishrock123 commentedon Apr 12, 2021
Just ran into this with
todo!()
, which seems very silly:camsteffen commentedon Apr 12, 2021
I don't think it would be worth it to special-case
todo!
since that indicates incomplete code by definition.Fishrock123 commentedon Apr 13, 2021
Oh, on re-reading this maybe this is a separate issue. I think it should not warn in the case of
todo!
. I think it would be worth it, that should be highlighted in different ways.bgeron commentedon Apr 1, 2022
I just ran into the same issue.
To be honest — matching with a single match arm is a bit silly,
let
is normally meant for that. And if you put a block in the match, now you have two indentation levels!It seems nice to have different syntax for this use case. For instance:
But the lint needs a fix irrespective of any new syntax.