Prevent some false positives

This commit is contained in:
Sosthène Guédon 2022-07-28 22:54:23 +02:00
parent f30d7c2495
commit b247594a39
7 changed files with 52 additions and 5 deletions

View File

@ -1,11 +1,10 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_lang_ctor;
use clippy_utils::is_no_std_crate;
use clippy_utils::source::snippet;
use clippy_utils::{get_expr_use_or_unification_node, is_lang_ctor, is_no_std_crate};
use rustc_errors::Applicability;
use rustc_hir::LangItem::{OptionNone, OptionSome};
use rustc_hir::{Expr, ExprKind};
use rustc_hir::{Expr, ExprKind, Node};
use rustc_lint::LateContext;
use super::{ITER_EMPTY, ITER_ONCE};
@ -56,6 +55,24 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, method_name:
_ => return,
};
let is_unified = match get_expr_use_or_unification_node(cx.tcx, expr) {
Some((Node::Expr(parent), child_id)) => match parent.kind {
ExprKind::If(e, _, _) | ExprKind::Match(e, _, _) if e.hir_id == child_id => false,
ExprKind::If(_, _, _)
| ExprKind::Match(_, _, _)
| ExprKind::Closure(_)
| ExprKind::Ret(_)
| ExprKind::Break(_, _) => true,
_ => false,
},
Some((Node::Stmt(_) | Node::Local(_), _)) => false,
_ => true,
};
if is_unified {
return;
}
if let Some(i) = item {
let sugg = format!(
"{}::iter::once({}{})",

View File

@ -490,7 +490,6 @@ fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, mut context:
}
}
}
#[allow(clippy::iter_empty)]
match *qpath {
QPath::Resolved(Some(ty), p) => {
context.is_nested_call = true;

View File

@ -484,7 +484,6 @@ fn item_child_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: &str) -> Option<Res>
}
fn find_primitive<'tcx>(tcx: TyCtxt<'tcx>, name: &str) -> impl Iterator<Item = DefId> + 'tcx {
let single = |ty| tcx.incoherent_impls(ty).iter().copied();
#[allow(clippy::iter_empty)]
let empty = || [].iter().copied();
match name {
"bool" => single(BoolSimplifiedType),

View File

@ -13,6 +13,14 @@ fn array() {
// Don't trigger on non-iter methods
let _: Option<String> = None.clone();
let _: [String; 0] = [].clone();
// Don't trigger on match or if branches
let _ = match 123 {
123 => [].iter(),
_ => ["test"].iter(),
};
let _ = if false { ["test"].iter() } else { [].iter() };
}
macro_rules! in_macros {

View File

@ -13,6 +13,14 @@ fn array() {
// Don't trigger on non-iter methods
let _: Option<String> = None.clone();
let _: [String; 0] = [].clone();
// Don't trigger on match or if branches
let _ = match 123 {
123 => [].iter(),
_ => ["test"].iter(),
};
let _ = if false { ["test"].iter() } else { [].iter() };
}
macro_rules! in_macros {

View File

@ -13,6 +13,14 @@ fn array() {
// Don't trigger on non-iter methods
let _: Option<String> = Some("test".to_string()).clone();
let _: [String; 1] = ["test".to_string()].clone();
// Don't trigger on match or if branches
let _ = match 123 {
123 => [].iter(),
_ => ["test"].iter(),
};
let _ = if false { ["test"].iter() } else { [].iter() };
}
macro_rules! in_macros {

View File

@ -13,6 +13,14 @@ fn array() {
// Don't trigger on non-iter methods
let _: Option<String> = Some("test".to_string()).clone();
let _: [String; 1] = ["test".to_string()].clone();
// Don't trigger on match or if branches
let _ = match 123 {
123 => [].iter(),
_ => ["test"].iter(),
};
let _ = if false { ["test"].iter() } else { [].iter() };
}
macro_rules! in_macros {