Skip to content

Unfortunate warning about “unnecessary parentheses” that aren’t really unnecessary #80636

Description

@steffahn
pub fn foo() {
    let x: u32 = 100;
    if x as (i32) < 0 {
        // ...
    }
}

(Playground)

Warnings:

   Compiling playground v0.0.1 (/playground)
warning: unnecessary parentheses around type
 --> src/lib.rs:3:13
  |
3 |     if x as (i32) < 0 {
  |             ^^^^^ help: remove these parentheses
  |
  = note: `#[warn(unused_parens)]` on by default

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 2.64s

Following this warning’s help: remove these parentheses results in

pub fn foo() {
    let x: u32 = 100;
    if x as i32 < 0 {
        // ...
    }
}
   Compiling playground v0.0.1 (/playground)
error: `<` is interpreted as a start of generic arguments for `i32`, not a comparison
 --> src/lib.rs:3:17
  |
3 |     if x as i32 < 0 {
  |        -------- ^ --- interpreted as generic arguments
  |        |        |
  |        |        not interpreted as comparison
  |        help: try comparing the cast value: `(x as i32)`

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.

Similarly, cargo fix results in

    Checking playground v0.1.0 (/home/frank/playground)
warning: failed to automatically apply fixes suggested by rustc to crate `playground`

after fixes were automatically applied the compiler reported errors within these files:

  * src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see 
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: `<` is interpreted as a start of generic arguments for `i32`, not a comparison
 --> src/lib.rs:3:17
  |
3 |     if x as i32 < 0 {
  |        -------- ^ --- interpreted as generic arguments
  |        |        |
  |        |        not interpreted as comparison
  |        help: try comparing the cast value: `(x as i32)`

error: aborting due to previous error

Original diagnostics will follow.

warning: unnecessary parentheses around type
 --> src/lib.rs:3:13
  |
3 |     if x as (i32) < 0 {
  |             ^^^^^ help: remove these parentheses
  |
  = note: `#[warn(unused_parens)]` on by default

warning: unnecessary parentheses around type
 --> src/lib.rs:3:13
  |
3 |     if x as (i32) < 0 {
  |             ^^^^^ help: remove these parentheses
  |
  = note: `#[warn(unused_parens)]` on by default

warning: 1 warning emitted

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.62s

(for some reason the warning is duplicated in the cargo fix output...)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions