Skip to content

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

Closed
@TudbuT

Description

@TudbuT

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions