Skip to content

Commit f2e6667

Browse files
committed
refactor: reduce code duplication further
1 parent 3714236 commit f2e6667

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
@@ -8,51 +8,49 @@ use rustc_hir::{QPath, Ty, TyKind};
88
use rustc_lint::LateContext;
99
use rustc_span::symbol::sym;
1010
use std::borrow::Cow;
11+
use std::fmt;
1112

1213
use super::RC_BUFFER;
1314

1415
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
1516
let mut app = Applicability::Unspecified;
16-
let name = cx.tcx.get_diagnostic_name(def_id);
17-
if name == Some(sym::Rc) {
18-
if let Some(ty) = qpath_generic_tys(qpath).next()
19-
&& let Some(alternate) = match_buffer_type(cx, ty, &mut app)
20-
{
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-
span_lint_and_then(
39-
cx,
40-
RC_BUFFER,
41-
hir_ty.span,
42-
"usage of `Arc<T>` when `T` is a buffer type",
43-
|diag| {
44-
diag.span_suggestion_verbose(ty.span, "try", alternate, app);
45-
},
46-
);
47-
true
48-
} else {
49-
false
50-
}
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
5135
} else {
5236
false
5337
}
5438
}
5539

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+
5654
fn match_buffer_type(
5755
cx: &LateContext<'_>,
5856
ty: &Ty<'_>,

0 commit comments

Comments
 (0)