Suggest wrapping mac args in parens rather than the whole expression

This commit is contained in:
Michael Goulet 2023-12-18 03:01:05 +00:00
parent c1c7707238
commit ec263df5e4
5 changed files with 45 additions and 23 deletions

View File

@ -722,6 +722,8 @@ parse_sugg_turbofish_syntax = use `::<...>` instead of `<...>` to specify lifeti
parse_sugg_wrap_expression_in_parentheses = wrap the expression in parentheses parse_sugg_wrap_expression_in_parentheses = wrap the expression in parentheses
parse_sugg_wrap_macro_in_parentheses = use parentheses instead of braces for this macro
parse_sugg_wrap_pattern_in_parens = wrap the pattern in parentheses parse_sugg_wrap_pattern_in_parens = wrap the pattern in parentheses
parse_switch_mut_let_order = parse_switch_mut_let_order =

View File

@ -722,19 +722,32 @@ pub(crate) struct LabeledLoopInBreak {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
#[subdiagnostic] #[subdiagnostic]
pub sub: WrapExpressionInParentheses, pub sub: WrapInParentheses,
} }
#[derive(Subdiagnostic)] #[derive(Subdiagnostic)]
pub(crate) enum WrapInParentheses {
#[multipart_suggestion( #[multipart_suggestion(
parse_sugg_wrap_expression_in_parentheses, parse_sugg_wrap_expression_in_parentheses,
applicability = "machine-applicable" applicability = "machine-applicable"
)] )]
pub(crate) struct WrapExpressionInParentheses { Expression {
#[suggestion_part(code = "(")] #[suggestion_part(code = "(")]
pub left: Span, left: Span,
#[suggestion_part(code = ")")] #[suggestion_part(code = ")")]
pub right: Span, right: Span,
},
#[multipart_suggestion(
parse_sugg_wrap_macro_in_parentheses,
applicability = "machine-applicable"
)]
MacroArgs {
#[suggestion_part(code = "(")]
left: Span,
#[suggestion_part(code = ")")]
right: Span,
},
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]
@ -936,7 +949,7 @@ pub(crate) struct InvalidExpressionInLetElse {
pub span: Span, pub span: Span,
pub operator: &'static str, pub operator: &'static str,
#[subdiagnostic] #[subdiagnostic]
pub sugg: WrapExpressionInParentheses, pub sugg: WrapInParentheses,
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]
@ -945,7 +958,7 @@ pub(crate) struct InvalidCurlyInLetElse {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
#[subdiagnostic] #[subdiagnostic]
pub sugg: WrapExpressionInParentheses, pub sugg: WrapInParentheses,
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]

View File

@ -1844,7 +1844,7 @@ fn parse_expr_break(&mut self) -> PResult<'a, P<Expr>> {
let lexpr = self.parse_expr_labeled(label, true)?; let lexpr = self.parse_expr_labeled(label, true)?;
self.dcx().emit_err(errors::LabeledLoopInBreak { self.dcx().emit_err(errors::LabeledLoopInBreak {
span: lexpr.span, span: lexpr.span,
sub: errors::WrapExpressionInParentheses { sub: errors::WrapInParentheses::Expression {
left: lexpr.span.shrink_to_lo(), left: lexpr.span.shrink_to_lo(),
right: lexpr.span.shrink_to_hi(), right: lexpr.span.shrink_to_hi(),
}, },

View File

@ -389,7 +389,7 @@ fn check_let_else_init_bool_expr(&self, init: &ast::Expr) {
self.dcx().emit_err(errors::InvalidExpressionInLetElse { self.dcx().emit_err(errors::InvalidExpressionInLetElse {
span: init.span, span: init.span,
operator: op.node.as_str(), operator: op.node.as_str(),
sugg: errors::WrapExpressionInParentheses { sugg: errors::WrapInParentheses::Expression {
left: init.span.shrink_to_lo(), left: init.span.shrink_to_lo(),
right: init.span.shrink_to_hi(), right: init.span.shrink_to_hi(),
}, },
@ -400,12 +400,19 @@ fn check_let_else_init_bool_expr(&self, init: &ast::Expr) {
fn check_let_else_init_trailing_brace(&self, init: &ast::Expr) { fn check_let_else_init_trailing_brace(&self, init: &ast::Expr) {
if let Some(trailing) = classify::expr_trailing_brace(init) { if let Some(trailing) = classify::expr_trailing_brace(init) {
self.dcx().emit_err(errors::InvalidCurlyInLetElse { let sugg = match &trailing.kind {
span: trailing.span.with_lo(trailing.span.hi() - BytePos(1)), ExprKind::MacCall(mac) => errors::WrapInParentheses::MacroArgs {
sugg: errors::WrapExpressionInParentheses { left: mac.args.dspan.open,
right: mac.args.dspan.close,
},
_ => errors::WrapInParentheses::Expression {
left: trailing.span.shrink_to_lo(), left: trailing.span.shrink_to_lo(),
right: trailing.span.shrink_to_hi(), right: trailing.span.shrink_to_hi(),
}, },
};
self.dcx().emit_err(errors::InvalidCurlyInLetElse {
span: trailing.span.with_lo(trailing.span.hi() - BytePos(1)),
sugg,
}); });
} }
} }

View File

@ -234,10 +234,10 @@ error: right curly brace `}` before `else` in a `let...else` statement not allow
LL | let bad = format_args! {""} else { return; }; LL | let bad = format_args! {""} else { return; };
| ^ | ^
| |
help: wrap the expression in parentheses help: use parentheses instead of braces for this macro
| |
LL | let bad = (format_args! {""}) else { return; }; LL | let bad = format_args! ("") else { return; };
| + + | ~ ~
error: right curly brace `}` before `else` in a `let...else` statement not allowed error: right curly brace `}` before `else` in a `let...else` statement not allowed
--> $DIR/bad-let-else-statement.rs:181:25 --> $DIR/bad-let-else-statement.rs:181:25
@ -249,10 +249,10 @@ LL | b!(1); b!(2);
| ----- in this macro invocation | ----- in this macro invocation
| |
= note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap the expression in parentheses help: use parentheses instead of braces for this macro
| |
LL | let x = (a! {}) else { return; }; LL | let x = a! () else { return; };
| + + | ~~
error: aborting due to 19 previous errors error: aborting due to 19 previous errors