@@ -1370,25 +1370,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1370
1370
} ;
1371
1371
}
1372
1372
ty:: Array ( _, len) => {
1373
- if let ( Some ( len) , Ok ( user_index) ) = (
1374
- len. try_eval_usize ( self . tcx , self . param_env ) ,
1375
- field. as_str ( ) . parse :: < u64 > ( )
1376
- ) {
1377
- let base = self . tcx . sess . source_map ( )
1378
- . span_to_snippet ( base. span )
1379
- . unwrap_or_else ( |_|
1380
- self . tcx . hir ( ) . hir_to_pretty_string ( base. hir_id ) ) ;
1381
- let help = "instead of using tuple indexing, use array indexing" ;
1382
- let suggestion = format ! ( "{}[{}]" , base, field) ;
1383
- let applicability = if len < user_index {
1384
- Applicability :: MachineApplicable
1385
- } else {
1386
- Applicability :: MaybeIncorrect
1387
- } ;
1388
- err. span_suggestion (
1389
- expr. span , help, suggestion, applicability
1390
- ) ;
1391
- }
1373
+ self . maybe_suggest_array_indexing ( & mut err, expr, base, field, len) ;
1392
1374
}
1393
1375
ty:: RawPtr ( ..) => {
1394
1376
let base = self . tcx . sess . source_map ( )
@@ -1417,7 +1399,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1417
1399
1418
1400
fn ban_private_field_access (
1419
1401
& self ,
1420
- expr : & ' tcx hir:: Expr ,
1402
+ expr : & hir:: Expr ,
1421
1403
expr_t : Ty < ' tcx > ,
1422
1404
field : ast:: Ident ,
1423
1405
base_did : DefId ,
@@ -1446,7 +1428,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1446
1428
err. emit ( ) ;
1447
1429
}
1448
1430
1449
- fn ban_take_value_of_method ( & self , expr : & ' tcx hir:: Expr , expr_t : Ty < ' tcx > , field : ast:: Ident ) {
1431
+ fn ban_take_value_of_method ( & self , expr : & hir:: Expr , expr_t : Ty < ' tcx > , field : ast:: Ident ) {
1450
1432
let mut err = type_error_struct ! (
1451
1433
self . tcx( ) . sess,
1452
1434
field. span,
@@ -1472,6 +1454,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1472
1454
err. emit ( ) ;
1473
1455
}
1474
1456
1457
+ fn maybe_suggest_array_indexing (
1458
+ & self ,
1459
+ err : & mut DiagnosticBuilder < ' _ > ,
1460
+ expr : & hir:: Expr ,
1461
+ base : & hir:: Expr ,
1462
+ field : ast:: Ident ,
1463
+ len : & ty:: Const < ' tcx > ,
1464
+ ) {
1465
+ if let ( Some ( len) , Ok ( user_index) ) = (
1466
+ len. try_eval_usize ( self . tcx , self . param_env ) ,
1467
+ field. as_str ( ) . parse :: < u64 > ( )
1468
+ ) {
1469
+ let base = self . tcx . sess . source_map ( )
1470
+ . span_to_snippet ( base. span )
1471
+ . unwrap_or_else ( |_| self . tcx . hir ( ) . hir_to_pretty_string ( base. hir_id ) ) ;
1472
+ let help = "instead of using tuple indexing, use array indexing" ;
1473
+ let suggestion = format ! ( "{}[{}]" , base, field) ;
1474
+ let applicability = if len < user_index {
1475
+ Applicability :: MachineApplicable
1476
+ } else {
1477
+ Applicability :: MaybeIncorrect
1478
+ } ;
1479
+ err. span_suggestion ( expr. span , help, suggestion, applicability) ;
1480
+ }
1481
+ }
1482
+
1475
1483
fn no_such_field_err < T : Display > ( & self , span : Span , field : T , expr_t : & ty:: TyS < ' _ > )
1476
1484
-> DiagnosticBuilder < ' _ > {
1477
1485
type_error_struct ! ( self . tcx( ) . sess, span, expr_t, E0609 ,
0 commit comments