@@ -321,14 +321,15 @@ impl Types {
321
321
if let Some ( def_id) = res. opt_def_id ( ) {
322
322
if Some ( def_id) == cx. tcx . lang_items ( ) . owned_box ( ) {
323
323
if let Some ( span) = match_borrows_parameter ( cx, qpath) {
324
+ let mut applicability = Applicability :: MachineApplicable ;
324
325
span_lint_and_sugg (
325
326
cx,
326
327
REDUNDANT_ALLOCATION ,
327
328
hir_ty. span ,
328
329
"usage of `Box<&T>`" ,
329
330
"try" ,
330
- snippet ( cx, span, ".." ) . to_string ( ) ,
331
- Applicability :: MachineApplicable ,
331
+ snippet_with_applicability ( cx, span, ".." , & mut applicability ) . to_string ( ) ,
332
+ applicability ,
332
333
) ;
333
334
return ; // don't recurse into the type
334
335
}
@@ -345,14 +346,15 @@ impl Types {
345
346
}
346
347
} else if cx. tcx . is_diagnostic_item ( sym:: Rc , def_id) {
347
348
if let Some ( span) = match_type_parameter ( cx, qpath, & paths:: RC ) {
349
+ let mut applicability = Applicability :: MachineApplicable ;
348
350
span_lint_and_sugg (
349
351
cx,
350
352
REDUNDANT_ALLOCATION ,
351
353
hir_ty. span ,
352
354
"usage of `Rc<Rc<T>>`" ,
353
355
"try" ,
354
- snippet ( cx, span, ".." ) . to_string ( ) ,
355
- Applicability :: MachineApplicable ,
356
+ snippet_with_applicability ( cx, span, ".." , & mut applicability ) . to_string ( ) ,
357
+ applicability ,
356
358
) ;
357
359
return ; // don't recurse into the type
358
360
}
@@ -368,26 +370,31 @@ impl Types {
368
370
GenericArg :: Type ( ty) => ty. span ,
369
371
_ => return ,
370
372
} ;
373
+ let mut applicability = Applicability :: MachineApplicable ;
371
374
span_lint_and_sugg (
372
375
cx,
373
376
REDUNDANT_ALLOCATION ,
374
377
hir_ty. span ,
375
378
"usage of `Rc<Box<T>>`" ,
376
379
"try" ,
377
- format ! ( "Rc<{}>" , snippet( cx, inner_span, ".." ) ) ,
378
- Applicability :: MachineApplicable ,
380
+ format ! (
381
+ "Rc<{}>" ,
382
+ snippet_with_applicability( cx, inner_span, ".." , & mut applicability)
383
+ ) ,
384
+ applicability,
379
385
) ;
380
386
return ; // don't recurse into the type
381
387
}
382
388
if let Some ( span) = match_borrows_parameter ( cx, qpath) {
389
+ let mut applicability = Applicability :: MachineApplicable ;
383
390
span_lint_and_sugg (
384
391
cx,
385
392
REDUNDANT_ALLOCATION ,
386
393
hir_ty. span ,
387
394
"usage of `Rc<&T>`" ,
388
395
"try" ,
389
- snippet ( cx, span, ".." ) . to_string ( ) ,
390
- Applicability :: MachineApplicable ,
396
+ snippet_with_applicability ( cx, span, ".." , & mut applicability ) . to_string ( ) ,
397
+ applicability ,
391
398
) ;
392
399
return ; // don't recurse into the type
393
400
}
@@ -546,7 +553,6 @@ impl Types {
546
553
// details.
547
554
return ;
548
555
}
549
- let mut applicability = Applicability :: MachineApplicable ;
550
556
span_lint_and_sugg(
551
557
cx,
552
558
BORROWED_BOX ,
@@ -556,8 +562,12 @@ impl Types {
556
562
format!(
557
563
"&{}{}" ,
558
564
ltopt,
559
- & snippet_with_applicability ( cx, inner. span, ".." , & mut applicability )
565
+ & snippet ( cx, inner. span, ".." )
560
566
) ,
567
+ // To make this `MachineApplicable`, at least one needs to check if it isn't a trait item
568
+ // because the trait impls of it will break otherwise;
569
+ // and there may be other cases that result in invalid code.
570
+ // For example, type coercion doesn't work nicely.
561
571
Applicability :: Unspecified ,
562
572
) ;
563
573
return ; // don't recurse into the type
0 commit comments