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