Skip to content

Make lint span smaller for needless return #14790

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
May 20, 2025

Conversation

barun511
Copy link
Contributor

@barun511 barun511 commented May 12, 2025

Fixes #14750 by reducing the lint span for needless_return.

changelog: [needless_return]: Lint span no longer wraps to previous line

@rustbot
Copy link
Collaborator

rustbot commented May 12, 2025

r? @dswij

rustbot has assigned @dswij.
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 May 12, 2025
@barun511 barun511 force-pushed the parruck/fix-lint-span branch from ffddadf to 73a19a0 Compare May 12, 2025 21:59
@@ -448,6 +455,7 @@ fn check_final_expr<'tcx>(

fn emit_return_lint(
cx: &LateContext<'_>,
lint_span: Span,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not super confident that this is the way we want to go - but it seemed like the lint_span and the suggestion_span had to diverge here. Happy to take suggestions!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally the suggestion span would be created inside this function, but that doesn't look like it will be trivial.

@@ -84,14 +84,14 @@ fn test_macro_call() -> i32 {
}

fn test_void_fun() {
//~^^ needless_return
//~^ needless_return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to change these because the lint span now starts on the immediate previous line, not the one before

|
LL | fn test_void_fun() {
| _____________________^
LL | | return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not entirely convinced that we shouldn't extend this span upto at least the full line?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe extend the span to the ;. Not sure why it wasn't included in the previous span though. There might've been a reason.

Might need some logic like "if parent statement is a StmtKind::Semi, extend the span to the statement" 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

The semicolon spans are handled separately in the suggestion since there may be multiple of them.

@@ -608,10 +590,10 @@ LL + let _ = 42;
|

error: unneeded `return` statement
--> tests/ui/needless_return.rs:371:20
--> tests/ui/needless_return.rs:371:21
|
LL | let _ = 42; return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example in this instance I think the previous span was slightly better. But as I don't have high conviction, leaving as is for now and we can address

Copy link
Contributor

Choose a reason for hiding this comment

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

I definitely think the new span is better. Linting on a return but pointing to whitespace is just weird and doesn't match how basically every other lint/error uses spans. It's also just extra work to extend the span.

@barun511
Copy link
Contributor Author

Still have some tests to fix - but I'll get to those once there's alignment on approach

Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

Thank you. Just needs an error marker fixed.

@barun511
Copy link
Contributor Author

Thank you! I won't be able to get to it until Tuesday, probably, but will fix the tests then

@Jarcho Jarcho added this pull request to the merge queue May 20, 2025
Merged via the queue into rust-lang:master with commit f00c58b May 20, 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-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Needlessly Verbose Replacement Suggestion for needless_return
5 participants