@@ -160,8 +160,10 @@ impl AttemptLocalParseRecovery {
160
160
/// C-style `i++`, `--i`, etc.
161
161
#[ derive( Debug , Copy , Clone ) ]
162
162
struct IncDecRecovery {
163
- /// This increment/decrement is not a subexpression.
164
- standalone : bool ,
163
+ /// Is this increment/decrement its own statement?
164
+ ///
165
+ /// This is `None` when we are unsure.
166
+ standalone : Option < bool > ,
165
167
/// Is this an increment or decrement?
166
168
op : IncOrDec ,
167
169
/// Is this pre- or postfix?
@@ -1225,7 +1227,7 @@ impl<'a> Parser<'a> {
1225
1227
prev_is_semi : bool ,
1226
1228
) -> PResult < ' a , P < Expr > > {
1227
1229
let kind = IncDecRecovery {
1228
- standalone : prev_is_semi,
1230
+ standalone : Some ( prev_is_semi) ,
1229
1231
op : IncOrDec :: Inc ,
1230
1232
fixity : UnaryFixity :: Pre ,
1231
1233
} ;
@@ -1237,13 +1239,9 @@ impl<'a> Parser<'a> {
1237
1239
& mut self ,
1238
1240
operand_expr : P < Expr > ,
1239
1241
op_span : Span ,
1240
- prev_is_semi : bool ,
1241
1242
) -> PResult < ' a , P < Expr > > {
1242
- let kind = IncDecRecovery {
1243
- standalone : prev_is_semi,
1244
- op : IncOrDec :: Inc ,
1245
- fixity : UnaryFixity :: Post ,
1246
- } ;
1243
+ let kind =
1244
+ IncDecRecovery { standalone : None , op : IncOrDec :: Inc , fixity : UnaryFixity :: Post } ;
1247
1245
1248
1246
self . recover_from_inc_dec ( operand_expr, kind, op_span)
1249
1247
}
@@ -1272,25 +1270,44 @@ impl<'a> Parser<'a> {
1272
1270
UnaryFixity :: Post => ( base. span . shrink_to_lo ( ) , op_span) ,
1273
1271
} ;
1274
1272
1275
- if kind. standalone {
1276
- self . inc_dec_standalone_recovery ( err, kind, spans)
1277
- } else {
1278
- let Ok ( base_src) = self . span_to_snippet ( base. span )
1279
- else { return help_base_case ( err, base) } ;
1280
- match kind. fixity {
1281
- UnaryFixity :: Pre => self . prefix_inc_dec_suggest ( base_src, err, kind, spans) ,
1282
- UnaryFixity :: Post => self . postfix_inc_dec_suggest ( base_src, err, kind, spans) ,
1273
+ match kind. standalone {
1274
+ Some ( true ) => self . inc_dec_standalone_recovery ( & mut err, kind, spans, false ) ,
1275
+ Some ( false ) => {
1276
+ let Ok ( base_src) = self . span_to_snippet ( base. span )
1277
+ else { return help_base_case ( err, base) } ;
1278
+ match kind. fixity {
1279
+ UnaryFixity :: Pre => {
1280
+ self . prefix_inc_dec_suggest ( base_src, & mut err, kind, spans)
1281
+ }
1282
+ UnaryFixity :: Post => {
1283
+ self . postfix_inc_dec_suggest ( base_src, & mut err, kind, spans)
1284
+ }
1285
+ }
1286
+ }
1287
+ None => {
1288
+ let Ok ( base_src) = self . span_to_snippet ( base. span )
1289
+ else { return help_base_case ( err, base) } ;
1290
+ match kind. fixity {
1291
+ UnaryFixity :: Pre => {
1292
+ self . prefix_inc_dec_suggest ( base_src, & mut err, kind, spans)
1293
+ }
1294
+ UnaryFixity :: Post => {
1295
+ self . postfix_inc_dec_suggest ( base_src, & mut err, kind, spans)
1296
+ }
1297
+ }
1298
+ self . inc_dec_standalone_recovery ( & mut err, kind, spans, true )
1283
1299
}
1284
1300
}
1301
+ Err ( err)
1285
1302
}
1286
1303
1287
1304
fn prefix_inc_dec_suggest (
1288
1305
& mut self ,
1289
1306
base_src : String ,
1290
- mut err : DiagnosticBuilder < ' a > ,
1307
+ err : & mut DiagnosticBuilder < ' a > ,
1291
1308
kind : IncDecRecovery ,
1292
1309
( pre_span, post_span) : ( Span , Span ) ,
1293
- ) -> PResult < ' a , P < Expr > > {
1310
+ ) {
1294
1311
err. multipart_suggestion (
1295
1312
& format ! ( "use `{}= 1` instead" , kind. op. chr( ) ) ,
1296
1313
vec ! [
@@ -1299,16 +1316,15 @@ impl<'a> Parser<'a> {
1299
1316
] ,
1300
1317
Applicability :: MachineApplicable ,
1301
1318
) ;
1302
- Err ( err)
1303
1319
}
1304
1320
1305
1321
fn postfix_inc_dec_suggest (
1306
1322
& mut self ,
1307
1323
base_src : String ,
1308
- mut err : DiagnosticBuilder < ' a > ,
1324
+ err : & mut DiagnosticBuilder < ' a > ,
1309
1325
kind : IncDecRecovery ,
1310
1326
( pre_span, post_span) : ( Span , Span ) ,
1311
- ) -> PResult < ' a , P < Expr > > {
1327
+ ) {
1312
1328
err. multipart_suggestion (
1313
1329
& format ! ( "use `{}= 1` instead" , kind. op. chr( ) ) ,
1314
1330
vec ! [
@@ -1317,21 +1333,31 @@ impl<'a> Parser<'a> {
1317
1333
] ,
1318
1334
Applicability :: MachineApplicable ,
1319
1335
) ;
1320
- Err ( err)
1321
1336
}
1322
1337
1323
1338
fn inc_dec_standalone_recovery (
1324
1339
& mut self ,
1325
- mut err : DiagnosticBuilder < ' a > ,
1340
+ err : & mut DiagnosticBuilder < ' a > ,
1326
1341
kind : IncDecRecovery ,
1327
1342
( pre_span, post_span) : ( Span , Span ) ,
1328
- ) -> PResult < ' a , P < Expr > > {
1343
+ maybe_not_standalone : bool ,
1344
+ ) {
1345
+ let msg = if maybe_not_standalone {
1346
+ "or, if you don't need to use it as an expression, change it to this" . to_owned ( )
1347
+ } else {
1348
+ format ! ( "use `{}= 1` instead" , kind. op. chr( ) )
1349
+ } ;
1350
+ let applicability = if maybe_not_standalone {
1351
+ // FIXME: Unspecified isn't right, but it's the least wrong option
1352
+ Applicability :: Unspecified
1353
+ } else {
1354
+ Applicability :: MachineApplicable
1355
+ } ;
1329
1356
err. multipart_suggestion (
1330
- & format ! ( "use `{}= 1` instead" , kind . op . chr ( ) ) ,
1357
+ & msg ,
1331
1358
vec ! [ ( pre_span, String :: new( ) ) , ( post_span, format!( " {}= 1" , kind. op. chr( ) ) ) ] ,
1332
- Applicability :: MachineApplicable ,
1359
+ applicability ,
1333
1360
) ;
1334
- Err ( err)
1335
1361
}
1336
1362
1337
1363
/// Tries to recover from associated item paths like `[T]::AssocItem` / `(T, U)::AssocItem`.
0 commit comments