-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggest i += 1
when we see i++
or ++i
#83536
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
fn test1() { | ||
let i = 0; | ||
i++; //~ ERROR | ||
} | ||
|
||
fn test2() { | ||
let i = 0; | ||
++i; //~ ERROR | ||
} | ||
|
||
fn main() {} | ||
Comment on lines
+8
to
+11
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. Probably should add tests for |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
error: expected expression, found `+` | ||
--> $DIR/increment.rs:3:7 | ||
| | ||
LL | i++; | ||
| ^ expected expression | ||
| | ||
= note: Rust has no dedicated increment and decrement operators | ||
help: try using `+= 1` instead | ||
| | ||
LL | i += 1; | ||
| ^^^^ | ||
Comment on lines
+8
to
+11
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. One issue with using a structured suggestion is that we will suggest invalid code in the case of fn main() {
let mut i = 0;
while i++ < 5 { /* ... */ } We suggest fn main() {
let mut i = 0;
while i += 1 < 5 { /* ... */ }
} Errors:
And adding parentheses doesn't fix it: fn main() {
let mut i = 0;
while (i += 1) < 5 { /* ... */ }
} Errors:
So maybe it would be better to just skip the structured suggestion? 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 thinking whether it would make sense to try to suggest 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. That's interesting! The advantage of that is that it will preserve the behavior of Also, what should we suggest in the ++temp; and then we would suggest { let temp = temp; temp += 1; temp } which is incorrect. 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. So, thinking about it more, maybe simply suggesting 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. You wrote that backwards: preincrement |
||
|
||
error: expected expression, found `+` | ||
--> $DIR/increment.rs:8:5 | ||
| | ||
LL | ++i; | ||
| ^ expected expression | ||
| | ||
= note: Rust has no dedicated increment and decrement operators | ||
= help: try using `+= 1` instead | ||
|
||
error: aborting due to 2 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.
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.
Won't this turn
++i
into+= 1i
?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.
Yes, good catch. You'll need to account for
++i
andi++
specifically.