Skip to content

Conversation

@A4-Tacks
Copy link
Member

@A4-Tacks A4-Tacks commented Jun 4, 2025

Removes the else keyword and else branches.

fn main() {
    if true {
        let _ = 2;
    } $0else {
        unreachable!();
    }
}

->

fn main() {
    if true {
        let _ = 2;
    }
}

fn main() {
    let _x = 2 $0else { unreachable!() };
}

->

fn main() {
    let _x = 2;
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 4, 2025
.and_then(|if_expr| if_expr.else_branch()?.syntax().clone().into())
.or_else(|| {
ctx.find_node_at_range::<ast::LetStmt>()?
.let_else()?
Copy link
Member

Choose a reason for hiding this comment

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

Should this be triggered on let else statements? I think most let else statements' else branch is not unreachable and removing them will result in compilation errors

Copy link
Member Author

Choose a reason for hiding this comment

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

Can be used for operations after refactoring into irrefutable pattern

@A4-Tacks A4-Tacks requested a review from ShoyuVanilla October 5, 2025 02:56
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

I think we can combine this with the unwrap block assist which has a similar purpose?

@A4-Tacks
Copy link
Member Author

I think we can combine this with the unwrap block assist which has a similar purpose?

I don't quite understand, what should I do?

@Veykril
Copy link
Member

Veykril commented Oct 26, 2025

We have this unwrap block assist here https://github.com/rust-analyzer/rust-analyzer/blob/c5181dbbe33af6f21b9d83e02fdb6fda298a3b65/crates/ide-assists/src/handlers/unwrap_block.rs

which I just realized already does what this assist does I believe? If you put the cursor on the else block's braces it pops up there

Though it does not trigger for let else yet

@A4-Tacks
Copy link
Member Author

We have this unwrap block assist here

If it is applicable to let-else, it may be strange:

For example:

let Some(x) = val else {$0 return false; };

Possible output one:

return false;

This seems niche enough

Possible output two:

let Some(x) = val;

This does not match the name of the unwrap block

Though it does not trigger for let else yet

Do I need to trigger remove_else_branches on the l_curly of let-else as well?

@Veykril
Copy link
Member

Veykril commented Oct 26, 2025

Ah right, the assist does have a different intent than this one, nevermind then.

@A4-Tacks
Copy link
Member Author

What do I need to do?

  • Move the assist of 'apply for let-else' to 'Unwrap block'?

    • Implement Possible output one?
    • Implement Possible output two?
  • Apply this assist to l_curly?

@Veykril
Copy link
Member

Veykril commented Oct 26, 2025

Nothing, disregard my earlier comment its not applicable to this after all.

@Veykril Veykril added this pull request to the merge queue Oct 26, 2025
Merged via the queue into rust-lang:master with commit 808c931 Oct 26, 2025
14 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 26, 2025
@A4-Tacks A4-Tacks deleted the remove-else-branches branch October 26, 2025 09:27
@lnicola lnicola changed the title Add ide-assist: remove else branches feat: add "Remove else branches" assist Oct 26, 2025
mendelsshop pushed a commit to mendelsshop/rust-analyzer that referenced this pull request Oct 27, 2025
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.

4 participants