@@ -59,30 +59,30 @@ declare_clippy_lint! {
59
59
/// ```
60
60
declare_clippy_lint ! {
61
61
pub INDEXING_SLICING ,
62
- restriction ,
62
+ pedantic ,
63
63
"indexing/slicing usage"
64
64
}
65
65
66
66
#[ derive( Copy , Clone ) ]
67
- pub struct IndexingSlicingPass ;
67
+ pub struct IndexingSlicing ;
68
68
69
- impl LintPass for IndexingSlicingPass {
69
+ impl LintPass for IndexingSlicing {
70
70
fn get_lints ( & self ) -> LintArray {
71
71
lint_array ! ( INDEXING_SLICING , OUT_OF_BOUNDS_INDEXING )
72
72
}
73
73
}
74
74
75
- impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for IndexingSlicingPass {
75
+ impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for IndexingSlicing {
76
76
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr ) {
77
- if let ExprIndex ( ref a , ref b ) = & expr. node {
78
- match & b . node {
77
+ if let ExprIndex ( ref array , ref index ) = & expr. node {
78
+ match & index . node {
79
79
// Both ExprStruct and ExprPath require this approach's checks
80
- // on the `range` returned by `higher::range(cx, b )`.
80
+ // on the `range` returned by `higher::range(cx, index )`.
81
81
// ExprStruct handles &x[n..m], &x[n..] and &x[..n].
82
82
// ExprPath handles &x[..] and x[var]
83
- ExprStruct ( _ , _ , _ ) | ExprPath ( _ ) => {
84
- if let Some ( range) = higher:: range ( cx, b ) {
85
- let ty = cx. tables . expr_ty ( a ) ;
83
+ ExprStruct ( .. ) | ExprPath ( .. ) => {
84
+ if let Some ( range) = higher:: range ( cx, index ) {
85
+ let ty = cx. tables . expr_ty ( array ) ;
86
86
if let ty:: TyArray ( _, s) = ty. sty {
87
87
let size: u128 = s. assert_usize ( cx. tcx ) . unwrap ( ) . into ( ) ;
88
88
// Index is a constant range.
@@ -100,49 +100,48 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicingPass {
100
100
}
101
101
}
102
102
}
103
+
104
+ let help_msg;
103
105
match ( range. start , range. end ) {
104
106
( None , Some ( _) ) => {
105
- cx. span_lint (
106
- INDEXING_SLICING ,
107
- expr. span ,
108
- "slicing may panic. Consider using \
109
- `.get(..n)`or `.get_mut(..n)` instead",
110
- ) ;
107
+ help_msg = "Consider using `.get(..n)`or `.get_mut(..n)` instead" ;
111
108
}
112
109
( Some ( _) , None ) => {
113
- cx. span_lint (
114
- INDEXING_SLICING ,
115
- expr. span ,
116
- "slicing may panic. Consider using \
117
- `.get(n..)` or .get_mut(n..)` instead",
118
- ) ;
110
+ help_msg = "Consider using `.get(n..)` or .get_mut(n..)` instead" ;
119
111
}
120
112
( Some ( _) , Some ( _) ) => {
121
- cx. span_lint (
122
- INDEXING_SLICING ,
123
- expr. span ,
124
- "slicing may panic. Consider using \
125
- `.get(n..m)` or `.get_mut(n..m)` instead",
126
- ) ;
113
+ help_msg =
114
+ "Consider using `.get(n..m)` or `.get_mut(n..m)` instead" ;
127
115
}
128
- ( None , None ) => ( ) ,
116
+ ( None , None ) => return , // [..] is ok
129
117
}
118
+
119
+ utils:: span_help_and_lint (
120
+ cx,
121
+ INDEXING_SLICING ,
122
+ expr. span ,
123
+ "slicing may panic." ,
124
+ help_msg,
125
+ ) ;
130
126
} else {
131
- cx. span_lint (
127
+ utils:: span_help_and_lint (
128
+ cx,
132
129
INDEXING_SLICING ,
133
130
expr. span ,
134
- "indexing may panic. Consider using `.get(n)` or \
135
- `.get_mut(n)` instead",
131
+ "indexing may panic." ,
132
+ "Consider using `.get(n)` or `.get_mut(n)` instead",
136
133
) ;
137
134
}
138
135
}
139
- ExprLit ( _ ) => {
136
+ ExprLit ( .. ) => {
140
137
// [n]
141
- let ty = cx. tables . expr_ty ( a ) ;
138
+ let ty = cx. tables . expr_ty ( array ) ;
142
139
if let ty:: TyArray ( _, s) = ty. sty {
143
140
let size: u128 = s. assert_usize ( cx. tcx ) . unwrap ( ) . into ( ) ;
144
141
// Index is a constant uint.
145
- if let Some ( ( Constant :: Int ( const_index) , _) ) = constant ( cx, cx. tables , b) {
142
+ if let Some ( ( Constant :: Int ( const_index) , _) ) =
143
+ constant ( cx, cx. tables , index)
144
+ {
146
145
if size <= const_index {
147
146
utils:: span_lint (
148
147
cx,
@@ -154,11 +153,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicingPass {
154
153
// Else index is in bounds, ok.
155
154
}
156
155
} else {
157
- cx. span_lint (
156
+ utils:: span_help_and_lint (
157
+ cx,
158
158
INDEXING_SLICING ,
159
159
expr. span ,
160
- "indexing may panic. Consider using `.get(n)` or \
161
- `.get_mut(n)` instead",
160
+ "indexing may panic." ,
161
+ "Consider using `.get(n)` or `.get_mut(n)` instead",
162
162
) ;
163
163
}
164
164
}
0 commit comments