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

Warn about using bool::then_some followed by unwrap_or, suggest if/else instead #9100

Open
joshtriplett opened this issue Jul 2, 2022 · 3 comments
Labels
A-lint Area: New lints

Comments

@joshtriplett
Copy link
Member

joshtriplett commented Jul 2, 2022

What it does

Detects the combination of then_some followed by unwrap_or in place of a more clear if/else.

Lint Name

bool_if_else

Category

style

Advantage

  • Clarity at a glance

Drawbacks

No response

Example

let mode = executable.then_some(IsExecutable).unwrap_or(IsNotExecutable);

Could be written as:

let mode = if executable { IsExecutable } else { IsNotExecutable };
@joshtriplett joshtriplett added the A-lint Area: New lints label Jul 2, 2022
@joshtriplett
Copy link
Member Author

This should also apply with any combination of then/then_some and unwrap_or/unwrap_or_else.

@arieluy
Copy link
Contributor

arieluy commented Jul 10, 2022

@rustbot claim

bors added a commit that referenced this issue Jul 18, 2022
Add new lint `obfuscated_if_else`

part of #9100, additional commits could make it work with `then` and `unwrap_or_else` as well

changelog: Add new lint `obfuscated_if_else`
@arieluy
Copy link
Contributor

arieluy commented Jul 28, 2022

My commit adds the lint for then_some/unwrap_or, more commits needed to address other cases. I'll get to them at some point, but anyone can feel free to take them before me.

@arieluy arieluy removed their assignment Nov 21, 2022
github-merge-queue bot pushed a commit that referenced this issue Jan 25, 2025
part of #9100

The `obfuscated_if_else` lint currently only triggers for the pattern
`.then_some(..).unwrap_or(..)`, but there're other cases where this lint
should be triggered, one of which is `.then(..).unwrap_or(..)`.

changelog: [`obfuscated_if_else`]: trigger lint for the
`.then(..).unwrap_or(..)` pattern as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants