1
Fork 0

Detect for _ in in bar {} typo

This commit is contained in:
Esteban Küber 2018-09-19 16:23:21 -07:00
parent 20dc0c5070
commit 06d577d8b2
4 changed files with 51 additions and 13 deletions

View file

@ -2702,8 +2702,8 @@ impl<'a> Parser<'a> {
token::Literal(token::Float(n), _suf) => { token::Literal(token::Float(n), _suf) => {
self.bump(); self.bump();
let fstr = n.as_str(); let fstr = n.as_str();
let mut err = self.diagnostic().struct_span_err(self.prev_span, let mut err = self.diagnostic()
&format!("unexpected token: `{}`", n)); .struct_span_err(self.prev_span, &format!("unexpected token: `{}`", n));
err.span_label(self.prev_span, "unexpected token"); err.span_label(self.prev_span, "unexpected token");
if fstr.chars().all(|x| "0123456789.".contains(x)) { if fstr.chars().all(|x| "0123456789.".contains(x)) {
let float = match fstr.parse::<f64>().ok() { let float = match fstr.parse::<f64>().ok() {
@ -2863,8 +2863,8 @@ impl<'a> Parser<'a> {
let e = self.parse_prefix_expr(None); let e = self.parse_prefix_expr(None);
let (span, e) = self.interpolated_or_expr_span(e)?; let (span, e) = self.interpolated_or_expr_span(e)?;
let span_of_tilde = lo; let span_of_tilde = lo;
let mut err = self.diagnostic().struct_span_err(span_of_tilde, let mut err = self.diagnostic()
"`~` cannot be used as a unary operator"); .struct_span_err(span_of_tilde, "`~` cannot be used as a unary operator");
err.span_suggestion_short_with_applicability( err.span_suggestion_short_with_applicability(
span_of_tilde, span_of_tilde,
"use `!` to perform bitwise negation", "use `!` to perform bitwise negation",
@ -3422,6 +3422,24 @@ impl<'a> Parser<'a> {
); );
err.emit(); err.emit();
} }
let in_span = self.prev_span;
if self.eat_keyword(keywords::In) {
// a common typo: `for _ in in bar {}`
let mut err = self.sess.span_diagnostic.struct_span_err(
self.prev_span,
"expected iterable, found keyword `in`",
);
err.span_suggestion_short_with_applicability(
in_span.until(self.prev_span),
"remove the duplicated `in`",
String::new(),
Applicability::MachineApplicable,
);
err.note("if you meant to use emplacement syntax, it is obsolete (for now, anyway)");
err.note("for more information on the status of emplacement syntax, see <\
https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>");
err.emit();
}
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?; let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
attrs.extend(iattrs); attrs.extend(iattrs);
@ -4760,12 +4778,9 @@ impl<'a> Parser<'a> {
if !self.eat(&token::OpenDelim(token::Brace)) { if !self.eat(&token::OpenDelim(token::Brace)) {
let sp = self.span; let sp = self.span;
let tok = self.this_token_to_string(); let tok = self.this_token_to_string();
let mut do_not_suggest_help = false;
let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok)); let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok));
if self.token.is_keyword(keywords::In) || self.token == token::Colon { let do_not_suggest_help =
do_not_suggest_help = true; self.token.is_keyword(keywords::In) || self.token == token::Colon;
e.span_label(sp, "expected `{`");
}
if self.token.is_ident_named("and") { if self.token.is_ident_named("and") {
e.span_suggestion_short_with_applicability( e.span_suggestion_short_with_applicability(
@ -4796,6 +4811,7 @@ impl<'a> Parser<'a> {
|| do_not_suggest_help { || do_not_suggest_help {
// if the next token is an open brace (e.g., `if a b {`), the place- // if the next token is an open brace (e.g., `if a b {`), the place-
// inside-a-block suggestion would be more likely wrong than right // inside-a-block suggestion would be more likely wrong than right
e.span_label(sp, "expected `{`");
return Err(e); return Err(e);
} }
let mut stmt_span = stmt.span; let mut stmt_span = stmt.span;

View file

@ -2,16 +2,20 @@ error: expected `{`, found `and`
--> $DIR/issue-54109-and_instead_of_ampersands.rs:14:10 --> $DIR/issue-54109-and_instead_of_ampersands.rs:14:10
| |
LL | if a and b { LL | if a and b {
| -- ^^^ help: use `&&` instead of `and` for the boolean operator | -- ^^^
| | | | |
| | expected `{`
| | help: use `&&` instead of `and` for the boolean operator
| this `if` statement has a condition, but no block | this `if` statement has a condition, but no block
error: expected `{`, found `or` error: expected `{`, found `or`
--> $DIR/issue-54109-and_instead_of_ampersands.rs:23:10 --> $DIR/issue-54109-and_instead_of_ampersands.rs:23:10
| |
LL | if a or b { LL | if a or b {
| -- ^^ help: use `||` instead of `or` for the boolean operator | -- ^^
| | | | |
| | expected `{`
| | help: use `||` instead of `or` for the boolean operator
| this `if` statement has a condition, but no block | this `if` statement has a condition, but no block
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `and` error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `and`

View file

@ -0,0 +1,5 @@
fn main() {
for i in in 1..2 {
println!("{}", i);
}
}

View file

@ -0,0 +1,13 @@
error: expected iterable, found keyword `in`
--> $DIR/if-in-in.rs:2:14
|
LL | for i in in 1..2 {
| ---^^
| |
| help: remove the duplicated `in`
|
= note: if you meant to use emplacement syntax, it is obsolete (for now, anyway)
= note: for more information on the status of emplacement syntax, see <https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>
error: aborting due to previous error