Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
flip1995 opened this issue Jul 31, 2019 · 0 comments
Labels
A-lint Area: New lints L-complexity Lint: Belongs in the complexity lint group L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@flip1995
Copy link
Member

flip1995 commented Jul 31, 2019

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
@flip1995 flip1995 added L-complexity Lint: Belongs in the complexity lint group L-suggestion Lint: Improving, adding or fixing lint suggestions A-lint Area: New lints labels Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints L-complexity Lint: Belongs in the complexity lint group L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

1 participant