@@ -241,14 +241,14 @@ fn check_inclusive_range_minus_one(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
241241}
242242
243243fn 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 {
248247 kind : ExprKind :: Index ( ..) ,
249248 ..
250- } )
251- )
249+ } ) => parent_expr,
250+ _ => None ,
251+ }
252252 }
253253
254254 fn is_empty_range ( limits : RangeLimits , ordering : Ordering ) -> bool {
@@ -267,18 +267,32 @@ fn check_reversed_empty_range(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
267267 if let Some ( ordering) = Constant :: partial_cmp( cx. tcx, ty, & start_idx, & end_idx) ;
268268 if is_empty_range( limits, ordering) ;
269269 then {
270- if inside_indexing_expr( cx, expr) {
270+ if let Some ( parent_expr ) = inside_indexing_expr( cx, expr) {
271271 let ( reason, outcome) = if ordering == Ordering :: Equal {
272272 ( "empty" , "always yield an empty slice" )
273273 } else {
274274 ( "reversed" , "panic at run-time" )
275275 } ;
276276
277- span_lint (
277+ span_lint_and_then (
278278 cx,
279279 REVERSED_EMPTY_RANGES ,
280280 expr. span,
281281 & 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+ }
282296 ) ;
283297 } else {
284298 span_lint_and_then(
0 commit comments