Skip to content

Commit 49b7330

Browse files
committed
library: use strict provenance lints consistently
The `fuzzy_provenance_casts` lint is enabled in most of the standard library, but its identical twin `lossy_provenance_casts` was not. As discussed in the tracking issue for those lints, there doesn't seem to be any good reason to enable one without the other. This PR applies this principle and as a result removes some unnecessary ptr->int `as` casts. It's also preparation for merging the two lints, which removes the option of only enabling `fuzzy_provenance_casts`. Tracking issue: #130351
1 parent 62f36da commit 49b7330

24 files changed

Lines changed: 46 additions & 29 deletions

File tree

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
// Lints:
7474
#![deny(unsafe_op_in_unsafe_fn)]
7575
#![deny(fuzzy_provenance_casts)]
76+
#![deny(lossy_provenance_casts)]
7677
#![warn(deprecated_in_future)]
7778
#![warn(missing_debug_implementations)]
7879
#![warn(missing_docs)]

library/alloctests/benches/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(strict_provenance_lints)]
88
#![feature(test)]
99
#![deny(fuzzy_provenance_casts)]
10+
#![deny(lossy_provenance_casts)]
1011

1112
extern crate test;
1213

library/alloctests/tests/boxed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ fn box_clone_from_ptr_stability() {
4747
for size in (0..8).map(|i| 2usize.pow(i)) {
4848
let control = vec![Dummy { _data: 42 }; size].into_boxed_slice();
4949
let mut copy = vec![Dummy { _data: 84 }; size].into_boxed_slice();
50-
let copy_raw = copy.as_ptr() as usize;
50+
let copy_raw = copy.as_ptr();
5151
copy.clone_from(&control);
52-
assert_eq!(copy.as_ptr() as usize, copy_raw);
52+
assert_eq!(copy.as_ptr(), copy_raw);
5353
}
5454
}
5555

library/alloctests/tests/heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn check_overalign_requests<T: Allocator>(allocator: T) {
2525
.collect();
2626
for &ptr in &pointers {
2727
assert_eq!(
28-
(ptr.as_non_null_ptr().as_ptr() as usize) % align,
28+
ptr.as_non_null_ptr().as_ptr().addr() % align,
2929
0,
3030
"Got a pointer less aligned than requested"
3131
)

library/alloctests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#![feature(vec_try_remove)]
4444
#![allow(internal_features)]
4545
#![deny(fuzzy_provenance_casts)]
46+
#![deny(lossy_provenance_casts)]
4647
#![deny(unsafe_op_in_unsafe_fn)]
4748

4849
extern crate alloc;

library/alloctests/tests/sort/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ fn self_cmp<T: Ord + Clone + Debug, S: Sort>(
746746
pattern_fn(len).into_iter().map(|val| type_into_fn(val)).collect::<Vec<_>>();
747747

748748
let comparison_fn = |a: &T, b: &T| {
749-
assert_ne!(a as *const T as usize, b as *const T as usize);
749+
assert_ne!(a as *const T, b as *const T);
750750
a.cmp(b)
751751
};
752752

library/alloctests/tests/vec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ fn test_into_iter_zst() {
11101110
struct AlignedZstWithDrop([u64; 0]);
11111111
impl Drop for AlignedZstWithDrop {
11121112
fn drop(&mut self) {
1113-
let addr = self as *mut _ as usize;
1113+
let addr = (self as *mut Self).addr();
11141114
assert!(hint::black_box(addr) % align_of::<u64>() == 0);
11151115
}
11161116
}
@@ -1356,10 +1356,10 @@ fn overaligned_allocations() {
13561356
for i in 0..0x1000 {
13571357
v.reserve_exact(i);
13581358
assert!(v[0].0 == 273);
1359-
assert!(v.as_ptr() as usize & 0xff == 0);
1359+
assert!(v.as_ptr().addr() & 0xff == 0);
13601360
v.shrink_to_fit();
13611361
assert!(v[0].0 == 273);
1362-
assert!(v.as_ptr() as usize & 0xff == 0);
1362+
assert!(v.as_ptr().addr() & 0xff == 0);
13631363
}
13641364
}
13651365

@@ -2574,7 +2574,7 @@ fn test_box_zero_allocator() {
25742574

25752575
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
25762576
if layout.size() == 0 {
2577-
let addr = ptr.as_ptr() as usize;
2577+
let addr = ptr.as_ptr().addr();
25782578
let mut state = self.state.borrow_mut();
25792579
std::println!("freeing {addr}");
25802580
assert!(state.0.remove(&addr), "ZST free that wasn't allocated");

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#![deny(rust_2021_incompatible_or_patterns)]
8181
#![deny(unsafe_op_in_unsafe_fn)]
8282
#![deny(fuzzy_provenance_casts)]
83+
#![deny(lossy_provenance_casts)]
8384
#![warn(deprecated_in_future)]
8485
#![warn(missing_debug_implementations)]
8586
#![warn(missing_docs)]

library/core/src/ptr/const_ptr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl<T: PointeeSized> *const T {
183183
/// [`with_exposed_provenance`]: with_exposed_provenance
184184
#[inline(always)]
185185
#[stable(feature = "exposed_provenance", since = "1.84.0")]
186+
#[expect(lossy_provenance_casts, reason = "this *is* the replacement")]
186187
pub fn expose_provenance(self) -> usize {
187188
self.cast::<()>() as usize
188189
}

library/core/src/ptr/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,21 +2588,21 @@ impl<F: FnPtr> Ord for F {
25882588
#[stable(feature = "fnptr_impls", since = "1.4.0")]
25892589
impl<F: FnPtr> hash::Hash for F {
25902590
fn hash<HH: hash::Hasher>(&self, state: &mut HH) {
2591-
state.write_usize(self.addr() as _)
2591+
state.write_usize(self.addr().addr())
25922592
}
25932593
}
25942594

25952595
#[stable(feature = "fnptr_impls", since = "1.4.0")]
25962596
impl<F: FnPtr> fmt::Pointer for F {
25972597
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2598-
fmt::pointer_fmt_inner(self.addr() as _, f)
2598+
fmt::pointer_fmt_inner(self.addr().addr(), f)
25992599
}
26002600
}
26012601

26022602
#[stable(feature = "fnptr_impls", since = "1.4.0")]
26032603
impl<F: FnPtr> fmt::Debug for F {
26042604
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2605-
fmt::pointer_fmt_inner(self.addr() as _, f)
2605+
fmt::pointer_fmt_inner(self.addr().addr(), f)
26062606
}
26072607
}
26082608

0 commit comments

Comments
 (0)