-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Improve diagnostics for unary plus operators (#88276) #88553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
6cfe98f
e7fb98e
5a863d5
ce9e765
bc9877c
65eb7e5
20eba43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -516,6 +516,27 @@ impl<'a> Parser<'a> { | |||||||
| token::BinOp(token::And) | token::AndAnd => { | ||||||||
| make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo)) | ||||||||
| } | ||||||||
| token::BinOp(token::Plus) => { | ||||||||
| let mut err = this.struct_span_err(lo, "leading `+` is not supported"); | ||||||||
| err.span_label(lo, "unexpected `+`"); | ||||||||
|
Comment on lines
+520
to
+521
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to replace this with
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm getting some unexpected results after changing this to I think this message is misleading because the programmer likely intended to type |
||||||||
|
|
||||||||
| // a block on the LHS might have been intended to be an expression instead | ||||||||
| let sp = this.sess.source_map().start_point(lo); | ||||||||
estebank marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&sp) { | ||||||||
| this.sess.expr_parentheses_needed(&mut err, *sp); | ||||||||
| } else { | ||||||||
| err.span_suggestion( | ||||||||
| lo, | ||||||||
| "try removing the `+`", | ||||||||
| "".to_string(), | ||||||||
| Applicability::MachineApplicable, | ||||||||
| ); | ||||||||
|
||||||||
| } | ||||||||
| err.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)) | ||||||||
| } | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| error: expected expression, found `+` | ||
| error: leading `+` is not supported | ||
| --> $DIR/issue-36499.rs:4:9 | ||
| | | ||
| LL | 2 + +2; | ||
| | ^ expected expression | ||
| | ^ | ||
| | | | ||
| | unexpected `+` | ||
| | help: try removing the `+` | ||
|
|
||
| error: aborting due to previous error | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| error: leading `+` is not supported | ||
| --> $DIR/issue-88276-unary-plus.rs:4:13 | ||
| | | ||
| LL | let _ = +1; | ||
| | ^ | ||
| | | | ||
| | unexpected `+` | ||
| | help: try removing the `+` | ||
|
|
||
| error: leading `+` is not supported | ||
| --> $DIR/issue-88276-unary-plus.rs:5:14 | ||
| | | ||
| LL | let _ = -+(1+2)*3; | ||
| | ^ | ||
| | | | ||
| | unexpected `+` | ||
| | help: try removing the `+` | ||
|
|
||
| error: leading `+` is not supported | ||
| --> $DIR/issue-88276-unary-plus.rs:6:14 | ||
| | | ||
| LL | let _ = -+-+(1+2)*3; | ||
| | ^ | ||
| | | | ||
| | unexpected `+` | ||
| | help: try removing the `+` | ||
|
|
||
| error: leading `+` is not supported | ||
| --> $DIR/issue-88276-unary-plus.rs:6:16 | ||
| | | ||
| 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 _ = (1 + +2) * +3; | ||
| | ^ | ||
| | | | ||
| | unexpected `+` | ||
| | help: try removing the `+` | ||
|
|
||
| error: leading `+` is not supported | ||
| --> $DIR/issue-88276-unary-plus.rs:8:24 | ||
| | | ||
| LL | let _ = (1 + +2) * +3; | ||
| | ^ | ||
| | | | ||
| | unexpected `+` | ||
| | help: try removing the `+` | ||
|
|
||
| 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 | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should only be done if the following token is a numeric literal. As this stands now, if you write
+fooor+""this error will trigger as well. The suggestion won't be wrong per-se, but in those cases the problems are deeper than the+.You can use
Parser::look_aheadwhich takes a closure giving you a token for whateverNtokens you are advancing, so you can do something liketoken::BinOp(token::Plus) if this.look_ahead(1, |t| t.is_numeric_literal() => {or something. We need to decide whether an identifier like+foois something where we want to also give this error or not. Personally I think we might not want to.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to limit the check to
+'s that are followed by a numeric literal, made changes to reflect that.