Skip to content

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

Open
@flip1995

Description

@flip1995

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-complexityLint: Belongs in the complexity lint groupL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions