Skip to content

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

Closed
@steffahn

Description

@steffahn
Member
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

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
C-bugCategory: This is a bug.
on Jan 3, 2021
jyn514

jyn514 commented on Jan 3, 2021

@jyn514
Member

Related: #78747

added
A-parserArea: The lexing & parsing of Rust source code to an AST
on Jan 3, 2021
leonardo-m

leonardo-m commented on Jan 3, 2021

@leonardo-m

Rust instead should suggest (x as i32) here.

osa1

osa1 commented on Jan 27, 2021

@osa1
Contributor

I think this requires adding a special case for _ as (_) <binary op> _ where <binary op> is << or <. In this case we don't want to generate the warning as removing the parens around the type breaks the code.

In all cases I think the warning can work as before.

I'm not sure how to implement this special case though..

self-assigned this
on Sep 1, 2023
added a commit that references this issue on Sep 1, 2023

Rollup merge of rust-lang#115424 - notriddle:notriddle/issue-106413, …

888dc2e
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

    Participants

    @osa1@notriddle@steffahn@leonardo-m@jyn514

    Issue actions

      Unfortunate warning about “unnecessary parentheses” that aren’t really unnecessary · Issue #80636 · rust-lang/rust