]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/redundant_slicing.rs
Rollup merge of #92021 - woodenarrow:br_single_fp_element, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / clippy_lints / src / redundant_slicing.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::get_parent_expr;
3 use clippy_utils::source::snippet_with_context;
4 use clippy_utils::ty::is_type_lang_item;
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::{BorrowKind, Expr, ExprKind, LangItem, Mutability};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::ty::TyS;
10 use rustc_session::{declare_lint_pass, declare_tool_lint};
11
12 declare_clippy_lint! {
13     /// ### What it does
14     /// Checks for redundant slicing expressions which use the full range, and
15     /// do not change the type.
16     ///
17     /// ### Why is this bad?
18     /// It unnecessarily adds complexity to the expression.
19     ///
20     /// ### Known problems
21     /// If the type being sliced has an implementation of `Index<RangeFull>`
22     /// that actually changes anything then it can't be removed. However, this would be surprising
23     /// to people reading the code and should have a note with it.
24     ///
25     /// ### Example
26     /// ```ignore
27     /// fn get_slice(x: &[u32]) -> &[u32] {
28     ///     &x[..]
29     /// }
30     /// ```
31     /// Use instead:
32     /// ```ignore
33     /// fn get_slice(x: &[u32]) -> &[u32] {
34     ///     x
35     /// }
36     /// ```
37     #[clippy::version = "1.51.0"]
38     pub REDUNDANT_SLICING,
39     complexity,
40     "redundant slicing of the whole range of a type"
41 }
42
43 declare_lint_pass!(RedundantSlicing => [REDUNDANT_SLICING]);
44
45 impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
46     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
47         if expr.span.from_expansion() {
48             return;
49         }
50
51         let ctxt = expr.span.ctxt();
52         if_chain! {
53             if let ExprKind::AddrOf(BorrowKind::Ref, mutability, addressee) = expr.kind;
54             if addressee.span.ctxt() == ctxt;
55             if let ExprKind::Index(indexed, range) = addressee.kind;
56             if is_type_lang_item(cx, cx.typeck_results().expr_ty_adjusted(range), LangItem::RangeFull);
57             if TyS::same_type(cx.typeck_results().expr_ty(expr), cx.typeck_results().expr_ty(indexed));
58             then {
59                 let mut app = Applicability::MachineApplicable;
60                 let snip = snippet_with_context(cx, indexed.span, ctxt, "..", &mut app).0;
61
62                 let (reborrow_str, help_str) = if mutability == Mutability::Mut {
63                     // The slice was used to reborrow the mutable reference.
64                     ("&mut *", "reborrow the original value instead")
65                 } else if matches!(
66                     get_parent_expr(cx, expr),
67                     Some(Expr {
68                         kind: ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _),
69                         ..
70                     })
71                 ) {
72                     // The slice was used to make a temporary reference.
73                     ("&*", "reborrow the original value instead")
74                 } else {
75                     ("", "use the original value instead")
76                 };
77
78                 span_lint_and_sugg(
79                     cx,
80                     REDUNDANT_SLICING,
81                     expr.span,
82                     "redundant slicing of the whole range",
83                     help_str,
84                     format!("{}{}", reborrow_str, snip),
85                     app,
86                 );
87             }
88         }
89     }
90 }