@@ -7,53 +7,50 @@ use rustc_hir::{QPath, Ty, TyKind};
7
7
use rustc_lint:: LateContext ;
8
8
use rustc_span:: symbol:: sym;
9
9
use std:: borrow:: Cow ;
10
+ use std:: fmt;
10
11
11
12
use super :: RC_BUFFER ;
12
13
13
14
pub ( super ) fn check ( cx : & LateContext < ' _ > , hir_ty : & Ty < ' _ > , qpath : & QPath < ' _ > , def_id : DefId ) -> bool {
14
15
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
52
35
} else {
53
36
false
54
37
}
55
38
}
56
39
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
+
57
54
fn match_buffer_type (
58
55
cx : & LateContext < ' _ > ,
59
56
ty : & Ty < ' _ > ,
0 commit comments