rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Fix for `needless_return` replaces no-value returns with blank lines

Open 64kramsystem opened this issue 3 years ago • 2 comments

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

64kramsystem avatar Sep 02 '22 10:09 64kramsystem

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.

dswij avatar Sep 05 '22 08:09 dswij

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

llogiq avatar Sep 05 '22 18:09 llogiq

@rustbot claim

koka831 avatar Nov 27 '22 12:11 koka831