Skip to content

Fix semicolon_outside_block suggests wrongly when inside macros #14954

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
Jun 2, 2025

Conversation

profetia
Copy link
Contributor

@profetia profetia commented Jun 2, 2025

Closes #14926

changelog: [semicolon_outside_block] fix wrong suggestions when inside macros

@rustbot
Copy link
Collaborator

rustbot commented Jun 2, 2025

r? @samueltardieu

rustbot has assigned @samueltardieu.
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 Jun 2, 2025
Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

I have a hard-time convincing myself that all this is necessary:

  • !block.span.from_expansion() is already checked in SemicolonBlock::check_stmt()
  • It looks like adding && stmt.span.contains(block.span) into the guard in SemicolonBlock::check_stmt() passes all the tests you add

Can you think of other test cases that would pass with your changes and fail with the proposed guard?

@rustbot rustbot 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 Jun 2, 2025
@profetia
Copy link
Contributor Author

profetia commented Jun 2, 2025

You are right about the first point. But for the second one, I think in SemicolonBlock::check_stmt() is not doing the same thing. What I'm doing here is checking the surrounding of the block to determine if it is safe to move the ; out, while that method is checking inside the block.

mac!(
    { <- block
        another_mac!(); // <- stmt
    }
);

@samueltardieu
Copy link
Contributor

I think in SemicolonBlock::check_stmt() is not doing the same thing.

I'm not sure I understand: your code is called by check_stmt().

What I'm doing here is checking the surrounding of the block to determine if it is safe to move the ; out, while that method is checking inside the block.

This is why I was asking of a test case where my proposed addition to the guard in check_stmt() is not enough, in order to convince me that this does not cover every case we are interested in not linting.

@samueltardieu
Copy link
Contributor

This is why I was asking of a test case where my proposed addition to the guard in check_stmt() is not enough, in order to convince me that this does not cover every case we are interested in not linting.

To make sure I am perfectly clear, what I am saying is that if I take the original code from master and changes it to:

    fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
        match stmt.kind {
            StmtKind::Expr(Expr {
                kind: ExprKind::Block(block, _),
                ..
            }) if !block.span.from_expansion() && stmt.span.contains(block.span) => {

(note the added && stmt.span.contains(block.span))

then all the uitests pass, including the ones you have added in the current PR. So I am wondering where the behavior difference might be.

@profetia
Copy link
Contributor Author

profetia commented Jun 2, 2025

Oh I see, I've added the guard to the wrong place. In this case, I think your idea is right. I'm updating this PR to this better version. Thank you!

@samueltardieu samueltardieu added this pull request to the merge queue Jun 2, 2025
Merged via the queue into rust-lang:master with commit 88bb8d1 Jun 2, 2025
11 checks passed
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.

FP semicolon_outside_block: macro
3 participants