Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions clippy_lints/src/transmute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ declare_clippy_lint! {
///
/// ### Example
/// ```ignore
/// let ptr: *const T = core::intrinsics::transmute('x')
/// let ptr: *const T = std::mem::transmute('x')
/// ```
#[clippy::version = "pre 1.29.0"]
pub WRONG_TRANSMUTE,
Expand All @@ -56,7 +56,8 @@ declare_clippy_lint! {
///
/// ### Example
/// ```rust,ignore
/// core::intrinsics::transmute(t); // where the result type is the same as `t`'s
/// let t : u8 = 12;
/// let u : u8 = std::mem::transmute(t);
/// ```
#[clippy::version = "pre 1.29.0"]
pub USELESS_TRANSMUTE,
Expand All @@ -67,7 +68,7 @@ declare_clippy_lint! {
// FIXME: Merge this lint with USELESS_TRANSMUTE once that is out of the nursery.
declare_clippy_lint! {
/// ### What it does
///Checks for transmutes that could be a pointer cast.
/// Checks for transmutes that could be a pointer cast.
///
/// ### Why is this bad?
/// Readability. The code tricks people into thinking that
Expand Down Expand Up @@ -100,7 +101,7 @@ declare_clippy_lint! {
///
/// ### Example
/// ```rust,ignore
/// core::intrinsics::transmute(t) // where the result type is the same as
/// std::mem::transmute(t) // where the result type is the same as
/// // `*t` or `&t`'s
/// ```
#[clippy::version = "pre 1.29.0"]
Expand All @@ -116,20 +117,16 @@ declare_clippy_lint! {
/// ### Why is this bad?
/// This can always be rewritten with `&` and `*`.
///
/// ### Known problems
/// - `mem::transmute` in statics and constants is stable from Rust 1.46.0,
/// while dereferencing raw pointer is not stable yet.
/// If you need to do this in those places,
/// you would have to use `transmute` instead.
///
Comment on lines -119 to -124
Copy link
Contributor

@Jarcho Jarcho Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint still doesn't take into account whether it's in a constant context. It would also need an MSRV check to fix this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I'm not sure I understand 100%.

If it doesn't check whether it's in a const context or not, but now it works properly in a const context, then this problem is effectively fixed, no?

I'm guessing this is what you mean about the MSRV, perhaps.

Though OTOH maybe the documentation could mention that "for rust versions prior to X release, this lint will have [this known issue] due to the fact that dereferencing raw pointers wasn't stable before then.". Would that be appropriate?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It only works correctly if the MSRV is high enough to include const raw pointer dereferences.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping @felix91gr. Just waiting on this. *const pointers can be dereferenced as of 1.58, *mut as of 1.83.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jarcho ty for the ping. I got taken away for a bit. Where do I specify this? I think I've never have added a MSRV in Clippy before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For documenting it just keep it in the "known problems" section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jarcho so then, if I got you correctly, the only change needed to correct this is of documentation, right? I thought you meant that a MSRV config should be added to differentiate between all the cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either would be an acceptable PR. Actually adding the code to handle the MSRV would be the better fix.

If you want a to actually fix the issue you'll need to add the msrv to clippy_utils/src/msrvs.rs. These are named by feature so you'll add something like CONST_RAW_CONST_PTR_DEREF and CONST_RAW_MUT_PTR_DEREF. You can use is_in_const_context to check for constness.

Everything else is already done since the lint has an MSRV check for the suggestion.

/// ### Example
/// ```rust,ignore
/// unsafe {
/// let _: &T = std::mem::transmute(p); // where p: *const T
/// }
///
/// // can be written:
/// let _: &T = &*p;
/// unsafe {
/// let _: &T = &*p;
/// }
/// ```
#[clippy::version = "pre 1.29.0"]
pub TRANSMUTE_PTR_TO_REF,
Expand Down