rust-lang / rust-lang/rust-clippy
Lint idea: Bind if/else returning bool and then using that binding to conditionally do something
Open
Nobody has claimed this yet.
A-lint
L-complexity
L-suggestion
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Lint idea:
let x = if [let]? _ {
{true,false}
} else {
{false,true}
}
// ... maybe some code not using x
if [!]?x {
do_something();
}
Can be rewritten as
match _ {
Some_::Thing(..) => do_something();
_ => {}
}
or
if _ {
do_something();
}
What to keep in mind when implementing this:
- Differ between
ifandif let: Suggest rewriting it as oneifin the first case and as amatchin the second*. - Between the binding and the usage of the binding is no other use of the binding.
- Get the conditions right. -> Write many test cases with all combinations of conditions and returns.
- Maybe this lint should only trigger on
if/elseblocks returningtrue/false, without doing anything else inside? - I couldn't come up with a lint name, so be creative :)
cc #4308
- Could conflict with the
single_match_elselint, but this is a pedantic lint, so we can ignore this
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
No file or test is named. Start by reviewing existing Clippy lint implementations and test conventions, then use the issue's if/else and if-let examples as cases. Done means the lint's name and exact trigger conditions are settled, including intervening uses and boolean-return combinations, with tests covering the listed cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100