use derefs_to_slice in methods/utils.rs
This commit is contained in:
parent
e578a536c7
commit
1bec8b6065
6 changed files with 6 additions and 46 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use crate::methods::derefs_to_slice;
|
||||
use super::utils::derefs_to_slice;
|
||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::source::snippet_with_applicability;
|
||||
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::methods::derefs_to_slice;
|
||||
use crate::methods::utils::derefs_to_slice;
|
||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::ty::is_type_diagnostic_item;
|
||||
use if_chain::if_chain;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::methods::derefs_to_slice;
|
||||
use super::utils::derefs_to_slice;
|
||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::paths;
|
||||
use clippy_utils::source::snippet_with_applicability;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::methods::derefs_to_slice;
|
||||
use crate::methods::utils::derefs_to_slice;
|
||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::source::snippet_with_applicability;
|
||||
use clippy_utils::ty::is_type_diagnostic_item;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::methods::derefs_to_slice;
|
||||
use crate::methods::iter_nth_zero;
|
||||
use clippy_utils::diagnostics::span_lint_and_help;
|
||||
use clippy_utils::ty::is_type_diagnostic_item;
|
||||
use crate::methods::utils::derefs_to_slice;
|
||||
use rustc_hir as hir;
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_span::symbol::sym;
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ mod unnecessary_fold;
|
|||
mod unnecessary_lazy_eval;
|
||||
mod unwrap_used;
|
||||
mod useless_asref;
|
||||
mod utils;
|
||||
mod wrong_self_convention;
|
||||
mod zst_offset;
|
||||
|
||||
|
|
@ -1976,47 +1977,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
|
|||
|
||||
extract_msrv_attr!(LateContext);
|
||||
}
|
||||
|
||||
fn derefs_to_slice<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
expr: &'tcx hir::Expr<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
) -> Option<&'tcx hir::Expr<'tcx>> {
|
||||
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
|
||||
match ty.kind() {
|
||||
ty::Slice(_) => true,
|
||||
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
|
||||
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
|
||||
ty::Array(_, size) => size
|
||||
.try_eval_usize(cx.tcx, cx.param_env)
|
||||
.map_or(false, |size| size < 32),
|
||||
ty::Ref(_, inner, _) => may_slice(cx, inner),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind {
|
||||
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
|
||||
Some(&args[0])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
match ty.kind() {
|
||||
ty::Slice(_) => Some(expr),
|
||||
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
|
||||
ty::Ref(_, inner, _) => {
|
||||
if may_slice(cx, inner) {
|
||||
Some(expr)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Used for `lint_binary_expr_with_method_call`.
|
||||
#[derive(Copy, Clone)]
|
||||
struct BinaryExprInfo<'a> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue