rust-lang / rust-lang/rust-clippy

Lint idea: Bind if/else returning bool and then using that binding to conditionally do something

Open
#4,311 0 comments 0 reactions 0 assignees View on GitHub

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:

  1. Differ between if and if let: Suggest rewriting it as one if in the first case and as a match in the second*.
  2. Between the binding and the usage of the binding is no other use of the binding.
  3. Get the conditions right. -> Write many test cases with all combinations of conditions and returns.
  4. Maybe this lint should only trigger on if/else blocks returning true/false, without doing anything else inside?
  5. I couldn't come up with a lint name, so be creative :)

cc #4308

  • Could conflict with the single_match_else lint, but this is a pedantic lint, so we can ignore this

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.