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

New lint: prefer bool::then #6760

Closed
magurotuna opened this issue Feb 18, 2021 · 3 comments · Fixed by #6859
Closed

New lint: prefer bool::then #6760

magurotuna opened this issue Feb 18, 2021 · 3 comments · Fixed by #6859
Assignees
Labels
A-lint Area: New lints

Comments

@magurotuna
Copy link
Contributor

magurotuna commented Feb 18, 2021

What it does

Checks for if-else that could be rewritten to bool::then

Categories (optional)

  • Kind: style restriction

Reduces redundancy and lines of code by leveraging a new stable API bool::then

Drawbacks

None.

Example

let a = if foo {
    println!("hello foo!");
    Some(bar())
} else {
    None
};

Could be written as:

let a = foo.then(|| {
    println!("hello foo!");
    bar()
});

Note that the following cannot be rewritten using bool::then because it has some action in else block:

let a = if foo {
    println!("hello foo!");
    Some(bar())
} else {
    println!("else!");
    None
};
@magurotuna magurotuna added the A-lint Area: New lints label Feb 18, 2021
@magurotuna
Copy link
Contributor Author

I would like to implement it myself. If there's any concern or negative opinion on this lint, please let me know.
@rustbot claim

@camsteffen
Copy link
Contributor

We definitely should have this. But I have a feeling that bool::then is "a little too functional" for some people and so it should not warn-by-default. So either pedantic or restriction. I would lean towards restriction.

@magurotuna
Copy link
Contributor Author

Sounds reasonable. I'm in favor of choosing "restriction" too. I'll go with it.

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

Successfully merging a pull request may close this issue.

2 participants