@@ -6,14 +6,15 @@ use rustc_hir::def_id::DefId;
6
6
use rustc_hir:: { self as hir, QPath , TyKind } ;
7
7
use rustc_lint:: LateContext ;
8
8
use rustc_span:: symbol:: sym;
9
+ use std:: borrow:: Cow ;
9
10
10
11
use super :: RC_BUFFER ;
11
12
12
13
pub ( super ) fn check ( cx : & LateContext < ' _ > , hir_ty : & hir:: Ty < ' _ > , qpath : & QPath < ' _ > , def_id : DefId ) -> bool {
13
- let app = Applicability :: Unspecified ;
14
+ let mut app = Applicability :: Unspecified ;
14
15
let name = cx. tcx . get_diagnostic_name ( def_id) ;
15
16
if name == Some ( sym:: Rc ) {
16
- if let Some ( alternate) = match_buffer_type ( cx, qpath) {
17
+ if let Some ( alternate) = match_buffer_type ( cx, qpath, & mut app ) {
17
18
#[ expect( clippy:: collapsible_span_lint_calls, reason = "rust-clippy#7797" ) ]
18
19
span_lint_and_then (
19
20
cx,
@@ -26,40 +27,10 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
26
27
) ;
27
28
true
28
29
} else {
29
- let Some ( ty) = qpath_generic_tys ( qpath) . next ( ) else {
30
- return false ;
31
- } ;
32
- let Some ( id) = path_def_id ( cx, ty) else { return false } ;
33
- if !cx. tcx . is_diagnostic_item ( sym:: Vec , id) {
34
- return false ;
35
- }
36
- let TyKind :: Path ( qpath) = & ty. kind else { return false } ;
37
- let inner_span = match qpath_generic_tys ( qpath) . next ( ) {
38
- Some ( ty) => ty. span ,
39
- None => return false ,
40
- } ;
41
- span_lint_and_then (
42
- cx,
43
- RC_BUFFER ,
44
- hir_ty. span ,
45
- "usage of `Rc<T>` when `T` is a buffer type" ,
46
- |diag| {
47
- let mut applicability = app;
48
- diag. span_suggestion (
49
- hir_ty. span ,
50
- "try" ,
51
- format ! (
52
- "Rc<[{}]>" ,
53
- snippet_with_applicability( cx, inner_span, ".." , & mut applicability)
54
- ) ,
55
- app,
56
- ) ;
57
- } ,
58
- ) ;
59
- true
30
+ false
60
31
}
61
32
} else if name == Some ( sym:: Arc ) {
62
- if let Some ( alternate) = match_buffer_type ( cx, qpath) {
33
+ if let Some ( alternate) = match_buffer_type ( cx, qpath, & mut app ) {
63
34
#[ expect( clippy:: collapsible_span_lint_calls, reason = "rust-clippy#7797" ) ]
64
35
span_lint_and_then (
65
36
cx,
@@ -71,35 +42,6 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
71
42
} ,
72
43
) ;
73
44
true
74
- } else if let Some ( ty) = qpath_generic_tys ( qpath) . next ( ) {
75
- let Some ( id) = path_def_id ( cx, ty) else { return false } ;
76
- if !cx. tcx . is_diagnostic_item ( sym:: Vec , id) {
77
- return false ;
78
- }
79
- let TyKind :: Path ( qpath) = & ty. kind else { return false } ;
80
- let inner_span = match qpath_generic_tys ( qpath) . next ( ) {
81
- Some ( ty) => ty. span ,
82
- None => return false ,
83
- } ;
84
- span_lint_and_then (
85
- cx,
86
- RC_BUFFER ,
87
- hir_ty. span ,
88
- "usage of `Arc<T>` when `T` is a buffer type" ,
89
- |diag| {
90
- let mut applicability = app;
91
- diag. span_suggestion (
92
- hir_ty. span ,
93
- "try" ,
94
- format ! (
95
- "Arc<[{}]>" ,
96
- snippet_with_applicability( cx, inner_span, ".." , & mut applicability)
97
- ) ,
98
- app,
99
- ) ;
100
- } ,
101
- ) ;
102
- true
103
45
} else {
104
46
false
105
47
}
@@ -108,13 +50,25 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
108
50
}
109
51
}
110
52
111
- fn match_buffer_type ( cx : & LateContext < ' _ > , qpath : & QPath < ' _ > ) -> Option < & ' static str > {
53
+ fn match_buffer_type (
54
+ cx : & LateContext < ' _ > ,
55
+ qpath : & QPath < ' _ > ,
56
+ applicability : & mut Applicability ,
57
+ ) -> Option < Cow < ' static , str > > {
112
58
let ty = qpath_generic_tys ( qpath) . next ( ) ?;
113
59
let id = path_def_id ( cx, ty) ?;
114
60
let path = match cx. tcx . get_diagnostic_name ( id) {
115
- Some ( sym:: OsString ) => "std::ffi::OsStr" ,
116
- Some ( sym:: PathBuf ) => "std::path::Path" ,
117
- _ if Some ( id) == cx. tcx . lang_items ( ) . string ( ) => "str" ,
61
+ Some ( sym:: OsString ) => "std::ffi::OsStr" . into ( ) ,
62
+ Some ( sym:: PathBuf ) => "std::path::Path" . into ( ) ,
63
+ Some ( sym:: Vec ) => {
64
+ let TyKind :: Path ( vec_qpath) = & ty. kind else {
65
+ return None ;
66
+ } ;
67
+ let vec_generic_ty = qpath_generic_tys ( vec_qpath) . next ( ) ?;
68
+ let snippet = snippet_with_applicability ( cx, vec_generic_ty. span , "_" , applicability) ;
69
+ format ! ( "[{snippet}]" ) . into ( )
70
+ } ,
71
+ _ if Some ( id) == cx. tcx . lang_items ( ) . string ( ) => "str" . into ( ) ,
118
72
_ => return None ,
119
73
} ;
120
74
Some ( path)
0 commit comments