@@ -1370,25 +1370,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13701370 } ;
13711371 }
13721372 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) ;
13921374 }
13931375 ty:: RawPtr ( ..) => {
13941376 let base = self . tcx . sess . source_map ( )
@@ -1417,7 +1399,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14171399
14181400 fn ban_private_field_access (
14191401 & self ,
1420- expr : & ' tcx hir:: Expr ,
1402+ expr : & hir:: Expr ,
14211403 expr_t : Ty < ' tcx > ,
14221404 field : ast:: Ident ,
14231405 base_did : DefId ,
@@ -1446,7 +1428,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14461428 err. emit ( ) ;
14471429 }
14481430
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 ) {
14501432 let mut err = type_error_struct ! (
14511433 self . tcx( ) . sess,
14521434 field. span,
@@ -1472,6 +1454,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14721454 err. emit ( ) ;
14731455 }
14741456
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+
14751483 fn no_such_field_err < T : Display > ( & self , span : Span , field : T , expr_t : & ty:: TyS < ' _ > )
14761484 -> DiagnosticBuilder < ' _ > {
14771485 type_error_struct ! ( self . tcx( ) . sess, span, expr_t, E0609 ,
0 commit comments