Skip to content

autofix for redundant_else lint #13936

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

Merged
merged 1 commit into from
Jan 30, 2025

Conversation

lapla-cogito
Copy link
Contributor

@lapla-cogito lapla-cogito commented Jan 3, 2025

changelog: [redundant_else]: autofix for some cases

redundant_else can be fixed automatically.

@rustbot
Copy link
Collaborator

rustbot commented Jan 3, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 3, 2025
@lapla-cogito lapla-cogito force-pushed the autofix_redundant_else branch from e395c27 to 8ade7a6 Compare January 3, 2025 16:32
@samueltardieu
Copy link
Member

What if the else block declares variables? Not only will they be dragged into the outer scope, but also their dropping will be deferred until the end of the outer scope, which may be problematic in case of a guard.

For example, the PR fails with this code:

fn with_drop(t: bool) -> u32 {
    let a = 42;
    if t {
        return 0;
    } else {
        let a = "foobar";
        println!("this a = {a}");
    }
    a + 1
}

because let a = "foobar"; has been dragged into the outer scope and hides the outer a. Here the example is constructed specially to exhibit the failure, but with a mutex guard we might have real issues.

Maybe the suggestion should apply only when no bindings are introduced inside the else block, even though the lint could warn about the redundant else.

@samueltardieu
Copy link
Member

Another (very minor) issue is that if the fix belongs to a macro, the indentation shown will be the one of the macro call, not the proper one, e.g.,

macro_rules! mac {
    ($t:expr) => {{
        if $t {
            return 0;
        } else {
            return 42;
        }
    }}
}

fn call_macro() -> u32 {
                       mac!(true)
}

will suggest using

macro_rules! mac {
    ($t:expr) => {{
        if $t {
            return 0;
        }
                       return 42;
    }}
}

@lapla-cogito lapla-cogito marked this pull request as draft January 5, 2025 05:35
@lapla-cogito lapla-cogito force-pushed the autofix_redundant_else branch from f397659 to edf3828 Compare January 8, 2025 21:56
@lapla-cogito lapla-cogito marked this pull request as ready for review January 8, 2025 22:25
@lapla-cogito
Copy link
Contributor Author

r? clippy

@rustbot rustbot assigned blyxyas and unassigned Jarcho Jan 12, 2025
@lapla-cogito lapla-cogito force-pushed the autofix_redundant_else branch from 2bd7c1d to 9e8ffc8 Compare January 12, 2025 08:07
@lapla-cogito
Copy link
Contributor Author

Commits are now squashed.

@lapla-cogito
Copy link
Contributor Author

note: I'd like to hear what the reviewers think about this issue, i.e., since I think this is usually a (very) minor issue, I'd like a decision on whether to merge this with this issue remaining (but clearly stated as a doc comment) or take some other action.

@samueltardieu
Copy link
Member

note: I'd like to hear what the reviewers think about this issue, i.e., since I think this is usually a (very) minor issue, I'd like a decision on whether to merge this with this issue remaining (but clearly stated as a doc comment) or take some other action.

This is so niche that I don't think it can block the patch (unless there is a very easy solution to fix this aesthetic issue).

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Very good patch, it will be very useful. Thanks for the contribution!

@lapla-cogito lapla-cogito force-pushed the autofix_redundant_else branch from 9e8ffc8 to 7aae4f4 Compare January 19, 2025 23:13
@lapla-cogito lapla-cogito requested a review from blyxyas January 20, 2025 00:56
@lapla-cogito lapla-cogito changed the title auto-fix for redundant_else lint autofix for redundant_else lint Jan 29, 2025
@lapla-cogito
Copy link
Contributor Author

@blyxyas Would you review again?

Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Just a single nit and this can be merged! Sorry for the long wait, I've been kinda busy.

@blyxyas blyxyas enabled auto-merge January 29, 2025 21:28
@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 29, 2025
@rustbot

This comment has been minimized.

@blyxyas blyxyas disabled auto-merge January 29, 2025 21:30
@blyxyas
Copy link
Member

blyxyas commented Jan 29, 2025

I'll fix this, I didn't think pushing the "Update branch" button would do such a thing. If anyone else with merge rights sees this comment do not merge

@blyxyas blyxyas force-pushed the autofix_redundant_else branch from b907ed9 to 7aae4f4 Compare January 29, 2025 21:34
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 29, 2025
@blyxyas blyxyas added this pull request to the merge queue Jan 30, 2025
Merged via the queue into rust-lang:master with commit 88a00a8 Jan 30, 2025
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants