More accurate incorrect use of await suggestion

This commit is contained in:
Esteban Küber 2024-07-11 22:07:11 +00:00
parent b5f94c61f7
commit 377d14be88
5 changed files with 76 additions and 56 deletions

View File

@ -323,10 +323,10 @@ parse_incorrect_semicolon =
.suggestion = remove this semicolon .suggestion = remove this semicolon
.help = {$name} declarations are not followed by a semicolon .help = {$name} declarations are not followed by a semicolon
parse_incorrect_use_of_await = parse_incorrect_use_of_await = incorrect use of `await`
incorrect use of `await`
.parentheses_suggestion = `await` is not a method call, remove the parentheses .parentheses_suggestion = `await` is not a method call, remove the parentheses
.postfix_suggestion = `await` is a postfix operation
parse_incorrect_use_of_await_postfix_suggestion = `await` is a postfix operation
parse_incorrect_visibility_restriction = incorrect visibility restriction parse_incorrect_visibility_restriction = incorrect visibility restriction
.help = some possible visibility restrictions are: .help = some possible visibility restrictions are:

View File

@ -103,19 +103,26 @@ pub(crate) struct IncorrectUseOfAwait {
pub span: Span, pub span: Span,
} }
#[derive(Subdiagnostic)]
#[multipart_suggestion(
parse_incorrect_use_of_await_postfix_suggestion,
applicability = "machine-applicable"
)]
pub(crate) struct AwaitSuggestion {
#[suggestion_part(code = "")]
pub removal: Span,
#[suggestion_part(code = ".await{question_mark}")]
pub dot_await: Span,
pub question_mark: &'static str,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(parse_incorrect_use_of_await)] #[diag(parse_incorrect_use_of_await)]
pub(crate) struct IncorrectAwait { pub(crate) struct IncorrectAwait {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
#[suggestion( #[subdiagnostic]
parse_postfix_suggestion, pub suggestion: AwaitSuggestion,
style = "verbose",
code = "{expr}.await{question_mark}"
)]
pub sugg_span: (Span, Applicability),
pub expr: String,
pub question_mark: &'static str,
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]

View File

@ -3,8 +3,8 @@ use super::{
BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep, TokenType, BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep, TokenType,
}; };
use crate::errors::{ use crate::errors::{
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, BadQPathStage2, AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, AwaitSuggestion,
BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained, BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained,
ComparisonOperatorsCannotBeChainedSugg, ConstGenericWithoutBraces, ComparisonOperatorsCannotBeChainedSugg, ConstGenericWithoutBraces,
ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType,
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
@ -1959,18 +1959,14 @@ impl<'a> Parser<'a> {
is_question: bool, is_question: bool,
) -> (Span, ErrorGuaranteed) { ) -> (Span, ErrorGuaranteed) {
let span = lo.to(hi); let span = lo.to(hi);
let applicability = match expr.kind {
ExprKind::Try(_) => Applicability::MaybeIncorrect, // `await <expr>?`
_ => Applicability::MachineApplicable,
};
let guar = self.dcx().emit_err(IncorrectAwait { let guar = self.dcx().emit_err(IncorrectAwait {
span, span,
sugg_span: (span, applicability), suggestion: AwaitSuggestion {
expr: self.span_to_snippet(expr.span).unwrap_or_else(|_| pprust::expr_to_string(expr)), removal: lo.until(expr.span),
question_mark: if is_question { "?" } else { "" }, dot_await: expr.span.shrink_to_hi(),
question_mark: if is_question { "?" } else { "" },
},
}); });
(span, guar) (span, guar)
} }

View File

@ -6,8 +6,9 @@ LL | let _ = await bar();
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar().await; LL - let _ = await bar();
| ~~~~~~~~~~~ LL + let _ = bar().await;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:12:13 --> $DIR/incorrect-syntax-suggestions.rs:12:13
@ -17,8 +18,9 @@ LL | let _ = await? bar();
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar().await?; LL - let _ = await? bar();
| ~~~~~~~~~~~~ LL + let _ = bar().await?;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:16:13 --> $DIR/incorrect-syntax-suggestions.rs:16:13
@ -28,8 +30,9 @@ LL | let _ = await bar()?;
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar()?.await; LL - let _ = await bar()?;
| ~~~~~~~~~~~~ LL + let _ = bar()?.await;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:20:13 --> $DIR/incorrect-syntax-suggestions.rs:20:13
@ -39,8 +42,9 @@ LL | let _ = await { bar() };
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = { bar() }.await; LL - let _ = await { bar() };
| ~~~~~~~~~~~~~~~ LL + let _ = { bar() }.await;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:24:13 --> $DIR/incorrect-syntax-suggestions.rs:24:13
@ -50,8 +54,9 @@ LL | let _ = await(bar());
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = (bar()).await; LL - let _ = await(bar());
| ~~~~~~~~~~~~~ LL + let _ = (bar()).await;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:28:13 --> $DIR/incorrect-syntax-suggestions.rs:28:13
@ -61,8 +66,9 @@ LL | let _ = await { bar() }?;
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = { bar() }.await?; LL - let _ = await { bar() }?;
| ~~~~~~~~~~~~~~~ LL + let _ = { bar() }.await?;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:32:14 --> $DIR/incorrect-syntax-suggestions.rs:32:14
@ -72,8 +78,9 @@ LL | let _ = (await bar())?;
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = (bar().await)?; LL - let _ = (await bar())?;
| ~~~~~~~~~~~ LL + let _ = (bar().await)?;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:36:24 --> $DIR/incorrect-syntax-suggestions.rs:36:24
@ -107,8 +114,9 @@ LL | let _ = await bar();
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar().await; LL - let _ = await bar();
| ~~~~~~~~~~~ LL + let _ = bar().await;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:56:13 --> $DIR/incorrect-syntax-suggestions.rs:56:13
@ -118,8 +126,9 @@ LL | let _ = await? bar();
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar().await?; LL - let _ = await? bar();
| ~~~~~~~~~~~~ LL + let _ = bar().await?;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:60:13 --> $DIR/incorrect-syntax-suggestions.rs:60:13
@ -129,8 +138,9 @@ LL | let _ = await bar()?;
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar()?.await; LL - let _ = await bar()?;
| ~~~~~~~~~~~~ LL + let _ = bar()?.await;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:64:14 --> $DIR/incorrect-syntax-suggestions.rs:64:14
@ -140,8 +150,9 @@ LL | let _ = (await bar())?;
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = (bar().await)?; LL - let _ = (await bar())?;
| ~~~~~~~~~~~ LL + let _ = (bar().await)?;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:68:24 --> $DIR/incorrect-syntax-suggestions.rs:68:24
@ -175,8 +186,9 @@ LL | let _ = await!(bar());
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar().await; LL - let _ = await!(bar());
| ~~~~~~~~~~~ LL + let _ = bar().await);
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:105:13 --> $DIR/incorrect-syntax-suggestions.rs:105:13
@ -186,8 +198,9 @@ LL | let _ = await!(bar())?;
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar().await?; LL - let _ = await!(bar())?;
| ~~~~~~~~~~~ LL + let _ = bar().await)?;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:110:17 --> $DIR/incorrect-syntax-suggestions.rs:110:17
@ -197,8 +210,9 @@ LL | let _ = await!(bar())?;
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar().await?; LL - let _ = await!(bar())?;
| ~~~~~~~~~~~ LL + let _ = bar().await)?;
|
error: incorrect use of `await` error: incorrect use of `await`
--> $DIR/incorrect-syntax-suggestions.rs:117:17 --> $DIR/incorrect-syntax-suggestions.rs:117:17
@ -208,8 +222,9 @@ LL | let _ = await!(bar())?;
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | let _ = bar().await?; LL - let _ = await!(bar())?;
| ~~~~~~~~~~~ LL + let _ = bar().await)?;
|
error: expected expression, found `=>` error: expected expression, found `=>`
--> $DIR/incorrect-syntax-suggestions.rs:124:25 --> $DIR/incorrect-syntax-suggestions.rs:124:25
@ -227,8 +242,9 @@ LL | match await { await => () }
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | match { await => () }.await LL - match await { await => () }
| ~~~~~~~~~~~~~~~~~~~~~ LL + match { await => () }.await
|
error: expected one of `.`, `?`, `{`, or an operator, found `}` error: expected one of `.`, `?`, `{`, or an operator, found `}`
--> $DIR/incorrect-syntax-suggestions.rs:127:1 --> $DIR/incorrect-syntax-suggestions.rs:127:1

View File

@ -6,8 +6,9 @@ LL | await {}()
| |
help: `await` is a postfix operation help: `await` is a postfix operation
| |
LL | {}.await() LL - await {}()
| ~~~~~~~~ LL + {}.await()
|
error: aborting due to 1 previous error error: aborting due to 1 previous error