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

identity_op: fails to apply fix for reference #12050

Closed
FineFindus opened this issue Dec 29, 2023 · 2 comments · Fixed by #12056
Closed

identity_op: fails to apply fix for reference #12050

FineFindus opened this issue Dec 29, 2023 · 2 comments · Fixed by #12056
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@FineFindus
Copy link

FineFindus commented Dec 29, 2023

Summary

When attempting to fix the identity_op lint for variables that are references, such as &i32, the lint fails to apply.

This issue is similar to #9904, which is correctly resolved by removing the reference.

Reproducer

I tried this code:

fn main() {
    let x = &0i32;
    let _: i32 = x + 0;
}

I expected to see this happen:
When running cargo clippy --fix the variable should be dereferenced.

fn main() {
    let x = &0i32;
    let _: i32 = *x;
}

Instead, this happened:

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

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-clippy/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:
warning: failed to connect to jobserver from environment variable `CARGO_MAKEFLAGS="-j --jobserver-fds=8,9 --jobserver-auth=8,9"`: cannot open file descriptor 8 from the jobserver environment variable value: Bad file descriptor (os error 9)
  |
  = note: the build environment is likely misconfigured

error[E0308]: mismatched types
 --> src/main.rs:3:18
  |
3 |     let _: i32 = x;
  |            ---   ^ expected `i32`, found `&i32`
  |            |
  |            expected due to this
  |
help: consider dereferencing the borrow
  |
3 |     let _: i32 = *x;
  |                  +

error: aborting due to 1 previous error

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

warning: this operation has no effect
 --> src/main.rs:3:18
  |
3 |     let _: i32 = x + 0;
  |                  ^^^^^ help: consider reducing it to: `x`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
  = note: `#[warn(clippy::identity_op)]` on by default

warning: `clippy-bug-report` (bin "clippy-bug-report") generated 1 warning (run `cargo clippy --fix --bin "clippy-bug-report"` to apply 1 suggestion)
warning: `clippy-bug-report` (bin "clippy-bug-report" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s

Version

rustc 1.77.0-nightly (fb5ed726f 2023-12-28)
binary: rustc
commit-hash: fb5ed726f72c6d16c788517c60ec00d4564b9348
commit-date: 2023-12-28
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

Additional Labels

No response

@FineFindus FineFindus added the C-bug Category: Clippy is not doing the correct thing label Dec 29, 2023
@m-rph
Copy link
Contributor

m-rph commented Dec 29, 2023

@rustbot claim

@rustbot label +I-suggestion-causes-error

@m-rph
Copy link
Contributor

m-rph commented Dec 29, 2023

Okay, so what's happening is that when a ref is implicitly deref'd / coerced into the value, the lint doesn't take it into consideration.

This fails, x is ref and gets deref'd.

fn main() {
    let x = &0i32;
    let _: i32 = x + 0;
}

This gets fixed correctly.

fn main() {
    let x = &0i32;
    let _: i32 = *x + 0;
}

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 29, 2023
bors added a commit that referenced this issue Jan 3, 2024
Fixes: #12050 - `identity_op` correctly suggests a deference for coerced references

When `identity_op` identifies a `no_op`, provides a suggestion, it also checks the type of the type of the variable. If the variable is a reference that's been coerced into a value, e.g.

```
let x = &0i32;
let _ = x + 0;
```

the suggestion will now use a derefence. This is done by identifying whether the variable is a reference to an integral value, and then whether it gets dereferenced.

changelog: false positive: [`identity_op`]: corrected suggestion for reference coerced to value.

fixes: #12050
@bors bors closed this as completed in c2b3f5c Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants