Skip to content

Fix for needless_return replaces no-value returns with blank lines #9416

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

Closed
64kramsystem opened this issue Sep 2, 2022 · 3 comments · Fixed by #9967
Closed

Fix for needless_return replaces no-value returns with blank lines #9416

64kramsystem opened this issue Sep 2, 2022 · 3 comments · Fixed by #9967
Assignees
Labels
L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@64kramsystem
Copy link

64kramsystem commented Sep 2, 2022

Description

When fixing needless_return, if the return statement has no value, clippy will fix it by replacing it with a blank line. Specifically, the blank line is a series of spaces whose ending matches the start of .
I guess clippy is not considering such case, so it prepares the leading space for the value, but then it doesn't add anything, since there isn't any value to add.

Example diff (it's not visible; select the text with the mouse in order to visualize it):

@@ -1072,7 +1072,7 @@ fn gargthink(gs: &mut GlobalState, pas: &mut PcrlibAState, pcs: &mut PcrlibCStat
             }
             _ => {}
         }
-        return;
+        
     } else {
         chasethink(false, gs, pas, pcs);
     };
@@ -1109,7 +1109,7 @@ fn dragonthink(gs: &mut GlobalState, pas: &mut PcrlibAState, pcs: &mut PcrlibCSt
             }
             _ => {}
         }
-        return;
+        
     } else {
         chasethink(false, gs, pas, pcs);
     };

Version

rustc 1.64.0-nightly (fe3342816 2022-08-01)
binary: rustc
commit-hash: fe3342816a282949f014caa05ea2e669ff9d3d3c
commit-date: 2022-08-01
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

Additional Labels

No response

@dswij dswij added the L-suggestion Lint: Improving, adding or fixing lint suggestions label Sep 5, 2022
@dswij
Copy link
Member

dswij commented Sep 5, 2022

I believe this is because when we make the suggestion, we use only the span of return. The spaces before it are outside of said span, and thus will be untouched by the lint.

Not sure if this would be a trivial fix.

@llogiq
Copy link
Contributor

llogiq commented Sep 5, 2022

I think it's not exactly trivial, but the two steps are

  1. Detect if we return unit (get the type, check if unit)
  2. Get the Span .lo, go backwards while encountering whitespace, extend the lint Span to the new .lo

@koka831
Copy link
Contributor

koka831 commented Nov 27, 2022

@rustbot claim

bors added a commit that referenced this issue Nov 27, 2022
Remove blank lines when needless_return returns no value

fix #9416

changelog: [`needless_return`] improve result format

r? `@llogiq`
@bors bors closed this as completed in 8c50dfb Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants