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

FP ptr-cast-constness breaks code #10874

Closed
matthiaskrgr opened this issue Jun 2, 2023 · 1 comment · Fixed by #10879
Closed

FP ptr-cast-constness breaks code #10874

matthiaskrgr opened this issue Jun 2, 2023 · 1 comment · Fixed by #10879
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Jun 2, 2023

Summary

.

Lint Name

ptr-cast-constness

Reproducer

I tried this code:

unsafe fn _ptr_to_ref<T, U>(p: *const T, om: *mut U) {
    let _: &mut T = std::mem::transmute(p as *mut T);
    let _ = &mut *(p as *mut T);
    let _: &T = &*(om as *const T);
}

fn main() {}

I saw this happen:

warning: `as` casting between raw pointers while changing its constness
 --> ./src/tools/clippy/tests/ui/transmute_ptr_to_ref.rs:2:41
  |
2 |     let _: &mut T = std::mem::transmute(p as *mut T);
  |                                         ^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `p.cast_mut()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_cast_constness
  = note: requested on the command line with `-W clippy::ptr-cast-constness`

warning: `as` casting between raw pointers while changing its constness
 --> ./src/tools/clippy/tests/ui/transmute_ptr_to_ref.rs:3:19
  |
3 |     let _ = &mut *(p as *mut T);
  |                   ^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `p.cast_mut()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_cast_constness

warning: `as` casting between raw pointers while changing its constness
 --> ./src/tools/clippy/tests/ui/transmute_ptr_to_ref.rs:4:19
  |
4 |     let _: &T = &*(om as *const T);
  |                   ^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `om.cast_const()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_cast_constness

I expected to see this happen:

The suggestion does not compile:

unsafe fn _ptr_to_ref<T, U>(p: *const T, om: *mut U) {
    let _: &mut T = std::mem::transmute(p.cast_mut());
    let _ = &mut *(p.cast_mut());
    let _: &T = &*(om.cast_const());
}
error[E0308]: mismatched types
 --> ./src/tools/clippy/tests/ui/transmute_ptr_to_ref.rs:4:17
  |
1 | unsafe fn _ptr_to_ref<T, U>(p: *const T, om: *mut U) {
  |                       -  - found type parameter
  |                       |
  |                       expected type parameter
...
4 |     let _: &T = &*(om.cast_const());
  |            --   ^^^^^^^^^^^^^^^^^^^ expected `&T`, found `&U`
  |            |
  |            expected due to this
  |
  = note: expected reference `&T`
             found reference `&U`
  = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
  = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

Version

clippy 0.1.72 (0939ec1 2023-06-02)
rustc 1.72.0-nightly (0939ec13d 2023-06-02)
binary: rustc
commit-hash: 0939ec13d88dfafcbb7f25314bd0d2f1519bf0d5
commit-date: 2023-06-02
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.4

Additional Labels

No response

@matthiaskrgr matthiaskrgr added 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 I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jun 2, 2023
@Alexendoo
Copy link
Member

Okay I can in fact spell the lint name correctly, it just didn't work on the playground because this isn't in nightly until tomorrow 😅

cc @Centri3

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-false-positive Issue: The lint was triggered on code it shouldn't have 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.

2 participants