diff options
| author | Alex Macleod <alex@macleod.io> | 2025-04-20 17:18:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-20 17:18:52 +0000 |
| commit | 077475f1a5bbc76b1a59631f8ab49c4f3b3f83fa (patch) | |
| tree | 7ae8e15a97987f062f7c9cfb1ed4a312775b8c87 | |
| parent | 781fdab9a95320bf8c3eb092ef628352643059bf (diff) | |
| parent | 5dc239aa3ed6a44c17e0e1ff9cf988804f382254 (diff) | |
| download | rust-077475f1a5bbc76b1a59631f8ab49c4f3b3f83fa.tar.gz rust-077475f1a5bbc76b1a59631f8ab49c4f3b3f83fa.zip | |
`ptr_cast_constness`: show snippet from the right context (#14622)
changelog: [`ptr_cast_constness`]: do not replace suggestion by content of macro Fixes rust-lang/rust-clippy#14621
| -rw-r--r-- | clippy_lints/src/casts/ptr_cast_constness.rs | 5 | ||||
| -rw-r--r-- | tests/ui/ptr_cast_constness.fixed | 6 | ||||
| -rw-r--r-- | tests/ui/ptr_cast_constness.rs | 6 | ||||
| -rw-r--r-- | tests/ui/ptr_cast_constness.stderr | 8 |
4 files changed, 22 insertions, 3 deletions
diff --git a/clippy_lints/src/casts/ptr_cast_constness.rs b/clippy_lints/src/casts/ptr_cast_constness.rs index 18e32c5c8a1..2471c735551 100644 --- a/clippy_lints/src/casts/ptr_cast_constness.rs +++ b/clippy_lints/src/casts/ptr_cast_constness.rs @@ -53,7 +53,8 @@ pub(super) fn check<'tcx>( } if msrv.meets(cx, msrvs::POINTER_CAST_CONSTNESS) { - let sugg = Sugg::hir(cx, cast_expr, "_"); + let mut app = Applicability::MachineApplicable; + let sugg = Sugg::hir_with_context(cx, cast_expr, expr.span.ctxt(), "_", &mut app); let constness = match *to_mutbl { Mutability::Not => "const", Mutability::Mut => "mut", @@ -66,7 +67,7 @@ pub(super) fn check<'tcx>( "`as` casting between raw pointers while changing only its constness", format!("try `pointer::cast_{constness}`, a safer alternative"), format!("{}.cast_{constness}()", sugg.maybe_paren()), - Applicability::MachineApplicable, + app, ); } } diff --git a/tests/ui/ptr_cast_constness.fixed b/tests/ui/ptr_cast_constness.fixed index 84a36d320f6..79bfae1f7eb 100644 --- a/tests/ui/ptr_cast_constness.fixed +++ b/tests/ui/ptr_cast_constness.fixed @@ -100,3 +100,9 @@ fn null_pointers() { let _ = external!(ptr::null::<u32>() as *mut u32); let _ = external!(ptr::null::<u32>().cast_mut()); } + +fn issue14621() { + let mut local = 4; + let _ = std::ptr::addr_of_mut!(local).cast_const(); + //~^ ptr_cast_constness +} diff --git a/tests/ui/ptr_cast_constness.rs b/tests/ui/ptr_cast_constness.rs index ba4eb00b26f..f6590dabd5b 100644 --- a/tests/ui/ptr_cast_constness.rs +++ b/tests/ui/ptr_cast_constness.rs @@ -100,3 +100,9 @@ fn null_pointers() { let _ = external!(ptr::null::<u32>() as *mut u32); let _ = external!(ptr::null::<u32>().cast_mut()); } + +fn issue14621() { + let mut local = 4; + let _ = std::ptr::addr_of_mut!(local) as *const _; + //~^ ptr_cast_constness +} diff --git a/tests/ui/ptr_cast_constness.stderr b/tests/ui/ptr_cast_constness.stderr index 5b2a918c404..0b1644168ff 100644 --- a/tests/ui/ptr_cast_constness.stderr +++ b/tests/ui/ptr_cast_constness.stderr @@ -83,5 +83,11 @@ LL | let _ = inline!(ptr::null::<u32>().cast_mut()); | = note: this error originates in the macro `__inline_mac_fn_null_pointers` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 13 previous errors +error: `as` casting between raw pointers while changing only its constness + --> tests/ui/ptr_cast_constness.rs:106:13 + | +LL | let _ = std::ptr::addr_of_mut!(local) as *const _; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `std::ptr::addr_of_mut!(local).cast_const()` + +error: aborting due to 14 previous errors |
