Use verbose suggestions and only match if the + is seen before a numeric literal

This commit is contained in:
Theodore Luo Wang 2021-09-04 22:35:59 -04:00
parent bc9877c5af
commit 65eb7e516c
9 changed files with 58 additions and 95 deletions

View File

@ -586,6 +586,10 @@ impl Token {
self.is_non_raw_ident_where(|id| id.name.is_bool_lit())
}
pub fn is_numeric_lit(&self) -> bool {
matches!(self.kind, Literal(Lit { kind: LitKind::Integer, ..}) | Literal(Lit { kind: LitKind::Float, ..}))
}
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool {
match self.ident() {

View File

@ -516,16 +516,15 @@ impl<'a> Parser<'a> {
token::BinOp(token::And) | token::AndAnd => {
make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo))
}
token::BinOp(token::Plus) => {
token::BinOp(token::Plus) if this.look_ahead(1, |tok| tok.is_numeric_lit()) => {
let mut err = this.struct_span_err(lo, "leading `+` is not supported");
err.span_label(lo, "unexpected `+`");
// a block on the LHS might have been intended to be an expression instead
let sp = this.sess.source_map().start_point(lo);
if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&sp) {
if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&lo) {
this.sess.expr_parentheses_needed(&mut err, *sp);
} else {
err.span_suggestion(
err.span_suggestion_verbose(
lo,
"try removing the `+`",
"".to_string(),

View File

@ -2,10 +2,13 @@ error: leading `+` is not supported
--> $DIR/issue-36499.rs:4:9
|
LL | 2 + +2;
| ^
| |
| unexpected `+`
| help: try removing the `+`
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - 2 + +2;
LL + 2 + 2;
|
error: aborting due to previous error

View File

@ -5,7 +5,7 @@
#![allow(unused_must_use)]
fn foo() -> i32 {
({2}) + {2} //~ ERROR leading `+` is not supported
({2}) + {2} //~ ERROR expected expression, found `+`
//~^ ERROR mismatched types
}
@ -16,7 +16,7 @@ fn bar() -> i32 {
fn zul() -> u32 {
let foo = 3;
({ 42 }) + foo; //~ ERROR leading `+` is not supported
({ 42 }) + foo; //~ ERROR expected expression, found `+`
//~^ ERROR mismatched types
32
}

View File

@ -5,7 +5,7 @@
#![allow(unused_must_use)]
fn foo() -> i32 {
{2} + {2} //~ ERROR leading `+` is not supported
{2} + {2} //~ ERROR expected expression, found `+`
//~^ ERROR mismatched types
}
@ -16,7 +16,7 @@ fn bar() -> i32 {
fn zul() -> u32 {
let foo = 3;
{ 42 } + foo; //~ ERROR leading `+` is not supported
{ 42 } + foo; //~ ERROR expected expression, found `+`
//~^ ERROR mismatched types
32
}

View File

@ -1,8 +1,8 @@
error: leading `+` is not supported
error: expected expression, found `+`
--> $DIR/expr-as-stmt.rs:8:9
|
LL | {2} + {2}
| ^ unexpected `+`
| ^ expected expression
|
help: parentheses are required to parse this as an expression
|
@ -20,11 +20,11 @@ help: parentheses are required to parse this as an expression
LL | ({2}) + 2
| + +
error: leading `+` is not supported
error: expected expression, found `+`
--> $DIR/expr-as-stmt.rs:19:12
|
LL | { 42 } + foo;
| ^ unexpected `+`
| ^ expected expression
|
help: parentheses are required to parse this as an expression
|

View File

@ -2,12 +2,7 @@
#[allow(unused_parens)]
fn main() {
let _ = 1; //~ ERROR leading `+` is not supported
let _ = -(1+2)*3; //~ ERROR leading `+` is not supported
let _ = --(1+2)*3; //~ ERROR leading `+` is not supported
//~| ERROR leading `+` is not supported
let _ = (1 + 2) * 3; //~ ERROR leading `+` is not supported
let _ = (1.0 + 2.0) * 3.0; //~ ERROR leading `+` is not supported
//~| ERROR leading `+` is not supported
let _ = (&"hello"); //~ ERROR leading `+` is not supported
let _ = [3, 4+6]; //~ ERROR leading `+` is not supported
//~| ERROR leading `+` is not supported
}

View File

@ -2,12 +2,7 @@
#[allow(unused_parens)]
fn main() {
let _ = +1; //~ ERROR leading `+` is not supported
let _ = -+(1+2)*3; //~ ERROR leading `+` is not supported
let _ = -+-+(1+2)*3; //~ ERROR leading `+` is not supported
//~| ERROR leading `+` is not supported
let _ = (1 + +2) * +3; //~ ERROR leading `+` is not supported
let _ = (1.0 + +2.0) * +3.0; //~ ERROR leading `+` is not supported
//~| ERROR leading `+` is not supported
let _ = (+&"hello"); //~ ERROR leading `+` is not supported
let _ = +[+3, 4+6]; //~ ERROR leading `+` is not supported
//~| ERROR leading `+` is not supported
let _ = [+3, 4+6]; //~ ERROR leading `+` is not supported
}

View File

@ -2,82 +2,49 @@ error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:4:13
|
LL | let _ = +1;
| ^
| |
| unexpected `+`
| help: try removing the `+`
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - let _ = +1;
LL + let _ = 1;
|
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:5:14
--> $DIR/issue-88276-unary-plus.rs:5:20
|
LL | let _ = -+(1+2)*3;
| ^
| |
| unexpected `+`
| help: try removing the `+`
LL | let _ = (1.0 + +2.0) * +3.0;
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - let _ = (1.0 + +2.0) * +3.0;
LL + let _ = (1.0 + 2.0) * +3.0;
|
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:6:14
--> $DIR/issue-88276-unary-plus.rs:5:28
|
LL | let _ = -+-+(1+2)*3;
| ^
| |
| unexpected `+`
| help: try removing the `+`
LL | let _ = (1.0 + +2.0) * +3.0;
| ^ unexpected `+`
|
help: try removing the `+`
|
LL - let _ = (1.0 + +2.0) * +3.0;
LL + let _ = (1.0 + +2.0) * 3.0;
|
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:6:16
--> $DIR/issue-88276-unary-plus.rs:7:14
|
LL | let _ = -+-+(1+2)*3;
| ^
| |
| unexpected `+`
| help: try removing the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:8:18
LL | let _ = [+3, 4+6];
| ^ unexpected `+`
|
LL | let _ = (1 + +2) * +3;
| ^
| |
| unexpected `+`
| help: try removing the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:8:24
help: try removing the `+`
|
LL | let _ = (1 + +2) * +3;
| ^
| |
| unexpected `+`
| help: try removing the `+`
LL - let _ = [+3, 4+6];
LL + let _ = [3, 4+6];
|
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:10:14
|
LL | let _ = (+&"hello");
| ^
| |
| unexpected `+`
| help: try removing the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:11:13
|
LL | let _ = +[+3, 4+6];
| ^
| |
| unexpected `+`
| help: try removing the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:11:15
|
LL | let _ = +[+3, 4+6];
| ^
| |
| unexpected `+`
| help: try removing the `+`
error: aborting due to 9 previous errors
error: aborting due to 4 previous errors