@@ -241,14 +241,14 @@ fn check_inclusive_range_minus_one(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
241
241
}
242
242
243
243
fn check_reversed_empty_range ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > ) {
244
- fn inside_indexing_expr ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > ) -> bool {
245
- matches ! (
246
- get_parent_expr( cx, expr) ,
247
- Some ( Expr {
244
+ fn inside_indexing_expr < ' a > ( cx : & ' a LateContext < ' _ , ' _ > , expr : & Expr < ' _ > ) -> Option < & ' a Expr < ' a > > {
245
+ match get_parent_expr ( cx, expr) {
246
+ parent_expr @ Some ( Expr {
248
247
kind : ExprKind :: Index ( ..) ,
249
248
..
250
- } )
251
- )
249
+ } ) => parent_expr,
250
+ _ => None ,
251
+ }
252
252
}
253
253
254
254
fn is_empty_range ( limits : RangeLimits , ordering : Ordering ) -> bool {
@@ -267,18 +267,32 @@ fn check_reversed_empty_range(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
267
267
if let Some ( ordering) = Constant :: partial_cmp( cx. tcx, ty, & start_idx, & end_idx) ;
268
268
if is_empty_range( limits, ordering) ;
269
269
then {
270
- if inside_indexing_expr( cx, expr) {
270
+ if let Some ( parent_expr ) = inside_indexing_expr( cx, expr) {
271
271
let ( reason, outcome) = if ordering == Ordering :: Equal {
272
272
( "empty" , "always yield an empty slice" )
273
273
} else {
274
274
( "reversed" , "panic at run-time" )
275
275
} ;
276
276
277
- span_lint (
277
+ span_lint_and_then (
278
278
cx,
279
279
REVERSED_EMPTY_RANGES ,
280
280
expr. span,
281
281
& format!( "this range is {} and using it to index a slice will {}" , reason, outcome) ,
282
+ |diag| {
283
+ if_chain! {
284
+ if ordering == Ordering :: Equal ;
285
+ if let ty:: Slice ( slice_ty) = cx. tables. expr_ty( parent_expr) . kind;
286
+ then {
287
+ diag. span_suggestion(
288
+ parent_expr. span,
289
+ "if you want an empty slice, use" ,
290
+ format!( "[] as &[{}]" , slice_ty) ,
291
+ Applicability :: MaybeIncorrect
292
+ ) ;
293
+ }
294
+ }
295
+ }
282
296
) ;
283
297
} else {
284
298
span_lint_and_then(
0 commit comments