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

Remove blank lines when needless_return returns no value #9967

Merged
merged 2 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
use rustc_span::BytePos;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -209,13 +210,14 @@ fn check_final_expr<'tcx>(
if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
if !borrows {
emit_return_lint(
cx,
peeled_drop_expr.span,
semi_spans,
inner.as_ref().map(|i| i.span),
replacement,
);
// check if expr return nothing
let ret_span = if inner.is_none() && replacement == RetReplacement::Empty {
extend_span_to_previous_non_ws(cx, peeled_drop_expr.span)
} else {
peeled_drop_expr.span
};

emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement);
}
}
},
Expand Down Expand Up @@ -289,3 +291,17 @@ fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>)
})
.is_some()
}

// Go backwards while encountering whitespace and extend the given Span to that point.
#[expect(clippy::cast_possible_truncation)]
fn extend_span_to_previous_non_ws(cx: &LateContext<'_>, sp: Span) -> Span {
if let Ok(prev_source) = cx.sess().source_map().span_to_prev_source(sp) {
let ws = [' ', '\t', '\n'];
if let Some(non_ws_pos) = prev_source.rfind(|c| !ws.contains(&c)) {
let len = prev_source.len() - non_ws_pos - 1;
return sp.with_lo(BytePos(sp.lo().0 - len as u32));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return sp.with_lo(BytePos(sp.lo().0 - len as u32));
return sp.with_lo(sp.lo() - BytePos::from_usize(len));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your quick response.
I resolved it in 39d0477.

}
}

sp
}
19 changes: 11 additions & 8 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ fn test_macro_call() -> i32 {
}

fn test_void_fun() {

}

fn test_void_if_fun(b: bool) {
if b {

} else {

}
}

Expand All @@ -82,7 +79,6 @@ fn test_nested_match(x: u32) {
0 => (),
1 => {
let _ = 42;

},
_ => (),
}
Expand Down Expand Up @@ -126,7 +122,6 @@ mod issue6501 {

fn test_closure() {
let _ = || {

};
let _ = || {};
}
Expand Down Expand Up @@ -179,14 +174,11 @@ async fn async_test_macro_call() -> i32 {
}

async fn async_test_void_fun() {

}

async fn async_test_void_if_fun(b: bool) {
if b {

} else {

}
}

Expand Down Expand Up @@ -269,4 +261,15 @@ fn issue9503(x: usize) -> isize {
}
}

mod issue9416 {
pub fn with_newline() {
let _ = 42;
}

#[rustfmt::skip]
pub fn oneline() {
let _ = 42;
}
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,17 @@ fn issue9503(x: usize) -> isize {
};
}

mod issue9416 {
pub fn with_newline() {
let _ = 42;

return;
}

#[rustfmt::skip]
pub fn oneline() {
let _ = 42; return;
}
}

fn main() {}
85 changes: 60 additions & 25 deletions tests/ui/needless_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,32 @@ LL | return the_answer!();
= help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:62:5
--> $DIR/needless_return.rs:61:21
|
LL | return;
| ^^^^^^
LL | fn test_void_fun() {
| _____________________^
LL | | return;
| |__________^
|
= help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:67:9
--> $DIR/needless_return.rs:66:11
|
LL | return;
| ^^^^^^
LL | if b {
| ___________^
LL | | return;
| |______________^
|
= help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:69:9
--> $DIR/needless_return.rs:68:13
|
LL | return;
| ^^^^^^
LL | } else {
| _____________^
LL | | return;
| |______________^
|
= help: remove `return`

Expand All @@ -104,10 +110,12 @@ LL | _ => return,
= help: replace `return` with a unit value

error: unneeded `return` statement
--> $DIR/needless_return.rs:85:13
--> $DIR/needless_return.rs:84:24
|
LL | return;
| ^^^^^^
LL | let _ = 42;
| ________________________^
LL | | return;
| |__________________^
|
= help: remove `return`

Expand Down Expand Up @@ -144,10 +152,12 @@ LL | bar.unwrap_or_else(|_| return)
= help: replace `return` with an empty block

error: unneeded `return` statement
--> $DIR/needless_return.rs:129:13
--> $DIR/needless_return.rs:128:21
|
LL | return;
| ^^^^^^
LL | let _ = || {
| _____________________^
LL | | return;
| |__________________^
|
= help: remove `return`

Expand Down Expand Up @@ -240,26 +250,32 @@ LL | return the_answer!();
= help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:182:5
--> $DIR/needless_return.rs:181:33
|
LL | return;
| ^^^^^^
LL | async fn async_test_void_fun() {
| _________________________________^
LL | | return;
| |__________^
|
= help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:187:9
--> $DIR/needless_return.rs:186:11
|
LL | return;
| ^^^^^^
LL | if b {
| ___________^
LL | | return;
| |______________^
|
= help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:189:9
--> $DIR/needless_return.rs:188:13
|
LL | return;
| ^^^^^^
LL | } else {
| _____________^
LL | | return;
| |______________^
|
= help: remove `return`

Expand Down Expand Up @@ -351,5 +367,24 @@ LL | return !*(x as *const isize);
|
= help: remove `return`

error: aborting due to 44 previous errors
error: unneeded `return` statement
--> $DIR/needless_return.rs:274:20
|
LL | let _ = 42;
| ____________________^
LL | |
LL | | return;
| |______________^
|
= help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:281:20
|
LL | let _ = 42; return;
| ^^^^^^^
|
= help: remove `return`

error: aborting due to 46 previous errors