Skip to content

Commit c297174

Browse files
committed
export derefs_to_slice from methods module
1 parent 77907e6 commit c297174

File tree

3 files changed

+48
-49
lines changed

3 files changed

+48
-49
lines changed

clippy_lints/src/methods/iter_count.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::utils::{
2-
derefs_to_slice, is_type_diagnostic_item, match_type, paths, snippet_with_applicability, span_lint_and_sugg,
3-
};
1+
use crate::methods::derefs_to_slice;
2+
use crate::utils::{is_type_diagnostic_item, match_type, paths, snippet_with_applicability, span_lint_and_sugg};
43

54
use rustc_errors::Applicability;
65
use rustc_hir::Expr;

clippy_lints/src/methods/mod.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ use crate::consts::{constant, Constant};
3333
use crate::utils::eager_or_lazy::is_lazyness_candidate;
3434
use crate::utils::usage::mutated_variables;
3535
use crate::utils::{
36-
contains_return, contains_ty, derefs_to_slice, get_parent_expr, get_trait_def_id, has_iter_method, higher,
37-
implements_trait, in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment,
38-
match_def_path, match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args,
39-
path_to_local_id, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
40-
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs,
41-
sugg, walk_ptrs_ty_depth, SpanlessEq,
36+
contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
37+
in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path,
38+
match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths,
39+
remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite,
40+
span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, sugg, walk_ptrs_ty_depth,
41+
SpanlessEq,
4242
};
4343

4444
declare_clippy_lint! {
@@ -2803,6 +2803,46 @@ fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[
28032803
}
28042804
}
28052805

2806+
pub(crate) fn derefs_to_slice<'tcx>(
2807+
cx: &LateContext<'tcx>,
2808+
expr: &'tcx hir::Expr<'tcx>,
2809+
ty: Ty<'tcx>,
2810+
) -> Option<&'tcx hir::Expr<'tcx>> {
2811+
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
2812+
match ty.kind() {
2813+
ty::Slice(_) => true,
2814+
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
2815+
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
2816+
ty::Array(_, size) => size
2817+
.try_eval_usize(cx.tcx, cx.param_env)
2818+
.map_or(false, |size| size < 32),
2819+
ty::Ref(_, inner, _) => may_slice(cx, inner),
2820+
_ => false,
2821+
}
2822+
}
2823+
2824+
if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind {
2825+
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
2826+
Some(&args[0])
2827+
} else {
2828+
None
2829+
}
2830+
} else {
2831+
match ty.kind() {
2832+
ty::Slice(_) => Some(expr),
2833+
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
2834+
ty::Ref(_, inner, _) => {
2835+
if may_slice(cx, inner) {
2836+
Some(expr)
2837+
} else {
2838+
None
2839+
}
2840+
},
2841+
_ => None,
2842+
}
2843+
}
2844+
}
2845+
28062846
/// lint use of `unwrap()` for `Option`s and `Result`s
28072847
fn lint_unwrap(cx: &LateContext<'_>, expr: &hir::Expr<'_>, unwrap_args: &[hir::Expr<'_>]) {
28082848
let obj_ty = cx.typeck_results().expr_ty(&unwrap_args[0]).peel_refs();

clippy_utils/src/lib.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,46 +1802,6 @@ pub fn is_some_ctor(cx: &LateContext<'_>, res: Res) -> bool {
18021802
false
18031803
}
18041804

1805-
pub fn derefs_to_slice<'tcx>(
1806-
cx: &LateContext<'tcx>,
1807-
expr: &'tcx hir::Expr<'tcx>,
1808-
ty: Ty<'tcx>,
1809-
) -> Option<&'tcx hir::Expr<'tcx>> {
1810-
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
1811-
match ty.kind() {
1812-
ty::Slice(_) => true,
1813-
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
1814-
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
1815-
ty::Array(_, size) => size
1816-
.try_eval_usize(cx.tcx, cx.param_env)
1817-
.map_or(false, |size| size < 32),
1818-
ty::Ref(_, inner, _) => may_slice(cx, inner),
1819-
_ => false,
1820-
}
1821-
}
1822-
1823-
if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind {
1824-
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
1825-
Some(&args[0])
1826-
} else {
1827-
None
1828-
}
1829-
} else {
1830-
match ty.kind() {
1831-
ty::Slice(_) => Some(expr),
1832-
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
1833-
ty::Ref(_, inner, _) => {
1834-
if may_slice(cx, inner) {
1835-
Some(expr)
1836-
} else {
1837-
None
1838-
}
1839-
},
1840-
_ => None,
1841-
}
1842-
}
1843-
}
1844-
18451805
#[cfg(test)]
18461806
mod test {
18471807
use super::{reindent_multiline, without_block_comments};

0 commit comments

Comments
 (0)