Skip to content

Commit 39b5a05

Browse files
committed
refactor: reduce code duplication further
1 parent 771ca38 commit 39b5a05

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

clippy_lints/src/types/rc_buffer.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,49 @@ 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-
span_lint_and_then(
21-
cx,
22-
RC_BUFFER,
23-
hir_ty.span,
24-
"usage of `Rc<T>` when `T` is a buffer type",
25-
|diag| {
26-
diag.span_suggestion_verbose(ty.span, "try", alternate, app);
27-
},
28-
);
29-
true
30-
} else {
31-
false
32-
}
33-
} else if name == Some(sym::Arc) {
34-
if let Some(ty) = qpath_generic_tys(qpath).next()
35-
&& let Some(alternate) = match_buffer_type(cx, ty, &mut app)
36-
{
37-
span_lint_and_then(
38-
cx,
39-
RC_BUFFER,
40-
hir_ty.span,
41-
"usage of `Arc<T>` when `T` is a buffer type",
42-
|diag| {
43-
diag.span_suggestion_verbose(ty.span, "try", alternate, app);
44-
},
45-
);
46-
true
47-
} else {
48-
false
49-
}
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+
span_lint_and_then(
25+
cx,
26+
RC_BUFFER,
27+
hir_ty.span,
28+
format!("usage of `{kind}<T>` when `T` is a buffer type"),
29+
|diag| {
30+
diag.span_suggestion_verbose(ty.span, "try", alternate, app);
31+
},
32+
);
33+
true
5034
} else {
5135
false
5236
}
5337
}
5438

39+
enum RcKind {
40+
Rc,
41+
Arc,
42+
}
43+
44+
impl fmt::Display for RcKind {
45+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46+
match self {
47+
Self::Rc => f.write_str("Rc"),
48+
Self::Arc => f.write_str("Arc"),
49+
}
50+
}
51+
}
52+
5553
fn match_buffer_type(
5654
cx: &LateContext<'_>,
5755
ty: &Ty<'_>,

0 commit comments

Comments
 (0)