@@ -7,51 +7,49 @@ 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
- 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
50
34
} else {
51
35
false
52
36
}
53
37
}
54
38
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
+
55
53
fn match_buffer_type (
56
54
cx : & LateContext < ' _ > ,
57
55
ty : & Ty < ' _ > ,
0 commit comments