Skip to content

Commit 595b478

Browse files
committed
use derefs_to_slice in methods/utils.rs
1 parent 00c0670 commit 595b478

File tree

6 files changed

+6
-46
lines changed

6 files changed

+6
-46
lines changed

clippy_lints/src/methods/get_unwrap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::methods::derefs_to_slice;
1+
use crate::methods::utils::derefs_to_slice;
22
use crate::utils::{get_parent_expr, paths};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::source::snippet_with_applicability;

clippy_lints/src/methods/iter_cloned_collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::methods::derefs_to_slice;
1+
use crate::methods::utils::derefs_to_slice;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use if_chain::if_chain;

clippy_lints/src/methods/iter_count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::methods::derefs_to_slice;
1+
use crate::methods::utils::derefs_to_slice;
22
use crate::utils::paths;
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::source::snippet_with_applicability;

clippy_lints/src/methods/iter_next_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::methods::derefs_to_slice;
1+
use crate::methods::utils::derefs_to_slice;
22
use crate::utils::{get_parent_expr, higher};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::source::snippet_with_applicability;

clippy_lints/src/methods/iter_nth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::methods::derefs_to_slice;
21
use crate::methods::iter_nth_zero;
32
use clippy_utils::diagnostics::span_lint_and_help;
43
use clippy_utils::ty::is_type_diagnostic_item;
4+
use crate::methods::utils::derefs_to_slice;
55
use rustc_hir as hir;
66
use rustc_lint::LateContext;
77
use rustc_span::symbol::sym;

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mod unnecessary_fold;
5353
mod unnecessary_lazy_eval;
5454
mod unwrap_used;
5555
mod useless_asref;
56+
mod utils;
5657
mod wrong_self_convention;
5758
mod zst_offset;
5859

@@ -1977,47 +1978,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
19771978

19781979
extract_msrv_attr!(LateContext);
19791980
}
1980-
1981-
fn derefs_to_slice<'tcx>(
1982-
cx: &LateContext<'tcx>,
1983-
expr: &'tcx hir::Expr<'tcx>,
1984-
ty: Ty<'tcx>,
1985-
) -> Option<&'tcx hir::Expr<'tcx>> {
1986-
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
1987-
match ty.kind() {
1988-
ty::Slice(_) => true,
1989-
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
1990-
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
1991-
ty::Array(_, size) => size
1992-
.try_eval_usize(cx.tcx, cx.param_env)
1993-
.map_or(false, |size| size < 32),
1994-
ty::Ref(_, inner, _) => may_slice(cx, inner),
1995-
_ => false,
1996-
}
1997-
}
1998-
1999-
if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind {
2000-
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
2001-
Some(&args[0])
2002-
} else {
2003-
None
2004-
}
2005-
} else {
2006-
match ty.kind() {
2007-
ty::Slice(_) => Some(expr),
2008-
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
2009-
ty::Ref(_, inner, _) => {
2010-
if may_slice(cx, inner) {
2011-
Some(expr)
2012-
} else {
2013-
None
2014-
}
2015-
},
2016-
_ => None,
2017-
}
2018-
}
2019-
}
2020-
20211981
/// Used for `lint_binary_expr_with_method_call`.
20221982
#[derive(Copy, Clone)]
20231983
struct BinaryExprInfo<'a> {

0 commit comments

Comments
 (0)