1
1
use clippy_utils:: consts:: { constant, Constant } ;
2
2
use clippy_utils:: diagnostics:: span_lint_and_then;
3
3
use clippy_utils:: higher:: IfLet ;
4
- use clippy_utils:: ty:: implements_trait ;
5
- use clippy_utils:: { in_macro , is_expn_of, is_lint_allowed, meets_msrv, msrvs, path_to_local} ;
4
+ use clippy_utils:: ty:: is_copy ;
5
+ use clippy_utils:: { is_expn_of, is_lint_allowed, meets_msrv, msrvs, path_to_local} ;
6
6
use if_chain:: if_chain;
7
7
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
8
8
use rustc_errors:: Applicability ;
@@ -53,14 +53,14 @@ declare_clippy_lint! {
53
53
54
54
#[ derive( Copy , Clone ) ]
55
55
pub struct IndexRefutableSlice {
56
- indexing_limit : u64 ,
56
+ max_suggested_slice : u64 ,
57
57
msrv : Option < RustcVersion > ,
58
58
}
59
59
60
60
impl IndexRefutableSlice {
61
61
pub fn new ( max_suggested_slice_pattern_length : u64 , msrv : Option < RustcVersion > ) -> Self {
62
62
Self {
63
- indexing_limit : max_suggested_slice_pattern_length - 1 ,
63
+ max_suggested_slice : max_suggested_slice_pattern_length,
64
64
msrv,
65
65
}
66
66
}
@@ -71,14 +71,14 @@ impl_lint_pass!(IndexRefutableSlice => [INDEX_REFUTABLE_SLICE]);
71
71
impl LateLintPass < ' _ > for IndexRefutableSlice {
72
72
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx hir:: Expr < ' _ > ) {
73
73
if_chain ! {
74
- if !in_macro ( expr. span) || is_expn_of( expr. span, "if_chain" ) . is_some( ) ;
74
+ if !expr. span. from_expansion ( ) || is_expn_of( expr. span, "if_chain" ) . is_some( ) ;
75
75
if let Some ( IfLet { let_pat, if_then, ..} ) = IfLet :: hir( cx, expr) ;
76
76
if !is_lint_allowed( cx, INDEX_REFUTABLE_SLICE , expr. hir_id) ;
77
77
if meets_msrv( self . msrv. as_ref( ) , & msrvs:: SLICE_PATTERNS ) ;
78
78
79
79
let found_slices = find_slice_values( cx, let_pat) ;
80
80
if !found_slices. is_empty( ) ;
81
- let filtered_slices = filter_lintable_slices( cx, found_slices, self . indexing_limit , if_then) ;
81
+ let filtered_slices = filter_lintable_slices( cx, found_slices, self . max_suggested_slice , if_then) ;
82
82
if !filtered_slices. is_empty( ) ;
83
83
then {
84
84
for slice in filtered_slices. values( ) {
@@ -117,12 +117,7 @@ fn find_slice_values(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> FxHashMap<hir:
117
117
// The values need to use the `ref` keyword if they can't be copied.
118
118
// This will need to be adjusted if the lint want to support multable access in the future
119
119
let src_is_ref = bound_ty. is_ref ( ) && binding != hir:: BindingAnnotation :: Ref ;
120
- let is_copy = cx
121
- . tcx
122
- . lang_items ( )
123
- . copy_trait ( )
124
- . map_or ( false , |trait_id| implements_trait ( cx, inner_ty, trait_id, & [ ] ) ) ;
125
- let needs_ref = !( src_is_ref || is_copy) ;
120
+ let needs_ref = !( src_is_ref || is_copy ( cx, inner_ty) ) ;
126
121
127
122
let slice_info = slices
128
123
. entry ( value_hir_id)
@@ -183,10 +178,9 @@ fn lint_slice(cx: &LateContext<'_>, slice: &SliceLintInformation) {
183
178
Applicability :: MaybeIncorrect ,
184
179
) ;
185
180
186
- // I thought about adding a note to the lint message to inform the user that these
187
- // refactorings will remove the index expression. However, I decided against this,
188
- // as `filter_lintable_slices` will only return slices where all access indices are
189
- // known at compile time. The removal should therefore not have any side effects.
181
+ // The lint message doesn't contain a warning about the removed index expression,
182
+ // since `filter_lintable_slices` will only return slices where all access indices
183
+ // are known at compile time. Therefore, they can be removed without side effects.
190
184
} ,
191
185
) ;
192
186
}
@@ -214,13 +208,13 @@ impl SliceLintInformation {
214
208
fn filter_lintable_slices < ' a , ' tcx > (
215
209
cx : & ' a LateContext < ' tcx > ,
216
210
slice_lint_info : FxHashMap < hir:: HirId , SliceLintInformation > ,
217
- index_limit : u64 ,
211
+ max_suggested_slice : u64 ,
218
212
scope : & ' tcx hir:: Expr < ' tcx > ,
219
213
) -> FxHashMap < hir:: HirId , SliceLintInformation > {
220
214
let mut visitor = SliceIndexLintingVisitor {
221
215
cx,
222
216
slice_lint_info,
223
- index_limit ,
217
+ max_suggested_slice ,
224
218
} ;
225
219
226
220
intravisit:: walk_expr ( & mut visitor, scope) ;
@@ -234,7 +228,7 @@ fn filter_lintable_slices<'a, 'tcx>(
234
228
struct SliceIndexLintingVisitor < ' a , ' tcx > {
235
229
cx : & ' a LateContext < ' tcx > ,
236
230
slice_lint_info : FxHashMap < hir:: HirId , SliceLintInformation > ,
237
- index_limit : u64 ,
231
+ max_suggested_slice : u64 ,
238
232
}
239
233
240
234
impl < ' a , ' tcx > Visitor < ' tcx > for SliceIndexLintingVisitor < ' a , ' tcx > {
@@ -249,7 +243,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
249
243
let Self {
250
244
cx,
251
245
ref mut slice_lint_info,
252
- index_limit ,
246
+ max_suggested_slice ,
253
247
} = * self ;
254
248
255
249
if_chain ! {
@@ -264,7 +258,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
264
258
if let hir:: ExprKind :: Index ( _, index_expr) = parent_expr. kind;
265
259
if let Some ( ( Constant :: Int ( index_value) , _) ) = constant( cx, cx. typeck_results( ) , index_expr) ;
266
260
if let Ok ( index_value) = index_value. try_into( ) ;
267
- if index_value <= index_limit ;
261
+ if index_value < max_suggested_slice ;
268
262
269
263
// Make sure that this slice index is read only
270
264
let maybe_addrof_id = map. get_parent_node( parent_id) ;
0 commit comments