Skip to content

[needless_return]: Remove all semicolons on suggestion #10187

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 2 commits into from
Jan 15, 2023

Conversation

dswij
Copy link
Member

@dswij dswij commented Jan 10, 2023

Closes #10182

Multiple semicolons currently breaks autofix for needless_return suggestions. Any semicolons left after removing return means that the return type will always be (), and thus fail to compile.

This PR allows needless_return to remove multiple semicolons.

The change won't cover the case where there is multiple line yet.

i.e.

fn needless_return() -> bool {
    return true;
;;
}

changelog: Sugg: [needless_return]: Now removes all semicolons on the same line
#10187

@rustbot
Copy link
Collaborator

rustbot commented Jan 10, 2023

r? @Jarcho

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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 10, 2023
@dswij dswij changed the title Issue 10182 semicolon [needless_return] suggestion now remove any number of semicolons on the same line. Jan 10, 2023
@dswij dswij changed the title [needless_return] suggestion now remove any number of semicolons on the same line. [needless_return]: Remove all semicolons on suggestion Jan 10, 2023
@Jarcho
Copy link
Contributor

Jarcho commented Jan 14, 2023

It would be better to use the lexer here. Start at the end of the statement, skip all whitespace/comment tokens, and take the offset of all the leading semicolons. You should also bail out of the lint if a # is found (e.g. ; #[cfg(...)] {}).

@dswij
Copy link
Member Author

dswij commented Jan 15, 2023

I specifically avoided the lexer because it has a wide definition of whitespace, e.g. line feed also falls into whitespace.

I'm keeping this PR fairly simple so it won't cover those unnecessary colons spanning multiple lines. Also, rustc lint will fire for these redundant semicolons and rustfmt will fix this. This change is just to allow running clippy fix before all of that. I'd argue that even this scenario is fairly uncommon.

You should also bail out of the lint if a # is found (e.g. ; #[cfg(...)] {}).

Maybe, yeah. I think this is out of the scope of this PR, but unreachable_code will fire here, and the user should address that first before considering clippy lints.

I also tested this out and seems like needless_return is already bailing out for these scenarios.

@Jarcho
Copy link
Contributor

Jarcho commented Jan 15, 2023

Also, rustc lint will fire for these redundant semicolons and rustfmt will fix this.

That's reasonable then. Given that the issue had come up at all I was assuming this wasn't the case.

@bors r+

@bors
Copy link
Contributor

bors commented Jan 15, 2023

📌 Commit d73adea has been approved by Jarcho

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jan 15, 2023

⌛ Testing commit d73adea with merge 07a7603...

@bors
Copy link
Contributor

bors commented Jan 15, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Jarcho
Pushing 07a7603 to master...

@bors bors merged commit 07a7603 into rust-lang:master Jan 15, 2023
@dswij dswij deleted the issue-10182-semicolon branch January 17, 2023 09:40
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.

clippy --fix fails for unnecessary return + unnecessary trailing semicolon
4 participants