From 80e57e223e98f7075cc4af83fa0eef948493d0df Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Tue, 15 Feb 2022 20:05:24 -0800 Subject: [PATCH] Reduce rightward drift --- .../rustc_parse/src/parser/diagnostics.rs | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index b29ea808d32..89cb1c89f81 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1255,41 +1255,40 @@ impl<'a> Parser<'a> { ); err.span_label(op_span, &format!("not a valid {} operator", kind.fixity)); - if let ExprKind::Path(_, path) = &base.kind { - if let [segment] = path.segments.as_slice() { - let ident = segment.ident; - // (pre, post) - let spans = match kind.fixity { - UnaryFixity::Pre => (op_span, ident.span.shrink_to_hi()), - UnaryFixity::Post => (ident.span.shrink_to_lo(), op_span), - }; + let help_base_case = |mut err: DiagnosticBuilder<'_>, base| { + err.help(&format!("use `{}= 1` instead", kind.op.chr())); + err.emit(); + Ok(base) + }; - if !ident.is_reserved() { - if kind.standalone { - return self.inc_dec_standalone_recovery(base, err, kind, ident, spans); - } else { - match kind.fixity { - UnaryFixity::Pre => { - return self.prefix_inc_dec_suggest_and_recover( - base, err, kind, ident, spans, - ); - } - UnaryFixity::Post => { - return self.postfix_inc_dec_suggest_and_recover( - base, err, kind, ident, spans, - ); - } - } - } + let ExprKind::Path(_, path) = &base.kind + else { return help_base_case(err, base) }; + let [segment] = path.segments.as_slice() + else { return help_base_case(err, base) }; + let ident = segment.ident; + + // (pre, post) + let spans = match kind.fixity { + UnaryFixity::Pre => (op_span, ident.span.shrink_to_hi()), + UnaryFixity::Post => (ident.span.shrink_to_lo(), op_span), + }; + + if ident.is_reserved() { + return help_base_case(err, base); + } + + if kind.standalone { + self.inc_dec_standalone_recovery(base, err, kind, ident, spans) + } else { + match kind.fixity { + UnaryFixity::Pre => { + self.prefix_inc_dec_suggest_and_recover(base, err, kind, ident, spans) + } + UnaryFixity::Post => { + self.postfix_inc_dec_suggest_and_recover(base, err, kind, ident, spans) } } } - - // base case - err.help(&format!("use `{}= 1` instead", kind.op.chr())); - err.emit(); - - Ok(base) } fn prefix_inc_dec_suggest_and_recover(