]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/types/rc_buffer.rs
Auto merge of #85020 - lrh2000:named-upvars, r=tmandry
[rust.git] / src / tools / clippy / clippy_lints / src / types / rc_buffer.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet_with_applicability;
3 use clippy_utils::{get_qpath_generic_tys, is_ty_param_diagnostic_item};
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::RC_BUFFER;
10
11 pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
12     if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
13         if let Some(alternate) = match_buffer_type(cx, qpath) {
14             span_lint_and_sugg(
15                 cx,
16                 RC_BUFFER,
17                 hir_ty.span,
18                 "usage of `Rc<T>` when T is a buffer type",
19                 "try",
20                 format!("Rc<{}>", alternate),
21                 Applicability::MachineApplicable,
22             );
23         } else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
24             let qpath = match &ty.kind {
25                 TyKind::Path(qpath) => qpath,
26                 _ => return false,
27             };
28             let inner_span = match get_qpath_generic_tys(qpath).next() {
29                 Some(ty) => ty.span,
30                 None => return false,
31             };
32             let mut applicability = Applicability::MachineApplicable;
33             span_lint_and_sugg(
34                 cx,
35                 RC_BUFFER,
36                 hir_ty.span,
37                 "usage of `Rc<T>` when T is a buffer type",
38                 "try",
39                 format!(
40                     "Rc<[{}]>",
41                     snippet_with_applicability(cx, inner_span, "..", &mut applicability)
42                 ),
43                 Applicability::MachineApplicable,
44             );
45             return true;
46         }
47     } else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
48         if let Some(alternate) = match_buffer_type(cx, qpath) {
49             span_lint_and_sugg(
50                 cx,
51                 RC_BUFFER,
52                 hir_ty.span,
53                 "usage of `Arc<T>` when T is a buffer type",
54                 "try",
55                 format!("Arc<{}>", alternate),
56                 Applicability::MachineApplicable,
57             );
58         } else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
59             let qpath = match &ty.kind {
60                 TyKind::Path(qpath) => qpath,
61                 _ => return false,
62             };
63             let inner_span = match get_qpath_generic_tys(qpath).next() {
64                 Some(ty) => ty.span,
65                 None => return false,
66             };
67             let mut applicability = Applicability::MachineApplicable;
68             span_lint_and_sugg(
69                 cx,
70                 RC_BUFFER,
71                 hir_ty.span,
72                 "usage of `Arc<T>` when T is a buffer type",
73                 "try",
74                 format!(
75                     "Arc<[{}]>",
76                     snippet_with_applicability(cx, inner_span, "..", &mut applicability)
77                 ),
78                 Applicability::MachineApplicable,
79             );
80             return true;
81         }
82     }
83
84     false
85 }
86
87 fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
88     if is_ty_param_diagnostic_item(cx, qpath, sym::string_type).is_some() {
89         Some("str")
90     } else if is_ty_param_diagnostic_item(cx, qpath, sym::OsString).is_some() {
91         Some("std::ffi::OsStr")
92     } else if is_ty_param_diagnostic_item(cx, qpath, sym::PathBuf).is_some() {
93         Some("std::path::Path")
94     } else {
95         None
96     }
97 }