Skip to content

Commit b7f1428

Browse files
committed
refactor: reduce code duplication further
1 parent 56cee2b commit b7f1428

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

clippy_lints/src/types/rc_buffer.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,50 @@ use rustc_hir::{QPath, Ty, TyKind};
77
use rustc_lint::LateContext;
88
use rustc_span::symbol::sym;
99
use std::borrow::Cow;
10+
use std::fmt;
1011

1112
use super::RC_BUFFER;
1213

1314
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
1415
let mut app = Applicability::Unspecified;
15-
let name = cx.tcx.get_diagnostic_name(def_id);
16-
if name == Some(sym::Rc) {
17-
if let Some(ty) = qpath_generic_tys(qpath).next()
18-
&& let Some(alternate) = match_buffer_type(cx, ty, &mut app)
19-
{
20-
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
21-
span_lint_and_then(
22-
cx,
23-
RC_BUFFER,
24-
hir_ty.span,
25-
"usage of `Rc<T>` when `T` is a buffer type",
26-
|diag| {
27-
diag.span_suggestion_verbose(ty.span, "try", alternate, app);
28-
},
29-
);
30-
true
31-
} else {
32-
false
33-
}
34-
} else if name == Some(sym::Arc) {
35-
if let Some(ty) = qpath_generic_tys(qpath).next()
36-
&& let Some(alternate) = match_buffer_type(cx, ty, &mut app)
37-
{
38-
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
39-
span_lint_and_then(
40-
cx,
41-
RC_BUFFER,
42-
hir_ty.span,
43-
"usage of `Arc<T>` when `T` is a buffer type",
44-
|diag| {
45-
diag.span_suggestion_verbose(ty.span, "try", alternate, app);
46-
},
47-
);
48-
true
49-
} else {
50-
false
51-
}
16+
let kind = match cx.tcx.get_diagnostic_name(def_id) {
17+
Some(sym::Rc) => RcKind::Rc,
18+
Some(sym::Arc) => RcKind::Arc,
19+
_ => return false,
20+
};
21+
if let Some(ty) = qpath_generic_tys(qpath).next()
22+
&& let Some(alternate) = match_buffer_type(cx, ty, &mut app)
23+
{
24+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
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
5235
} else {
5336
false
5437
}
5538
}
5639

40+
enum RcKind {
41+
Rc,
42+
Arc,
43+
}
44+
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+
}
52+
}
53+
5754
fn match_buffer_type(
5855
cx: &LateContext<'_>,
5956
ty: &Ty<'_>,

0 commit comments

Comments
 (0)