Skip to content

Commit

Permalink
early return removed
Browse files Browse the repository at this point in the history
  • Loading branch information
jrqc committed Aug 15, 2020
1 parent 96efaee commit baa4cb1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
4 changes: 0 additions & 4 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ fn check_final_expr<'tcx>(
span: Option<Span>,
replacement: RetReplacement,
) {
if last_statement_borrows(cx, expr) {
return;
}

match expr.kind {
// simple return is always "bad"
ExprKind::Ret(ref inner) => {
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/unused_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ declare_clippy_lint! {
/// less readable. Depending on formatting they can make a `break` or `return`
/// statement look like a function call.
///
/// **Known problems:** The lint currently misses unit return types in types,
/// e.g., the `F` in `fn generic_unit<F: Fn() -> ()>(f: F) { .. }`.
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
Expand Down
30 changes: 13 additions & 17 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,20 @@ fn test_void_match(x: u32) {
}
}

mod no_lint_if_stmt_borrows {
mod issue_5858 {
fn read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
return stdin.lock().lines().next().unwrap().unwrap();
}
fn read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
return stdin.lock().lines().next().unwrap().unwrap();
}

fn read_line2(value: bool) -> String {
if value {
use std::io::BufRead;
let stdin = ::std::io::stdin();
let _a = stdin.lock().lines().next().unwrap().unwrap();
return String::from("test");
} else {
return String::new();
}
}
fn borrows_but_not_last(value: bool) -> String {
if value {
use std::io::BufRead;
let stdin = ::std::io::stdin();
let _a = stdin.lock().lines().next().unwrap().unwrap();
String::from("test")
} else {
String::new()
}
}

Expand Down
30 changes: 13 additions & 17 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,20 @@ fn test_void_match(x: u32) {
}
}

mod no_lint_if_stmt_borrows {
mod issue_5858 {
fn read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
return stdin.lock().lines().next().unwrap().unwrap();
}
fn read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
return stdin.lock().lines().next().unwrap().unwrap();
}

fn read_line2(value: bool) -> String {
if value {
use std::io::BufRead;
let stdin = ::std::io::stdin();
let _a = stdin.lock().lines().next().unwrap().unwrap();
return String::from("test");
} else {
return String::new();
}
}
fn borrows_but_not_last(value: bool) -> String {
if value {
use std::io::BufRead;
let stdin = ::std::io::stdin();
let _a = stdin.lock().lines().next().unwrap().unwrap();
return String::from("test");
} else {
return String::new();
}
}

Expand Down
14 changes: 13 additions & 1 deletion tests/ui/needless_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,17 @@ error: unneeded `return` statement
LL | _ => return,
| ^^^^^^ help: replace `return` with an empty block: `{}`

error: aborting due to 12 previous errors
error: unneeded `return` statement
--> $DIR/needless_return.rs:83:9
|
LL | return String::from("test");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")`

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

error: aborting due to 14 previous errors

0 comments on commit baa4cb1

Please sign in to comment.