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

fix span calculation for non-ascii in needless_return #12493

Merged
merged 1 commit into from Mar 15, 2024

Conversation

y21
Copy link
Member

@y21 y21 commented Mar 15, 2024

Fixes #12491

Probably fixes #12328 as well, but that one has no reproducer, so 🤷

The bug was that the lint used rfind() for finding the byte index of the start of the previous non-whitespace character:

// abc\n     return;
     ^

... then subtracting one to get the byte index of the actual whitespace (the \n here).
(Subtracting instead of adding because it treats this as the length from the return token to the \n)

That's correct for ascii, like here, and will get us to the \n, however for non ascii, the c could be multiple bytes wide, which would put us in the middle of a codepoint if we simply subtract 1 and is what caused the ICE.

There's probably a lot of ways we could fix this.
This PR changes it to iterate backwards using bytes instead of characters, so that when rposition() finally finds a non-whitespace byte, we know that we've skipped exactly 1 byte. This was probably(?) what the code was intending to do

changelog: Fix ICE in [needless_return] when previous line end in a non-ascii character

@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2024

r? @Alexendoo

rustbot has assigned @Alexendoo.
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 Mar 15, 2024
@Alexendoo
Copy link
Member

Yeah they're probably the same issue, byte wise seems like a nice way of handling it. Thanks!

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 15, 2024

📌 Commit d3f8f3e has been approved by Alexendoo

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Mar 15, 2024

⌛ Testing commit d3f8f3e with merge c9f2482...

@bors
Copy link
Collaborator

bors commented Mar 15, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Alexendoo
Pushing c9f2482 to master...

@bors bors merged commit c9f2482 into rust-lang:master Mar 15, 2024
5 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
4 participants