Skip to content

Commit b247594

Browse files
committed
Prevent some false positives
1 parent f30d7c2 commit b247594

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

clippy_lints/src/methods/iter_once_empty.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_lang_ctor;
3-
use clippy_utils::is_no_std_crate;
42
use clippy_utils::source::snippet;
3+
use clippy_utils::{get_expr_use_or_unification_node, is_lang_ctor, is_no_std_crate};
54

65
use rustc_errors::Applicability;
76
use rustc_hir::LangItem::{OptionNone, OptionSome};
8-
use rustc_hir::{Expr, ExprKind};
7+
use rustc_hir::{Expr, ExprKind, Node};
98
use rustc_lint::LateContext;
109

1110
use super::{ITER_EMPTY, ITER_ONCE};
@@ -56,6 +55,24 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, method_name:
5655
_ => return,
5756
};
5857

58+
let is_unified = match get_expr_use_or_unification_node(cx.tcx, expr) {
59+
Some((Node::Expr(parent), child_id)) => match parent.kind {
60+
ExprKind::If(e, _, _) | ExprKind::Match(e, _, _) if e.hir_id == child_id => false,
61+
ExprKind::If(_, _, _)
62+
| ExprKind::Match(_, _, _)
63+
| ExprKind::Closure(_)
64+
| ExprKind::Ret(_)
65+
| ExprKind::Break(_, _) => true,
66+
_ => false,
67+
},
68+
Some((Node::Stmt(_) | Node::Local(_), _)) => false,
69+
_ => true,
70+
};
71+
72+
if is_unified {
73+
return;
74+
}
75+
5976
if let Some(i) = item {
6077
let sugg = format!(
6178
"{}::iter::once({}{})",

clippy_lints/src/types/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,6 @@ impl Types {
490490
}
491491
}
492492
}
493-
#[allow(clippy::iter_empty)]
494493
match *qpath {
495494
QPath::Resolved(Some(ty), p) => {
496495
context.is_nested_call = true;

clippy_utils/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ pub fn def_path_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
484484
}
485485
fn find_primitive<'tcx>(tcx: TyCtxt<'tcx>, name: &str) -> impl Iterator<Item = DefId> + 'tcx {
486486
let single = |ty| tcx.incoherent_impls(ty).iter().copied();
487-
#[allow(clippy::iter_empty)]
488487
let empty = || [].iter().copied();
489488
match name {
490489
"bool" => single(BoolSimplifiedType),

tests/ui/iter_empty.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ fn array() {
1313
// Don't trigger on non-iter methods
1414
let _: Option<String> = None.clone();
1515
let _: [String; 0] = [].clone();
16+
17+
// Don't trigger on match or if branches
18+
let _ = match 123 {
19+
123 => [].iter(),
20+
_ => ["test"].iter(),
21+
};
22+
23+
let _ = if false { ["test"].iter() } else { [].iter() };
1624
}
1725

1826
macro_rules! in_macros {

tests/ui/iter_empty.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ fn array() {
1313
// Don't trigger on non-iter methods
1414
let _: Option<String> = None.clone();
1515
let _: [String; 0] = [].clone();
16+
17+
// Don't trigger on match or if branches
18+
let _ = match 123 {
19+
123 => [].iter(),
20+
_ => ["test"].iter(),
21+
};
22+
23+
let _ = if false { ["test"].iter() } else { [].iter() };
1624
}
1725

1826
macro_rules! in_macros {

tests/ui/iter_once.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ fn array() {
1313
// Don't trigger on non-iter methods
1414
let _: Option<String> = Some("test".to_string()).clone();
1515
let _: [String; 1] = ["test".to_string()].clone();
16+
17+
// Don't trigger on match or if branches
18+
let _ = match 123 {
19+
123 => [].iter(),
20+
_ => ["test"].iter(),
21+
};
22+
23+
let _ = if false { ["test"].iter() } else { [].iter() };
1624
}
1725

1826
macro_rules! in_macros {

tests/ui/iter_once.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ fn array() {
1313
// Don't trigger on non-iter methods
1414
let _: Option<String> = Some("test".to_string()).clone();
1515
let _: [String; 1] = ["test".to_string()].clone();
16+
17+
// Don't trigger on match or if branches
18+
let _ = match 123 {
19+
123 => [].iter(),
20+
_ => ["test"].iter(),
21+
};
22+
23+
let _ = if false { ["test"].iter() } else { [].iter() };
1624
}
1725

1826
macro_rules! in_macros {

0 commit comments

Comments
 (0)