Skip to content

Commit

Permalink
Fix needless_return with let-else
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Sep 18, 2021
1 parent 59cd777 commit b365912
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
5 changes: 4 additions & 1 deletion clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustc_middle::hir::map::Map;
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::hygiene::DesugaringKind;
use rustc_span::source_map::Span;
use rustc_span::sym;

Expand Down Expand Up @@ -199,7 +200,9 @@ fn check_final_expr<'tcx>(
check_block_return(cx, ifblock);
}
if let Some(else_clause) = else_clause_opt {
check_final_expr(cx, else_clause, None, RetReplacement::Empty);
if expr.span.desugaring_kind() != Some(DesugaringKind::LetElse) {
check_final_expr(cx, else_clause, None, RetReplacement::Empty);
}
}
},
// a match expr, check all arms
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// run-rustfix
// edition:2018

#![feature(let_else)]
#![allow(unused)]
#![allow(
clippy::if_same_then_else,
clippy::single_match,
clippy::branches_sharing_code,
clippy::needless_bool
)]
#![allow(clippy::if_same_then_else, clippy::single_match, clippy::needless_bool)]
#![warn(clippy::needless_return)]

macro_rules! the_answer {
Expand Down Expand Up @@ -207,4 +203,8 @@ async fn async_test_return_in_macro() {
needed_return!(0);
}

fn let_else() {
let Some(1) = Some(1) else { return };
}

fn main() {}
12 changes: 6 additions & 6 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// run-rustfix
// edition:2018

#![feature(let_else)]
#![allow(unused)]
#![allow(
clippy::if_same_then_else,
clippy::single_match,
clippy::branches_sharing_code,
clippy::needless_bool
)]
#![allow(clippy::if_same_then_else, clippy::single_match, clippy::needless_bool)]
#![warn(clippy::needless_return)]

macro_rules! the_answer {
Expand Down Expand Up @@ -207,4 +203,8 @@ async fn async_test_return_in_macro() {
needed_return!(0);
}

fn let_else() {
let Some(1) = Some(1) else { return };
}

fn main() {}
64 changes: 32 additions & 32 deletions tests/ui/needless_return.stderr
Original file line number Diff line number Diff line change
@@ -1,193 +1,193 @@
error: unneeded `return` statement
--> $DIR/needless_return.rs:24:5
--> $DIR/needless_return.rs:20:5
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`
|
= note: `-D clippy::needless-return` implied by `-D warnings`

error: unneeded `return` statement
--> $DIR/needless_return.rs:28:5
--> $DIR/needless_return.rs:24:5
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:33:9
--> $DIR/needless_return.rs:29:9
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:35:9
--> $DIR/needless_return.rs:31:9
|
LL | return false;
| ^^^^^^^^^^^^^ help: remove `return`: `false`

error: unneeded `return` statement
--> $DIR/needless_return.rs:41:17
--> $DIR/needless_return.rs:37:17
|
LL | true => return false,
| ^^^^^^^^^^^^ help: remove `return`: `false`

error: unneeded `return` statement
--> $DIR/needless_return.rs:43:13
--> $DIR/needless_return.rs:39:13
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:50:9
--> $DIR/needless_return.rs:46:9
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:52:16
--> $DIR/needless_return.rs:48:16
|
LL | let _ = || return true;
| ^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:60:5
--> $DIR/needless_return.rs:56:5
|
LL | return;
| ^^^^^^^ help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:65:9
--> $DIR/needless_return.rs:61:9
|
LL | return;
| ^^^^^^^ help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:67:9
--> $DIR/needless_return.rs:63:9
|
LL | return;
| ^^^^^^^ help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:74:14
--> $DIR/needless_return.rs:70:14
|
LL | _ => return,
| ^^^^^^ help: replace `return` with an empty block: `{}`

error: unneeded `return` statement
--> $DIR/needless_return.rs:89:9
--> $DIR/needless_return.rs:85:9
|
LL | return String::from("test");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")`

error: unneeded `return` statement
--> $DIR/needless_return.rs:91:9
--> $DIR/needless_return.rs:87:9
|
LL | return String::new();
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`

error: unneeded `return` statement
--> $DIR/needless_return.rs:112:32
--> $DIR/needless_return.rs:108:32
|
LL | bar.unwrap_or_else(|_| return)
| ^^^^^^ help: replace `return` with an empty block: `{}`

error: unneeded `return` statement
--> $DIR/needless_return.rs:117:13
--> $DIR/needless_return.rs:113:13
|
LL | return;
| ^^^^^^^ help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:119:20
--> $DIR/needless_return.rs:115:20
|
LL | let _ = || return;
| ^^^^^^ help: replace `return` with an empty block: `{}`

error: unneeded `return` statement
--> $DIR/needless_return.rs:125:32
--> $DIR/needless_return.rs:121:32
|
LL | res.unwrap_or_else(|_| return Foo)
| ^^^^^^^^^^ help: remove `return`: `Foo`

error: unneeded `return` statement
--> $DIR/needless_return.rs:134:5
--> $DIR/needless_return.rs:130:5
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:138:5
--> $DIR/needless_return.rs:134:5
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:143:9
--> $DIR/needless_return.rs:139:9
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:145:9
--> $DIR/needless_return.rs:141:9
|
LL | return false;
| ^^^^^^^^^^^^^ help: remove `return`: `false`

error: unneeded `return` statement
--> $DIR/needless_return.rs:151:17
--> $DIR/needless_return.rs:147:17
|
LL | true => return false,
| ^^^^^^^^^^^^ help: remove `return`: `false`

error: unneeded `return` statement
--> $DIR/needless_return.rs:153:13
--> $DIR/needless_return.rs:149:13
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:160:9
--> $DIR/needless_return.rs:156:9
|
LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:162:16
--> $DIR/needless_return.rs:158:16
|
LL | let _ = || return true;
| ^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:170:5
--> $DIR/needless_return.rs:166:5
|
LL | return;
| ^^^^^^^ help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:175:9
--> $DIR/needless_return.rs:171:9
|
LL | return;
| ^^^^^^^ help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:177:9
--> $DIR/needless_return.rs:173:9
|
LL | return;
| ^^^^^^^ help: remove `return`

error: unneeded `return` statement
--> $DIR/needless_return.rs:184:14
--> $DIR/needless_return.rs:180:14
|
LL | _ => return,
| ^^^^^^ help: replace `return` with an empty block: `{}`

error: unneeded `return` statement
--> $DIR/needless_return.rs:199:9
--> $DIR/needless_return.rs:195:9
|
LL | return String::from("test");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")`

error: unneeded `return` statement
--> $DIR/needless_return.rs:201:9
--> $DIR/needless_return.rs:197:9
|
LL | return String::new();
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
Expand Down

0 comments on commit b365912

Please sign in to comment.