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

Add Redundant else lint #6330

Merged
merged 2 commits into from
Dec 8, 2020
Merged

Add Redundant else lint #6330

merged 2 commits into from
Dec 8, 2020

Conversation

camsteffen
Copy link
Contributor

changelog: Add redundant_else lint

It seemed appropriate for "pedantic".

Closes #112 *blows off dust*

@rust-highfive
Copy link

r? @ebroto

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 13, 2020
@camsteffen camsteffen force-pushed the redundant-else branch 3 times, most recently from 78ea897 to 2d8798f Compare November 13, 2020 22:14
tests/ui/redundant_else.rs Outdated Show resolved Hide resolved
Copy link
Member

@ebroto ebroto left a comment

Choose a reason for hiding this comment

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

First of all sorry for the delay in reviewing, I didn't have much free time lately


I did not dive into the implementation itself, but I think I've found some problematic cases (suggestion would cause compile error) when the result of the linted if expression is used in another expression. Some examples:

    // Binary
    42 + if false {
        return;
    } else {
        21
    };

    // Unary
    -if false {
        return;
    } else {
        42
    };

    // Parens + Cast
    (if false {
        return;
    } else {
        42
    }) as i64;

    // Parameter to function call
    Box::new(if false {
        return;
    } else {
        42
    });

    // Array expression
    [if false {
        return;
    } else {
        42
    }];

    // Parens + Method call
    (if false {
        return;
    } else {
        42
    })
    .clone();

There are probably more cases like this. I'm not sure about the way to go to consistently avoid these... maybe we could simplify by linting only if the "parent" expression of the linted if is a block?

@ebroto ebroto added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Nov 24, 2020
@camsteffen
Copy link
Contributor Author

maybe we could simplify by linting only if the "parent" expression of the linted if is a block?

Good catch. Yeah that makes a lot of sense as I think about it.

@bors
Copy link
Collaborator

bors commented Nov 27, 2020

☔ The latest upstream changes (presumably #6389) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@camsteffen
Copy link
Contributor Author

It is much simpler now!

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Nov 29, 2020
Copy link
Member

@ebroto ebroto left a comment

Choose a reason for hiding this comment

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

LGTM mostly, but I would like to clarify something I don't understand before merging

clippy_lints/src/redundant_else.rs Show resolved Hide resolved
return;
}
// Only look at expressions that are a whole statement
let expr: &Expr = match &stmt.kind {
Copy link
Member

Choose a reason for hiding this comment

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

The type annotations do not seem to be needed (here and below)

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 type annotation coerces &P<Expr> to &Expr. I think this is preferable to doing &**expr inside the match?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, just removing the annotations compiles fine. I guess it could be related with match ergonomics being able to auto-deref if the pattern matches?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I think so.

  1. type annotations - auto-deref at binding - clear and smart-pointer-agnostic
  2. &**expr - manual deref before binding - less clear and smart-pointer-dependant
  3. neither - auto-deref at each usage - simplest code but inefficient

Copy link
Member

@ebroto ebroto Dec 8, 2020

Choose a reason for hiding this comment

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

I think it won't make a big difference, so I think it's fine to merge it as is. Thanks!


To explain my reasoning:

  1. I believe type annotations are generally less idiomatic if they are not needed to disambiguate (that's why I raised a concern).
  2. Correct me if I'm wrong, but I would say in this case using the annotation would just change the place where the dereference happens. From the let binding to the match/function call later, but I would say the number of derefs would be the same?

Copy link
Member

Choose a reason for hiding this comment

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

But yeah, I see your point, there would be cases where you could need more derefs than if you do it once when binding 👍

@ebroto ebroto added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Dec 7, 2020
@ebroto
Copy link
Member

ebroto commented Dec 8, 2020

@bors r+

Thanks!

@bors
Copy link
Collaborator

bors commented Dec 8, 2020

📌 Commit 70f6a2c has been approved by ebroto

@bors
Copy link
Collaborator

bors commented Dec 8, 2020

⌛ Testing commit 70f6a2c with merge 50bca8a...

@bors
Copy link
Collaborator

bors commented Dec 8, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: ebroto
Pushing 50bca8a to master...

@bors bors merged commit 50bca8a into rust-lang:master Dec 8, 2020
@camsteffen camsteffen deleted the redundant-else branch July 8, 2021 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

break; else / return; else
5 participants