Skip to content
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

postfix_increment show both type of suggestion for single statement #104867

Closed
chenyukang opened this issue Nov 25, 2022 · 2 comments · Fixed by #104875
Closed

postfix_increment show both type of suggestion for single statement #104867

chenyukang opened this issue Nov 25, 2022 · 2 comments · Fixed by #104875
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@chenyukang
Copy link
Member

chenyukang commented Nov 25, 2022

Story from here

Given the following code:

fn main() {
    let mut i = 0;
    i++;
    println!("i: {:?}", i);
}

The current output is:

error: Rust has no postfix increment operator
 --> ./p/inc.rs:3:6
  |
3 |     i++;
  |      ^^ not a valid postfix operator
  |
help: use `+= 1` instead
  |
3 |     { let tmp = i; i += 1; tmp };
  |     +++++++++++  ~~~~~~~~~~~~~~~
3 |     i += 1;
  |       ~~~~

Ideally the output should look like:

error: Rust has no postfix increment operator
 --> ./p/inc.rs:3:6
  |
3 |     i++;
  |      ^^ not a valid postfix operator
  |
help: use `+= 1` instead
3 |     i += 1;
  |       ~~~~
@chenyukang chenyukang added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 25, 2022
@chenyukang
Copy link
Member Author

@rustbot claim

@chenyukang
Copy link
Member Author

Our current suggestion for this code is wrong:

fn test2() {
    let mut i = 0;
    if i++ == 1 {}
}

Current output:

error: Rust has no postfix increment operator
 --> ./p/inc.rs:8:9
  |
8 |     if i++ == 1 {}
  |         ^^ not a valid postfix operator
  |
help: use `+= 1` instead
  |
8 |     if { let tmp = i; i += 1; tmp } == 1 {}
  |        +++++++++++  ~~~~~~~~~~~~~~~
8 |     if i += 1 == 1 {}
  |          ~~~~

The second one is not right.

bors added a commit to rust-lang-ci/rust that referenced this issue Dec 14, 2022
…estebank

Properly handle postfix inc/dec in standalone and subexpr scenarios

Fixes rust-lang#104867
r? `@estebank`
@bors bors closed this as completed in 7c11a53 Dec 14, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
…estebank

Properly handle postfix inc/dec in standalone and subexpr scenarios

Fixes rust-lang#104867
r? `@estebank`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant