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

unused_variables suggestion has false positives for variables that are only named in unreachable code #107734

Open
Moises-Herradon-Cueto opened this issue Feb 6, 2023 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. 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. L-unused_variables Lint: unused_variables T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Moises-Herradon-Cueto
Copy link

Moises-Herradon-Cueto commented Feb 6, 2023

Running cargo clippy --fix or cargo +nightly clippy --fix on this code causes it to break, and tells me to come here and file a bug report

fn example() -> u8 {
    let result = 0_u8;
    return 0;
    let a = result;
}

I think clippy wants to turn result into _result, and then the second result is not found.

Meta

rustc --version --verbose:

rustc 1.67.0 (fc594f156 2023-01-24)
binary: rustc
commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
commit-date: 2023-01-24
host: x86_64-unknown-linux-gnu
release: 1.67.0
LLVM version: 15.0.6
Full output of clippy

warning: failed to automatically apply fixes suggested by rustc to crate `test_bug`

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[E0425]: cannot find value `result` in this scope
 --> src/lib.rs:4:14
  |
4 |     let _a = result;
  |              ^^^^^^ help: a local variable with a similar name exists: `_result`

warning: unreachable statement
 --> src/lib.rs:4:5
  |
3 |     return 0;
  |     -------- any code following this expression is unreachable
4 |     let _a = result;
  |     ^^^^^^^^^^^^^^^^ unreachable statement
  |
  = note: `#[warn(unreachable_code)]` on by default

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0425`.
Original diagnostics will follow.

warning: unreachable statement
 --> src/lib.rs:4:5
  |
3 |     return 0;
  |     -------- any code following this expression is unreachable
4 |     let a = result;
  |     ^^^^^^^^^^^^^^^ unreachable statement
  |
  = note: `#[warn(unreachable_code)]` on by default

warning: unused variable: `result`
 --> src/lib.rs:2:9
  |
2 |     let result = 0_u8;
  |         ^^^^^^ help: if this is intentional, prefix it with an underscore: `_result`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `a`
 --> src/lib.rs:4:9
  |
4 |     let a = result;
  |         ^ help: if this is intentional, prefix it with an underscore: `_a`

warning: function `example` is never used
 --> src/lib.rs:1:10
  |
1 | async fn example() -> u8 {
  |          ^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `test_bug` (lib test) generated 4 warnings (run `cargo clippy --fix --lib -p test_bug --tests` to apply 2 suggestions)
warning: `test_bug` (lib) generated 4 warnings (4 duplicates)
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s

@Moises-Herradon-Cueto Moises-Herradon-Cueto added the C-bug Category: This is a bug. label Feb 6, 2023
@Moises-Herradon-Cueto Moises-Herradon-Cueto changed the title Cargo clippy fix breaks async code if there's unreachable statements Cargo clippy fix breaks with unreachable statements Feb 6, 2023
@Moises-Herradon-Cueto
Copy link
Author

I've just realized it's the same if the function is not async

@clubby789
Copy link
Contributor

clubby789 commented Feb 6, 2023

This also happens with cargo fix, and is not a clippy-specific issue.

The problem is that clippy fails to recognise that the warnings also existed before the fix was applied
My mistake, this specific case actually is breaking the code by changing the name of a used variable.

@jieyouxu
Copy link
Member

jieyouxu commented Feb 9, 2023

I think it's because renaming unused variable names to be prepended with an underscore is a machine-applicable level lint. The lint probably adheres strictly to variable liveness analysis and considers the let result = 0_u8; variable dead because it's never used (since the variable name is referenced only in future unreachable expressions) so it doesn't need to rename the unreachable result. Not sure what the right fix is.

Relevant function:

fn report_unused(

@jyn514 jyn514 changed the title Cargo clippy fix breaks with unreachable statements unreachable_code suggestion has false positives for variables that are only named in unreachable code Apr 27, 2023
@jyn514 jyn514 changed the title unreachable_code suggestion has false positives for variables that are only named in unreachable code unused_variables suggestion has false positives for variables that are only named in unreachable code Apr 27, 2023
@jyn514 jyn514 added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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 Apr 27, 2023
@jieyouxu jieyouxu added the L-unused_variables Lint: unused_variables label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. 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. L-unused_variables Lint: unused_variables T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants