Check for missing space between fat arrow and range pattern
This commit is contained in:
parent
d6f0642827
commit
4bfab39f9b
4 changed files with 59 additions and 14 deletions
|
@ -3168,7 +3168,7 @@ impl<'a> Parser<'a> {
|
||||||
limits: RangeLimits,
|
limits: RangeLimits,
|
||||||
) -> ExprKind {
|
) -> ExprKind {
|
||||||
if end.is_none() && limits == RangeLimits::Closed {
|
if end.is_none() && limits == RangeLimits::Closed {
|
||||||
self.inclusive_range_with_incorrect_end(self.prev_token.span);
|
self.inclusive_range_with_incorrect_end();
|
||||||
ExprKind::Err
|
ExprKind::Err
|
||||||
} else {
|
} else {
|
||||||
ExprKind::Range(start, end, limits)
|
ExprKind::Range(start, end, limits)
|
||||||
|
|
|
@ -746,32 +746,38 @@ impl<'a> Parser<'a> {
|
||||||
// Parsing e.g. `X..`.
|
// Parsing e.g. `X..`.
|
||||||
if let RangeEnd::Included(_) = re.node {
|
if let RangeEnd::Included(_) = re.node {
|
||||||
// FIXME(Centril): Consider semantic errors instead in `ast_validation`.
|
// FIXME(Centril): Consider semantic errors instead in `ast_validation`.
|
||||||
self.inclusive_range_with_incorrect_end(re.span);
|
self.inclusive_range_with_incorrect_end();
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
Ok(PatKind::Range(Some(begin), end, re))
|
Ok(PatKind::Range(Some(begin), end, re))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn inclusive_range_with_incorrect_end(&mut self, span: Span) {
|
pub(super) fn inclusive_range_with_incorrect_end(&mut self) {
|
||||||
let tok = &self.token;
|
let tok = &self.token;
|
||||||
|
let span = self.prev_token.span;
|
||||||
// If the user typed "..==" instead of "..=", we want to give them
|
// If the user typed "..==" instead of "..=", we want to give them
|
||||||
// a specific error message telling them to use "..=".
|
// a specific error message telling them to use "..=".
|
||||||
|
// If they typed "..=>", suggest they use ".. =>".
|
||||||
// Otherwise, we assume that they meant to type a half open exclusive
|
// Otherwise, we assume that they meant to type a half open exclusive
|
||||||
// range and give them an error telling them to do that instead.
|
// range and give them an error telling them to do that instead.
|
||||||
if matches!(tok.kind, token::Eq) && tok.span.lo() == span.hi() {
|
let no_space = tok.span.lo() == span.hi();
|
||||||
let span_with_eq = span.to(tok.span);
|
match tok.kind {
|
||||||
|
token::Eq if no_space => {
|
||||||
|
let span_with_eq = span.to(tok.span);
|
||||||
|
|
||||||
// Ensure the user doesn't receive unhelpful unexpected token errors
|
// Ensure the user doesn't receive unhelpful unexpected token errors
|
||||||
self.bump();
|
self.bump();
|
||||||
if self.is_pat_range_end_start(0) {
|
if self.is_pat_range_end_start(0) {
|
||||||
let _ = self.parse_pat_range_end().map_err(|e| e.cancel());
|
let _ = self.parse_pat_range_end().map_err(|e| e.cancel());
|
||||||
|
}
|
||||||
|
|
||||||
|
self.error_inclusive_range_with_extra_equals(span_with_eq);
|
||||||
}
|
}
|
||||||
|
token::Gt if no_space => {
|
||||||
self.error_inclusive_range_with_extra_equals(span_with_eq);
|
self.error_inclusive_range_match_arrow(span);
|
||||||
} else {
|
}
|
||||||
self.error_inclusive_range_with_no_end(span);
|
_ => self.error_inclusive_range_with_no_end(span),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -782,6 +788,18 @@ impl<'a> Parser<'a> {
|
||||||
.emit();
|
.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn error_inclusive_range_match_arrow(&self, span: Span) {
|
||||||
|
let without_eq = span.with_hi(span.hi() - rustc_span::BytePos(1));
|
||||||
|
self.struct_span_err(span, "unexpected `=>` after open range")
|
||||||
|
.span_suggestion_verbose(
|
||||||
|
without_eq.shrink_to_hi(),
|
||||||
|
"add a space between the pattern and `=>`",
|
||||||
|
" ",
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
)
|
||||||
|
.emit();
|
||||||
|
}
|
||||||
|
|
||||||
fn error_inclusive_range_with_no_end(&self, span: Span) {
|
fn error_inclusive_range_with_no_end(&self, span: Span) {
|
||||||
struct_span_err!(self.sess.span_diagnostic, span, E0586, "inclusive range with no end")
|
struct_span_err!(self.sess.span_diagnostic, span, E0586, "inclusive range with no end")
|
||||||
.span_suggestion_short(span, "use `..` instead", "..", Applicability::MachineApplicable)
|
.span_suggestion_short(span, "use `..` instead", "..", Applicability::MachineApplicable)
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
fn main() {
|
||||||
|
let x = 42;
|
||||||
|
match x {
|
||||||
|
0..=73 => {},
|
||||||
|
74..=> {}, //~ ERROR unexpected `=>` after open range
|
||||||
|
//~^ ERROR expected one of `=>`, `if`, or `|`, found `>`
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
error: unexpected `=>` after open range
|
||||||
|
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:11
|
||||||
|
|
|
||||||
|
LL | 74..=> {},
|
||||||
|
| ^^^
|
||||||
|
|
|
||||||
|
help: add a space between the pattern and `=>`
|
||||||
|
|
|
||||||
|
LL | 74.. => {},
|
||||||
|
| +
|
||||||
|
|
||||||
|
error: expected one of `=>`, `if`, or `|`, found `>`
|
||||||
|
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:14
|
||||||
|
|
|
||||||
|
LL | 74..=> {},
|
||||||
|
| ^ expected one of `=>`, `if`, or `|`
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue