]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/types/redundant_allocation.rs
Rollup merge of #93613 - crlf0710:rename_to_async_iter, r=yaahc
[rust.git] / src / tools / clippy / clippy_lints / src / types / redundant_allocation.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::{snippet, snippet_with_applicability};
3 use clippy_utils::{path_def_id, qpath_generic_tys};
4 use rustc_errors::Applicability;
5 use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind};
6 use rustc_lint::LateContext;
7 use rustc_span::symbol::sym;
8
9 use super::{utils, REDUNDANT_ALLOCATION};
10
11 pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
12     let outer_sym = if Some(def_id) == cx.tcx.lang_items().owned_box() {
13         "Box"
14     } else if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
15         "Rc"
16     } else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
17         "Arc"
18     } else {
19         return false;
20     };
21
22     if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
23         let mut applicability = Applicability::MaybeIncorrect;
24         let generic_snippet = snippet_with_applicability(cx, span, "..", &mut applicability);
25         span_lint_and_then(
26             cx,
27             REDUNDANT_ALLOCATION,
28             hir_ty.span,
29             &format!("usage of `{}<{}>`", outer_sym, generic_snippet),
30             |diag| {
31                 diag.span_suggestion(hir_ty.span, "try", format!("{}", generic_snippet), applicability);
32                 diag.note(&format!(
33                     "`{generic}` is already a pointer, `{outer}<{generic}>` allocates a pointer on the heap",
34                     outer = outer_sym,
35                     generic = generic_snippet
36                 ));
37             },
38         );
39         return true;
40     }
41
42     let Some(ty) = qpath_generic_tys(qpath).next() else { return false };
43     let Some(id) = path_def_id(cx, ty) else { return false };
44     let (inner_sym, ty) = match cx.tcx.get_diagnostic_name(id) {
45         Some(sym::Arc) => ("Arc", ty),
46         Some(sym::Rc) => ("Rc", ty),
47         _ if Some(id) == cx.tcx.lang_items().owned_box() => ("Box", ty),
48         _ => return false,
49     };
50
51     let inner_qpath = match &ty.kind {
52         TyKind::Path(inner_qpath) => inner_qpath,
53         _ => return false,
54     };
55     let inner_span = match qpath_generic_tys(inner_qpath).next() {
56         Some(ty) => {
57             // Box<Box<dyn T>> is smaller than Box<dyn T> because of wide pointers
58             if matches!(ty.kind, TyKind::TraitObject(..)) {
59                 return false;
60             }
61             ty.span
62         },
63         None => return false,
64     };
65     if inner_sym == outer_sym {
66         let mut applicability = Applicability::MaybeIncorrect;
67         let generic_snippet = snippet_with_applicability(cx, inner_span, "..", &mut applicability);
68         span_lint_and_then(
69             cx,
70             REDUNDANT_ALLOCATION,
71             hir_ty.span,
72             &format!("usage of `{}<{}<{}>>`", outer_sym, inner_sym, generic_snippet),
73             |diag| {
74                 diag.span_suggestion(
75                     hir_ty.span,
76                     "try",
77                     format!("{}<{}>", outer_sym, generic_snippet),
78                     applicability,
79                 );
80                 diag.note(&format!(
81                     "`{inner}<{generic}>` is already on the heap, `{outer}<{inner}<{generic}>>` makes an extra allocation",
82                     outer = outer_sym,
83                     inner = inner_sym,
84                     generic = generic_snippet
85                 ));
86             },
87         );
88     } else {
89         let generic_snippet = snippet(cx, inner_span, "..");
90         span_lint_and_then(
91             cx,
92             REDUNDANT_ALLOCATION,
93             hir_ty.span,
94             &format!("usage of `{}<{}<{}>>`", outer_sym, inner_sym, generic_snippet),
95             |diag| {
96                 diag.note(&format!(
97                     "`{inner}<{generic}>` is already on the heap, `{outer}<{inner}<{generic}>>` makes an extra allocation",
98                     outer = outer_sym,
99                     inner = inner_sym,
100                     generic = generic_snippet
101                 ));
102                 diag.help(&format!(
103                     "consider using just `{outer}<{generic}>` or `{inner}<{generic}>`",
104                     outer = outer_sym,
105                     inner = inner_sym,
106                     generic = generic_snippet
107                 ));
108             },
109         );
110     }
111     true
112 }