Skip to content

Commit 6d60568

Browse files
committed
refactor: also handle Vec in match_buffer_type
Reduces code repetition
1 parent 06e8ff7 commit 6d60568

File tree

1 file changed

+21
-67
lines changed

1 file changed

+21
-67
lines changed

clippy_lints/src/types/rc_buffer.rs

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use rustc_hir::def_id::DefId;
66
use rustc_hir::{self as hir, QPath, TyKind};
77
use rustc_lint::LateContext;
88
use rustc_span::symbol::sym;
9+
use std::borrow::Cow;
910

1011
use super::RC_BUFFER;
1112

1213
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
13-
let app = Applicability::Unspecified;
14+
let mut app = Applicability::Unspecified;
1415
let name = cx.tcx.get_diagnostic_name(def_id);
1516
if name == Some(sym::Rc) {
16-
if let Some(alternate) = match_buffer_type(cx, qpath) {
17+
if let Some(alternate) = match_buffer_type(cx, qpath, &mut app) {
1718
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
1819
span_lint_and_then(
1920
cx,
@@ -26,40 +27,10 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
2627
);
2728
true
2829
} else {
29-
let Some(ty) = qpath_generic_tys(qpath).next() else {
30-
return false;
31-
};
32-
let Some(id) = path_def_id(cx, ty) else { return false };
33-
if !cx.tcx.is_diagnostic_item(sym::Vec, id) {
34-
return false;
35-
}
36-
let TyKind::Path(qpath) = &ty.kind else { return false };
37-
let inner_span = match qpath_generic_tys(qpath).next() {
38-
Some(ty) => ty.span,
39-
None => return false,
40-
};
41-
span_lint_and_then(
42-
cx,
43-
RC_BUFFER,
44-
hir_ty.span,
45-
"usage of `Rc<T>` when `T` is a buffer type",
46-
|diag| {
47-
let mut applicability = app;
48-
diag.span_suggestion(
49-
hir_ty.span,
50-
"try",
51-
format!(
52-
"Rc<[{}]>",
53-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
54-
),
55-
app,
56-
);
57-
},
58-
);
59-
true
30+
false
6031
}
6132
} else if name == Some(sym::Arc) {
62-
if let Some(alternate) = match_buffer_type(cx, qpath) {
33+
if let Some(alternate) = match_buffer_type(cx, qpath, &mut app) {
6334
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
6435
span_lint_and_then(
6536
cx,
@@ -71,35 +42,6 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
7142
},
7243
);
7344
true
74-
} else if let Some(ty) = qpath_generic_tys(qpath).next() {
75-
let Some(id) = path_def_id(cx, ty) else { return false };
76-
if !cx.tcx.is_diagnostic_item(sym::Vec, id) {
77-
return false;
78-
}
79-
let TyKind::Path(qpath) = &ty.kind else { return false };
80-
let inner_span = match qpath_generic_tys(qpath).next() {
81-
Some(ty) => ty.span,
82-
None => return false,
83-
};
84-
span_lint_and_then(
85-
cx,
86-
RC_BUFFER,
87-
hir_ty.span,
88-
"usage of `Arc<T>` when `T` is a buffer type",
89-
|diag| {
90-
let mut applicability = app;
91-
diag.span_suggestion(
92-
hir_ty.span,
93-
"try",
94-
format!(
95-
"Arc<[{}]>",
96-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
97-
),
98-
app,
99-
);
100-
},
101-
);
102-
true
10345
} else {
10446
false
10547
}
@@ -108,13 +50,25 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
10850
}
10951
}
11052

111-
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
53+
fn match_buffer_type(
54+
cx: &LateContext<'_>,
55+
qpath: &QPath<'_>,
56+
applicability: &mut Applicability,
57+
) -> Option<Cow<'static, str>> {
11258
let ty = qpath_generic_tys(qpath).next()?;
11359
let id = path_def_id(cx, ty)?;
11460
let path = match cx.tcx.get_diagnostic_name(id) {
115-
Some(sym::OsString) => "std::ffi::OsStr",
116-
Some(sym::PathBuf) => "std::path::Path",
117-
_ 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(),
11872
_ => return None,
11973
};
12074
Some(path)

0 commit comments

Comments
 (0)