rust-lang / rust-lang/rust-clippy

Clippy Error during auto fix

Open
#9,513 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Clippy reported error during auto fix

❯ cargo clippy --workspace --all-features --fix --allow-dirty --broken-code  -- -D clippy::all -D clippy::pedantic -D clippy::nursery -D clippy::cargo
    Checking ril v0.1.0 (C:\Users\alvin\OneDrive\Desktop\repos\ril)
warning: failed to automatically apply fixes suggested by rustc to crate `ril`

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

  * src\encode.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[E0699]: the type of this value must be known to call a method on a raw pointer on it
  --> src\encode.rs:55:37
   |
55 |         unsafe { (data as *const _).cast::<T>().read() }
   |                                     ^^^^

error: aborting due to previous error

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

error[E0699]: the type of this value must be known to call a method on a raw pointer on it
  --> src\encode.rs:55:37
   |
55 |         unsafe { (data as *const _).cast::<T>().read() }
   |                                     ^^^^

For more information about this error, try `rustc --explain E0699`.
error: could not compile `ril` due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `ril` due to previous error

Original code is

unsafe { (data as *const _ as *const T).read() }

Clippy attempted to change it to

unsafe { (data as *const _).cast::<T>().read() }

which leads to an error

Note: data is &[u8]

rustc --version --verbose

rustc 1.64.0-nightly (7b46aa594 2022-07-05)
binary: rustc
commit-hash: 7b46aa594c4bdc507fbd904b6777ca30c37a9209
commit-date: 2022-07-05
host: x86_64-pc-windows-msvc
release: 1.64.0-nightly
LLVM version: 14.0.6

The lint that likely caused it

error: `as` casting between raw pointers without changing its mutability
  --> src\encode.rs:55:18
   |
55 |         unsafe { (data as *const _ as *const T).read() }
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `(data as *const _).cast::<T>()`
   |
   = note: `-D clippy::ptr-as-ptr` implied by `-D clippy::pedantic`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr

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

Reproduce the issue with the provided cargo clippy command and inspect src/encode.rs at line 55, focusing on the ptr-as-ptr suggestion. Done means the automatic fix no longer leaves the crate with E0699 and the workspace check completes successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.