1
Fork 0

Improve error checking on unary plus

This commit is contained in:
Theodore Luo Wang 2021-08-31 23:07:58 -04:00
parent 5eacec9ec7
commit 6cfe98f196
4 changed files with 121 additions and 0 deletions

View file

@ -23,6 +23,8 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, Pos};
use std::mem;
use tracing::debug;
/// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression
/// dropped into the token stream, which happens while parsing the result of
/// macro expansion). Placement of these is not as complex as I feared it would
@ -355,6 +357,7 @@ impl<'a> Parser<'a> {
/// but the next token implies this should be parsed as an expression.
/// For example: `if let Some(x) = x { x } else { 0 } / 2`.
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
debug!("error_found_expr_would_be_stmt(lhs: {:?})", lhs);
let mut err = self.struct_span_err(
self.token.span,
&format!("expected expression, found `{}`", pprust::token_to_string(&self.token),),
@ -516,6 +519,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) => {
this.struct_span_err(lo, "leading `+` is not supported")
.span_label(lo, "unexpected `+`")
.span_suggestion_short(lo, "remove the `+`", "".to_string(), Applicability::MachineApplicable)
.emit();
this.bump();
this.parse_prefix_expr(None)
} // `+expr`
token::Ident(..) if this.token.is_keyword(kw::Box) => {
make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
}

View file

@ -0,0 +1,13 @@
// run-rustfix
#[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
//~| 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

@ -0,0 +1,13 @@
// run-rustfix
#[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
//~| 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

@ -0,0 +1,83 @@
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:4:13
|
LL | let _ = +1;
| ^
| |
| unexpected `+`
| help: remove the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:5:14
|
LL | let _ = -+(1+2)*3;
| ^
| |
| unexpected `+`
| help: remove the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:6:13
|
LL | let _ = +-+(1+2)*3;
| ^
| |
| unexpected `+`
| help: remove the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:6:15
|
LL | let _ = +-+(1+2)*3;
| ^
| |
| unexpected `+`
| help: remove the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:8:14
|
LL | let _ = -+-+(1+2)*3;
| ^
| |
| unexpected `+`
| help: remove the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:8:16
|
LL | let _ = -+-+(1+2)*3;
| ^
| |
| unexpected `+`
| help: remove the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:10:14
|
LL | let _ = (+&"hello");
| ^
| |
| unexpected `+`
| help: remove the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:11:13
|
LL | let _ = +[+3, 4+6];
| ^
| |
| unexpected `+`
| help: remove the `+`
error: leading `+` is not supported
--> $DIR/issue-88276-unary-plus.rs:11:15
|
LL | let _ = +[+3, 4+6];
| ^
| |
| unexpected `+`
| help: remove the `+`
error: aborting due to 9 previous errors