Skip to content

suggestion in unused_parens diagnostic when cast is followed by less-than produces broken code #117142

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

Closed
TudbuT opened this issue Oct 24, 2023 · 0 comments · Fixed by #117321
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@TudbuT
Copy link

TudbuT commented Oct 24, 2023

Code

fn main() {
    let a: i32 = 1;
    let b: i64 = 1;
    if b - a as (i64) < 0 {
        println!(":D");
    }
}

Current output

warning: unnecessary parentheses around type
 --> src/main.rs:4:17
  |
4 |     if b - a as (i64) < 0 {
  |                 ^   ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
4 -     if b - a as (i64) < 0 {
4 +     if b - a as i64 < 0 {
  |

warning: `playground` (bin "playground") generated 1 warning (run `cargo fix --bin "playground"` to apply 1 suggestion)

Desired output

--no output--

Rationale and extra context

The code uses a cast followed by a less-than operator. When parens are removed, the compiler errors:

error: `<` is interpreted as a start of generic arguments for `i64`, not a comparison
 --> src/main.rs:4:21
  |
4 |     if b - a as i64 < 0 {
  |                     ^ --- interpreted as generic arguments
  |                     |
  |                     not interpreted as comparison
  |
help: try comparing the cast value
  |
4 |     if b - (a as i64) < 0 {
  |            +        +

This is not a particularly problematic issue as the suggestion given in this output does not produce any wrong diagnostics, but the help given in the problematic output does cause a compiler error which was not present previously, so I think it's an incorrect warning.

Using parens like in the example does feel like bad style, but it should not generate this particular warning.

Other cases

No response

Anything else?

No response

@TudbuT TudbuT 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 Oct 24, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 24, 2023
@saethlin saethlin added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 25, 2023
@chenyukang chenyukang self-assigned this Oct 28, 2023
@bors bors closed this as completed in 3071aef Jan 13, 2024
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 D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

4 participants