|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::msrvs::{self, Msrv}; |
| 4 | +use clippy_utils::sugg::Sugg; |
| 5 | +use clippy_utils::visitors::is_const_evaluatable; |
| 6 | +use clippy_utils::{is_in_const_context, is_mutable, is_trait_method}; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{Expr, ExprKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_session::impl_lint_pass; |
| 11 | +use rustc_span::sym; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// |
| 16 | + /// Checks for slice references with cloned references such as `&[f.clone()]`. |
| 17 | + /// |
| 18 | + /// ### Why is this bad |
| 19 | + /// |
| 20 | + /// A reference does not need to be owned in order to used as a slice. |
| 21 | + /// |
| 22 | + /// ### Known problems |
| 23 | + /// |
| 24 | + /// This lint does not know whether or not a clone implementation has side effects. |
| 25 | + /// |
| 26 | + /// ### Example |
| 27 | + /// |
| 28 | + /// ```ignore |
| 29 | + /// let data = 10; |
| 30 | + /// let data_ref = &data; |
| 31 | + /// take_slice(&[data_ref.clone()]); |
| 32 | + /// ``` |
| 33 | + /// Use instead: |
| 34 | + /// ```ignore |
| 35 | + /// use std::slice; |
| 36 | + /// let data = 10; |
| 37 | + /// let data_ref = &data; |
| 38 | + /// take_slice(slice::from_ref(data_ref)); |
| 39 | + /// ``` |
| 40 | + #[clippy::version = "1.87.0"] |
| 41 | + pub CLONED_REF_TO_SLICE_REFS, |
| 42 | + perf, |
| 43 | + "cloning a reference for slice references" |
| 44 | +} |
| 45 | + |
| 46 | +pub struct ClonedRefToSliceRefs<'a> { |
| 47 | + msrv: &'a Msrv, |
| 48 | +} |
| 49 | +impl<'a> ClonedRefToSliceRefs<'a> { |
| 50 | + pub fn new(conf: &'a Conf) -> Self { |
| 51 | + Self { msrv: &conf.msrv } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl_lint_pass!(ClonedRefToSliceRefs<'_> => [CLONED_REF_TO_SLICE_REFS]); |
| 56 | + |
| 57 | +impl<'tcx> LateLintPass<'tcx> for ClonedRefToSliceRefs<'_> { |
| 58 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 59 | + if self.msrv.meets(cx, { |
| 60 | + if is_in_const_context(cx) { |
| 61 | + msrvs::CONST_SLICE_FROM_REF |
| 62 | + } else { |
| 63 | + msrvs::SLICE_FROM_REF |
| 64 | + } |
| 65 | + }) |
| 66 | + // `&[foo.clone()]` expressions |
| 67 | + && let ExprKind::AddrOf(_, mutability, arr) = &expr.kind |
| 68 | + // mutable references would have a different meaning |
| 69 | + && mutability.is_not() |
| 70 | + |
| 71 | + // check for single item arrays |
| 72 | + && let ExprKind::Array([item]) = &arr.kind |
| 73 | + |
| 74 | + // check for clones |
| 75 | + && let ExprKind::MethodCall(_, val, _, _) = item.kind |
| 76 | + && is_trait_method(cx, item, sym::Clone) |
| 77 | + |
| 78 | + // check for immutability or purity |
| 79 | + && (!is_mutable(cx, val) || is_const_evaluatable(cx, val)) |
| 80 | + |
| 81 | + // get appropriate crate for `slice::from_ref` |
| 82 | + && let Some(builtin_crate) = clippy_utils::std_or_core(cx) |
| 83 | + { |
| 84 | + let mut sugg = Sugg::hir(cx, val, "_"); |
| 85 | + if !cx.typeck_results().expr_ty(val).is_ref() { |
| 86 | + sugg = sugg.addr(); |
| 87 | + } |
| 88 | + |
| 89 | + span_lint_and_sugg( |
| 90 | + cx, |
| 91 | + CLONED_REF_TO_SLICE_REFS, |
| 92 | + expr.span, |
| 93 | + format!("this call to `clone` can be replaced with `{builtin_crate}::slice::from_ref`"), |
| 94 | + "try", |
| 95 | + format!("{builtin_crate}::slice::from_ref({sugg})"), |
| 96 | + Applicability::MaybeIncorrect, |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments