|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_then; |
2 | 2 | use clippy_utils::qpath_generic_tys; |
3 | | -use clippy_utils::res::{MaybeDef, MaybeResPath}; |
| 3 | +use clippy_utils::res::MaybeResPath; |
4 | 4 | use clippy_utils::source::snippet_with_applicability; |
5 | 5 | use rustc_errors::Applicability; |
6 | 6 | use rustc_hir::def_id::DefId; |
7 | | -use rustc_hir::{self as hir, QPath, TyKind}; |
| 7 | +use rustc_hir::{QPath, Ty, TyKind}; |
8 | 8 | use rustc_lint::LateContext; |
9 | 9 | use rustc_span::symbol::sym; |
| 10 | +use std::borrow::Cow; |
| 11 | +use std::fmt; |
10 | 12 |
|
11 | 13 | use super::RC_BUFFER; |
12 | 14 |
|
13 | | -pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool { |
14 | | - let app = Applicability::Unspecified; |
15 | | - let name = cx.tcx.get_diagnostic_name(def_id); |
16 | | - if name == Some(sym::Rc) { |
17 | | - if let Some(alternate) = match_buffer_type(cx, qpath) { |
18 | | - #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] |
19 | | - span_lint_and_then( |
20 | | - cx, |
21 | | - RC_BUFFER, |
22 | | - hir_ty.span, |
23 | | - "usage of `Rc<T>` when T is a buffer type", |
24 | | - |diag| { |
25 | | - diag.span_suggestion(hir_ty.span, "try", format!("Rc<{alternate}>"), app); |
26 | | - }, |
27 | | - ); |
28 | | - } else { |
29 | | - let Some(ty) = qpath_generic_tys(qpath).next() else { |
30 | | - return false; |
31 | | - }; |
32 | | - if !ty.basic_res().is_diag_item(cx, sym::Vec) { |
33 | | - return false; |
34 | | - } |
35 | | - let TyKind::Path(qpath) = &ty.kind else { return false }; |
36 | | - let inner_span = match qpath_generic_tys(qpath).next() { |
37 | | - Some(ty) => ty.span, |
38 | | - None => return false, |
39 | | - }; |
40 | | - span_lint_and_then( |
41 | | - cx, |
42 | | - RC_BUFFER, |
43 | | - hir_ty.span, |
44 | | - "usage of `Rc<T>` when T is a buffer type", |
45 | | - |diag| { |
46 | | - let mut applicability = app; |
47 | | - diag.span_suggestion( |
48 | | - hir_ty.span, |
49 | | - "try", |
50 | | - format!( |
51 | | - "Rc<[{}]>", |
52 | | - snippet_with_applicability(cx, inner_span, "..", &mut applicability) |
53 | | - ), |
54 | | - app, |
55 | | - ); |
56 | | - }, |
57 | | - ); |
58 | | - return true; |
59 | | - } |
60 | | - } else if name == Some(sym::Arc) { |
61 | | - if let Some(alternate) = match_buffer_type(cx, qpath) { |
62 | | - #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] |
63 | | - span_lint_and_then( |
64 | | - cx, |
65 | | - RC_BUFFER, |
66 | | - hir_ty.span, |
67 | | - "usage of `Arc<T>` when T is a buffer type", |
68 | | - |diag| { |
69 | | - diag.span_suggestion(hir_ty.span, "try", format!("Arc<{alternate}>"), app); |
70 | | - }, |
71 | | - ); |
72 | | - } else if let Some(ty) = qpath_generic_tys(qpath).next() { |
73 | | - if !ty.basic_res().is_diag_item(cx, sym::Vec) { |
74 | | - return false; |
75 | | - } |
76 | | - let TyKind::Path(qpath) = &ty.kind else { return false }; |
77 | | - let inner_span = match qpath_generic_tys(qpath).next() { |
78 | | - Some(ty) => ty.span, |
79 | | - None => return false, |
80 | | - }; |
81 | | - span_lint_and_then( |
82 | | - cx, |
83 | | - RC_BUFFER, |
84 | | - hir_ty.span, |
85 | | - "usage of `Arc<T>` when T is a buffer type", |
86 | | - |diag| { |
87 | | - let mut applicability = app; |
88 | | - diag.span_suggestion( |
89 | | - hir_ty.span, |
90 | | - "try", |
91 | | - format!( |
92 | | - "Arc<[{}]>", |
93 | | - snippet_with_applicability(cx, inner_span, "..", &mut applicability) |
94 | | - ), |
95 | | - app, |
96 | | - ); |
97 | | - }, |
98 | | - ); |
99 | | - return true; |
100 | | - } |
| 15 | +pub(super) fn check(cx: &LateContext<'_>, hir_ty: &Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool { |
| 16 | + let mut app = Applicability::Unspecified; |
| 17 | + let kind = match cx.tcx.get_diagnostic_name(def_id) { |
| 18 | + Some(sym::Rc) => RcKind::Rc, |
| 19 | + Some(sym::Arc) => RcKind::Arc, |
| 20 | + _ => return false, |
| 21 | + }; |
| 22 | + if let Some(ty) = qpath_generic_tys(qpath).next() |
| 23 | + && let Some(alternate) = match_buffer_type(cx, ty, &mut app) |
| 24 | + { |
| 25 | + span_lint_and_then( |
| 26 | + cx, |
| 27 | + RC_BUFFER, |
| 28 | + hir_ty.span, |
| 29 | + format!("usage of `{kind}<T>` when `T` is a buffer type"), |
| 30 | + |diag| { |
| 31 | + diag.span_suggestion_verbose(ty.span, "try", alternate, app); |
| 32 | + }, |
| 33 | + ); |
| 34 | + true |
| 35 | + } else { |
| 36 | + false |
101 | 37 | } |
| 38 | +} |
| 39 | + |
| 40 | +enum RcKind { |
| 41 | + Rc, |
| 42 | + Arc, |
| 43 | +} |
102 | 44 |
|
103 | | - false |
| 45 | +impl fmt::Display for RcKind { |
| 46 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 47 | + match self { |
| 48 | + Self::Rc => f.write_str("Rc"), |
| 49 | + Self::Arc => f.write_str("Arc"), |
| 50 | + } |
| 51 | + } |
104 | 52 | } |
105 | 53 |
|
106 | | -fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> { |
107 | | - let ty = qpath_generic_tys(qpath).next()?; |
| 54 | +fn match_buffer_type( |
| 55 | + cx: &LateContext<'_>, |
| 56 | + ty: &Ty<'_>, |
| 57 | + applicability: &mut Applicability, |
| 58 | +) -> Option<Cow<'static, str>> { |
108 | 59 | let id = ty.basic_res().opt_def_id()?; |
109 | 60 | let path = match cx.tcx.get_diagnostic_name(id) { |
110 | | - Some(sym::OsString) => "std::ffi::OsStr", |
111 | | - Some(sym::PathBuf) => "std::path::Path", |
112 | | - _ if Some(id) == cx.tcx.lang_items().string() => "str", |
| 61 | + Some(sym::OsString) => "std::ffi::OsStr".into(), |
| 62 | + Some(sym::PathBuf) => "std::path::Path".into(), |
| 63 | + Some(sym::Vec) => { |
| 64 | + let TyKind::Path(vec_qpath) = &ty.kind else { |
| 65 | + return None; |
| 66 | + }; |
| 67 | + let vec_generic_ty = qpath_generic_tys(vec_qpath).next()?; |
| 68 | + let snippet = snippet_with_applicability(cx, vec_generic_ty.span, "_", applicability); |
| 69 | + format!("[{snippet}]").into() |
| 70 | + }, |
| 71 | + _ if Some(id) == cx.tcx.lang_items().string() => "str".into(), |
113 | 72 | _ => return None, |
114 | 73 | }; |
115 | 74 | Some(path) |
|
0 commit comments