Skip to content

Commit 2a96bc4

Browse files
committed
Auto merge of #7067 - TaKO8Ki:fix-false-negative-on-needless-return, r=llogiq
Fix a false negative on `needless return` closes #7042 changelog: fix a false negative on `needless return`
2 parents 411c0df + e6c67ad commit 2a96bc4

File tree

4 files changed

+268
-31
lines changed

4 files changed

+268
-31
lines changed

clippy_lints/src/returns.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ fn check_final_expr<'tcx>(
223223
},
224224
_ => (),
225225
},
226+
ExprKind::DropTemps(expr) => check_final_expr(cx, expr, None, RetReplacement::Empty),
226227
_ => (),
227228
}
228229
}

tests/ui/needless_return.fixed

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// edition:2018
23

34
#![allow(unused)]
45
#![allow(
@@ -125,10 +126,85 @@ mod issue6501 {
125126
}
126127
}
127128

128-
fn main() {
129-
let _ = test_end_of_fn();
130-
let _ = test_no_semicolon();
131-
let _ = test_if_block();
132-
let _ = test_match(true);
133-
test_closure();
129+
async fn async_test_end_of_fn() -> bool {
130+
if true {
131+
// no error!
132+
return true;
133+
}
134+
true
135+
}
136+
137+
async fn async_test_no_semicolon() -> bool {
138+
true
139+
}
140+
141+
async fn async_test_if_block() -> bool {
142+
if true {
143+
true
144+
} else {
145+
false
146+
}
147+
}
148+
149+
async fn async_test_match(x: bool) -> bool {
150+
match x {
151+
true => false,
152+
false => {
153+
true
154+
},
155+
}
156+
}
157+
158+
async fn async_test_closure() {
159+
let _ = || {
160+
true
161+
};
162+
let _ = || true;
163+
}
164+
165+
async fn async_test_macro_call() -> i32 {
166+
return the_answer!();
167+
}
168+
169+
async fn async_test_void_fun() {
170+
171+
}
172+
173+
async fn async_test_void_if_fun(b: bool) {
174+
if b {
175+
176+
} else {
177+
178+
}
179+
}
180+
181+
async fn async_test_void_match(x: u32) {
182+
match x {
183+
0 => (),
184+
_ => {},
185+
}
186+
}
187+
188+
async fn async_read_line() -> String {
189+
use std::io::BufRead;
190+
let stdin = ::std::io::stdin();
191+
return stdin.lock().lines().next().unwrap().unwrap();
134192
}
193+
194+
async fn async_borrows_but_not_last(value: bool) -> String {
195+
if value {
196+
use std::io::BufRead;
197+
let stdin = ::std::io::stdin();
198+
let _a = stdin.lock().lines().next().unwrap().unwrap();
199+
String::from("test")
200+
} else {
201+
String::new()
202+
}
203+
}
204+
205+
async fn async_test_return_in_macro() {
206+
needed_return!(10);
207+
needed_return!(0);
208+
}
209+
210+
fn main() {}

tests/ui/needless_return.rs

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// edition:2018
23

34
#![allow(unused)]
45
#![allow(
@@ -125,10 +126,85 @@ mod issue6501 {
125126
}
126127
}
127128

128-
fn main() {
129-
let _ = test_end_of_fn();
130-
let _ = test_no_semicolon();
131-
let _ = test_if_block();
132-
let _ = test_match(true);
133-
test_closure();
129+
async fn async_test_end_of_fn() -> bool {
130+
if true {
131+
// no error!
132+
return true;
133+
}
134+
return true;
135+
}
136+
137+
async fn async_test_no_semicolon() -> bool {
138+
return true;
139+
}
140+
141+
async fn async_test_if_block() -> bool {
142+
if true {
143+
return true;
144+
} else {
145+
return false;
146+
}
147+
}
148+
149+
async fn async_test_match(x: bool) -> bool {
150+
match x {
151+
true => return false,
152+
false => {
153+
return true;
154+
},
155+
}
156+
}
157+
158+
async fn async_test_closure() {
159+
let _ = || {
160+
return true;
161+
};
162+
let _ = || return true;
163+
}
164+
165+
async fn async_test_macro_call() -> i32 {
166+
return the_answer!();
167+
}
168+
169+
async fn async_test_void_fun() {
170+
return;
171+
}
172+
173+
async fn async_test_void_if_fun(b: bool) {
174+
if b {
175+
return;
176+
} else {
177+
return;
178+
}
179+
}
180+
181+
async fn async_test_void_match(x: u32) {
182+
match x {
183+
0 => (),
184+
_ => return,
185+
}
186+
}
187+
188+
async fn async_read_line() -> String {
189+
use std::io::BufRead;
190+
let stdin = ::std::io::stdin();
191+
return stdin.lock().lines().next().unwrap().unwrap();
134192
}
193+
194+
async fn async_borrows_but_not_last(value: bool) -> String {
195+
if value {
196+
use std::io::BufRead;
197+
let stdin = ::std::io::stdin();
198+
let _a = stdin.lock().lines().next().unwrap().unwrap();
199+
return String::from("test");
200+
} else {
201+
return String::new();
202+
}
203+
}
204+
205+
async fn async_test_return_in_macro() {
206+
needed_return!(10);
207+
needed_return!(0);
208+
}
209+
210+
fn main() {}

0 commit comments

Comments
 (0)