Skip to content

Commit 37e1b80

Browse files
authored
fix: manual_find suggests wrongly when early return (rust-lang#14405)
Closes rust-lang#9521 changelog: [`manual_find`]: fix wrong suggestions when early return
2 parents d5a6688 + 95d7281 commit 37e1b80

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

clippy_lints/src/loops/manual_find.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::utils::make_iterator_snippet;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::ty::implements_trait;
6+
use clippy_utils::usage::contains_return_break_continue_macro;
67
use clippy_utils::{higher, is_res_lang_ctor, path_res, peel_blocks_with_stmt};
78
use rustc_errors::Applicability;
89
use rustc_hir::def::Res;
@@ -35,6 +36,7 @@ pub(super) fn check<'tcx>(
3536
&& let ExprKind::Call(ctor, [inner_ret]) = ret_value.kind
3637
&& is_res_lang_ctor(cx, path_res(cx, ctor), LangItem::OptionSome)
3738
&& path_res(cx, inner_ret) == Res::Local(binding_id)
39+
&& !contains_return_break_continue_macro(cond)
3840
&& let Some((last_stmt, last_ret)) = last_stmt_and_ret(cx, expr)
3941
{
4042
let mut applicability = Applicability::MachineApplicable;

tests/ui/manual_find.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,32 @@ fn tuple(arr: Vec<(String, i32)>) -> Option<String> {
2323
None
2424
}
2525

26+
mod issue9521 {
27+
fn condition(x: u32, y: u32) -> Result<bool, ()> {
28+
todo!()
29+
}
30+
31+
fn find_with_early_return(v: Vec<u32>) -> Option<u32> {
32+
for x in v {
33+
if condition(x, 10).ok()? {
34+
return Some(x);
35+
}
36+
}
37+
None
38+
}
39+
40+
fn find_with_early_break(v: Vec<u32>) -> Option<u32> {
41+
for x in v {
42+
if if x < 3 {
43+
break;
44+
} else {
45+
x < 10
46+
} {
47+
return Some(x);
48+
}
49+
}
50+
None
51+
}
52+
}
53+
2654
fn main() {}

0 commit comments

Comments
 (0)