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

bad suggestion for unused_parens, needs space in replacement #112276

Closed
legofanclub opened this issue Jun 4, 2023 · 1 comment · Fixed by #112316
Closed

bad suggestion for unused_parens, needs space in replacement #112276

legofanclub opened this issue Jun 4, 2023 · 1 comment · Fixed by #112316
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.

Comments

@legofanclub
Copy link

Problem

Running the command cargo fix fails to fix the following minimal example:

fn foo() -> bool {
    return(true);
}

fn main() {
    println!("{}", foo());
}

and gives the following output

PS C:\Users\name\Documents\minimal example for cargo --fix bug\minimal-example> cargo fix
    Checking minimal-example v0.1.0 (C:\Users\name\Documents\minimal example for cargo --fix bug\minimal-example)
warning: failed to automatically apply fixes suggested by rustc to crate `minimal_example`

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

  * src\main.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[E0425]: cannot find value `returntrue` in this scope
 --> src\main.rs:2:5
  |
2 |     returntrue;
  |     ^^^^^^^^^^ not found in this scope

error[E0308]: mismatched types
 --> src\main.rs:1:13
  |
  |    ---      ^^^^ expected `bool`, found `()`
  |    |
  |    implicitly returns `()` as its body has no tail or `return` expression

Some errors have detailed explanations: E0308, E0425.
For more information about an error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: unnecessary parentheses around `return` value
 --> src\main.rs:2:11
  |
2 |     return(true);
  |           ^    ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
2 -     return(true);
2 +     returntrue;
  |

warning: `minimal-example` (bin "minimal-example") generated 1 warning (run `cargo fix --bin "minimal-example"` to apply 1 suggestion)
warning: `minimal-example` (bin "minimal-example" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.80s
PS C:\Users\name\Documents\minimal example for cargo --fix bug\minimal-example> cargo clippy --fix
    Checking minimal-example v0.1.0 (C:\Users\name\Documents\minimal example for cargo --fix bug\minimal-example)
       Fixed src\main.rs (2 fixes)
    Finished dev [unoptimized + debuginfo] target(s) in 2.19s

Steps

  1. run cargo init
  2. paste the code above into main.rs
  3. run cargo fix --allow-no-vcs

Possible Solution(s)

The fix command isn't adding a space where it should (in between return and true). If you add the space in, the bug does not occur.

fn foo() -> bool {
    return(true); // produces bug
    // return( true ); // produces bug
    // return ( true ); // no bug
    // return (true); // no bug
}

Notes

Running cargo clippy --fix doesn't produce the same bug.

Version

Cargo version on my PC:

cargo 1.69.0 (6e9a83356 2023-04-12)
release: 1.69.0
commit-hash: 6e9a83356b70586d4b77613a6b33f9ea067b9cdf
commit-date: 2023-04-12
host: x86_64-pc-windows-msvc
libgit2: 1.5.0 (sys:0.16.0 vendored)
libcurl: 7.86.0-DEV (sys:0.4.59+curl-7.86.0 vendored ssl:Schannel)
os: Windows 10.0.19045 (Windows 10 Home) [64-bit]
PS C:\Users\name\Documents\minimal example for cargo --fix bug\minimal-example> 

Cargo version on a Replit Rust Repl. (I was able to reproduce the bug there.)

cargo 1.64.0
release: 1.64.0
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.86.0 (sys:0.4.55+curl-7.83.1 system ssl:OpenSSL/3.0.7)
os: Ubuntu 20.4.0 [64-bit]
@legofanclub legofanclub added the C-bug Category: This is a bug. label Jun 4, 2023
@epage epage changed the title Cargo Fix Bug cargo-fix failed when compiler reported errors on a fix Jun 4, 2023
@ehuss ehuss changed the title cargo-fix failed when compiler reported errors on a fix bad suggestion for unused_parens, needs spaces in replacement Jun 4, 2023
@ehuss ehuss transferred this issue from rust-lang/cargo Jun 4, 2023
@ehuss
Copy link
Contributor

ehuss commented Jun 4, 2023

Thanks for the report! I have transferred to rust-lang/rust since this is an issue with rustc.

The reason it works with clippy is because clippy has an additional needless_return lint that removes the return keyword.

The problem here is that unused_parens leaves the result as returntrue; when it should be return true; with a space for the left parenthesis.

@ehuss ehuss added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jun 4, 2023
@ehuss ehuss changed the title bad suggestion for unused_parens, needs spaces in replacement bad suggestion for unused_parens, needs space in replacement Jun 4, 2023
@bors bors closed this as completed in 0ff5a6e Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants