mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
Prevent some false positives
This commit is contained in:
parent
f30d7c2495
commit
b247594a39
7 changed files with 52 additions and 5 deletions
|
@ -1,11 +1,10 @@
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
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::source::snippet;
|
||||||
|
use clippy_utils::{get_expr_use_or_unification_node, is_lang_ctor, is_no_std_crate};
|
||||||
|
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
||||||
use rustc_hir::{Expr, ExprKind};
|
use rustc_hir::{Expr, ExprKind, Node};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
|
|
||||||
use super::{ITER_EMPTY, ITER_ONCE};
|
use super::{ITER_EMPTY, ITER_ONCE};
|
||||||
|
@ -56,6 +55,24 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, method_name:
|
||||||
_ => return,
|
_ => 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 {
|
if let Some(i) = item {
|
||||||
let sugg = format!(
|
let sugg = format!(
|
||||||
"{}::iter::once({}{})",
|
"{}::iter::once({}{})",
|
||||||
|
|
|
@ -490,7 +490,6 @@ impl Types {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[allow(clippy::iter_empty)]
|
|
||||||
match *qpath {
|
match *qpath {
|
||||||
QPath::Resolved(Some(ty), p) => {
|
QPath::Resolved(Some(ty), p) => {
|
||||||
context.is_nested_call = true;
|
context.is_nested_call = true;
|
||||||
|
|
|
@ -484,7 +484,6 @@ pub fn def_path_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
|
||||||
}
|
}
|
||||||
fn find_primitive<'tcx>(tcx: TyCtxt<'tcx>, name: &str) -> impl Iterator<Item = DefId> + 'tcx {
|
fn find_primitive<'tcx>(tcx: TyCtxt<'tcx>, name: &str) -> impl Iterator<Item = DefId> + 'tcx {
|
||||||
let single = |ty| tcx.incoherent_impls(ty).iter().copied();
|
let single = |ty| tcx.incoherent_impls(ty).iter().copied();
|
||||||
#[allow(clippy::iter_empty)]
|
|
||||||
let empty = || [].iter().copied();
|
let empty = || [].iter().copied();
|
||||||
match name {
|
match name {
|
||||||
"bool" => single(BoolSimplifiedType),
|
"bool" => single(BoolSimplifiedType),
|
||||||
|
|
|
@ -13,6 +13,14 @@ fn array() {
|
||||||
// Don't trigger on non-iter methods
|
// Don't trigger on non-iter methods
|
||||||
let _: Option<String> = None.clone();
|
let _: Option<String> = None.clone();
|
||||||
let _: [String; 0] = [].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 {
|
macro_rules! in_macros {
|
||||||
|
|
|
@ -13,6 +13,14 @@ fn array() {
|
||||||
// Don't trigger on non-iter methods
|
// Don't trigger on non-iter methods
|
||||||
let _: Option<String> = None.clone();
|
let _: Option<String> = None.clone();
|
||||||
let _: [String; 0] = [].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 {
|
macro_rules! in_macros {
|
||||||
|
|
|
@ -13,6 +13,14 @@ fn array() {
|
||||||
// Don't trigger on non-iter methods
|
// Don't trigger on non-iter methods
|
||||||
let _: Option<String> = Some("test".to_string()).clone();
|
let _: Option<String> = Some("test".to_string()).clone();
|
||||||
let _: [String; 1] = ["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 {
|
macro_rules! in_macros {
|
||||||
|
|
|
@ -13,6 +13,14 @@ fn array() {
|
||||||
// Don't trigger on non-iter methods
|
// Don't trigger on non-iter methods
|
||||||
let _: Option<String> = Some("test".to_string()).clone();
|
let _: Option<String> = Some("test".to_string()).clone();
|
||||||
let _: [String; 1] = ["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 {
|
macro_rules! in_macros {
|
||||||
|
|
Loading…
Reference in a new issue