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

Single match else #579

Merged
merged 2 commits into from Feb 1, 2016
Merged

Single match else #579

merged 2 commits into from Feb 1, 2016

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Jan 24, 2016

fixes #172

@@ -112,34 +112,40 @@ impl LateLintPass for MatchPass {
fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
if arms.len() == 2 &&
arms[0].pats.len() == 1 && arms[0].guard.is_none() &&
arms[1].pats.len() == 1 && arms[1].guard.is_none() &&
is_unit_expr(&arms[1].body) {
arms[1].pats.len() == 1 && arms[1].guard.is_none() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should have an Allow lint for matches with an else block? I don't think one style is clearly preferred over the other, cc @llogiq .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm also not sure about this – on one hand the match statement has one more level of nesting, which may lead to more line breaks which hurt readability, on the other hand if let will often lead to one more line of very sparse code, which takes more vertical space then necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conclusion is: add a lint that suggests turning regular if/else into a match on a boolean xD

One thing I have noticed is that I find statements much more readable with if let and expressions with match

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the match arm is a (multiline) block, the if let version takes up one line less:

match x {
    Y => println!("Jupiter"),
    _ => {
        println!("Mars");
        println!("Venus");
    },
}

vs

if let Y = x {
    println!("Jupiter");
} else {
    println!("Mars");
    println!("Venus");
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. But I'm more worried about

if let Some(x) = whaa {
    x
} else {
    0
}

which would obviously better be written as whaa.unwrap_or(0)...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change the check to only show up if there's no else arm or if any of the arms is a block with at least a statement and an expression

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

@Manishearth
Copy link
Member

On the bikeshed issue someone pointed out that this is a matter of personal style. I think we should just unconditionally allow (as in, a pedantic lint) matches with an else block.

@oli-obk
Copy link
Contributor Author

oli-obk commented Feb 1, 2016

I have added a new lint and made it allow by default. The new lint is also much better than before. There is now no (imo) controversial fallout in clippy.

Manishearth added a commit that referenced this pull request Feb 1, 2016
@Manishearth Manishearth merged commit fd906c0 into rust-lang:master Feb 1, 2016
@oli-obk oli-obk deleted the single_match_else branch February 1, 2016 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

single_match suggestion should handle else blocks
3 participants