rust-lang / rust-lang/rust-clippy

Incorrect fix: immediately dereferencing a reference to a literal as a place expression.

Open
#13,423 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

This corner case triggers an incorrect fix suggestion, leading to a compilation error.

Reproducer

main.rs

fn main() {
    *&mut 4 = 5; // /!\ immediately dereferencing a reference
//  ^^^^^^^ help: try: `4`
}

@Kyuuhachi Assigns to a temporary that is then dropped [1]

$ cargo clippy --fix --allow-dirty --allow-no-vcs
    Checking rust_test v0.1.0 (./tests/rust)
warning: failed to automatically apply fixes suggested by rustc to crate `rust_test`

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:
error[E0070]: invalid left-hand side of assignment
 --> src/main.rs:2:7
  |
2 |     4 = 5;
  |     - ^
  |     |
  |     cannot assign to this expression

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0070`.
Original diagnostics will follow.
Version
$ rustc -Vv                                                                                                                                                                                          
rustc 1.83.0-nightly (f79a912d9 2024-09-18)
binary: rustc
commit-hash: f79a912d9edc3ad4db910c0e93672ed5c65133fa
commit-date: 2024-09-18
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the main.rs reproducer and run cargo clippy --fix using the shown command and rustc version. Check that the suggested replacement for immediately dereferencing a reference to a literal no longer produces the invalid assignment error E0070, and confirm the fix does not leave compilation errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.