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

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

Closed
steffahn opened this issue Jan 3, 2021 · 3 comments · Fixed by #115424
Closed
Assignees
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug.

Comments

@steffahn
Copy link
Member

steffahn commented Jan 3, 2021

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...)

@jyn514 jyn514 added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Jan 3, 2021
@jyn514
Copy link
Member

jyn514 commented Jan 3, 2021

Related: #78747

@jyn514 jyn514 added the A-parser Area: The parsing of Rust source code to an AST label Jan 3, 2021
@leonardo-m
Copy link

Rust instead should suggest (x as i32) here.

@osa1
Copy link
Contributor

osa1 commented Jan 27, 2021

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants