Skip to content

Commit 5cb8deb

Browse files
committed
Fix sync fallout
1 parent 9e73d33 commit 5cb8deb

5 files changed

+54
-77
lines changed

clippy_lints/src/transmute.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -704,14 +704,14 @@ fn can_be_expressed_as_pointer_cast<'tcx>(
704704
from_ty: Ty<'tcx>,
705705
to_ty: Ty<'tcx>,
706706
) -> bool {
707-
use CastKind::*;
707+
use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
708708
matches!(
709709
check_cast(cx, e, from_ty, to_ty),
710710
Some(PtrPtrCast | PtrAddrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast)
711711
)
712712
}
713713

714-
/// If a cast from from_ty to to_ty is valid, returns an Ok containing the kind of
714+
/// If a cast from `from_ty` to `to_ty` is valid, returns an Ok containing the kind of
715715
/// the cast. In certain cases, including some invalid casts from array references
716716
/// to pointers, this may cause additional errors to be emitted and/or ICE error
717717
/// messages. This function will panic if that occurs.

tests/compile-test.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ fn run_ui_toml(config: &mut compiletest::Config) {
147147
}
148148

149149
fn run_ui_cargo(config: &mut compiletest::Config) {
150-
if cargo::is_rustc_test_suite() {
151-
return;
152-
}
153150
fn run_tests(
154151
config: &compiletest::Config,
155152
filter: &Option<String>,
@@ -217,6 +214,10 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
217214
Ok(result)
218215
}
219216

217+
if cargo::is_rustc_test_suite() {
218+
return;
219+
}
220+
220221
config.mode = TestMode::Ui;
221222
config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
222223

tests/ui/transmutes_expressible_as_ptr_casts.fixed

+13-25
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,48 @@
99

1010
use std::mem::{size_of, transmute};
1111

12-
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
12+
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
1313
// valid, which we quote from below.
1414
fn main() {
1515
// We should see an error message for each transmute, and no error messages for
1616
// the casts, since the casts are the recommended fixes.
1717

1818
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
19-
let _ptr_i32_transmute = unsafe {
20-
usize::MAX as *const i32
21-
};
19+
let _ptr_i32_transmute = unsafe { usize::MAX as *const i32 };
2220
let ptr_i32 = usize::MAX as *const i32;
2321

2422
// e has type *T, U is *U_0, and either U_0: Sized ...
25-
let _ptr_i8_transmute = unsafe {
26-
ptr_i32 as *const i8
27-
};
23+
let _ptr_i8_transmute = unsafe { ptr_i32 as *const i8 };
2824
let _ptr_i8 = ptr_i32 as *const i8;
2925

30-
let slice_ptr = &[0,1,2,3] as *const [i32];
26+
let slice_ptr = &[0, 1, 2, 3] as *const [i32];
3127

3228
// ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
33-
let _ptr_to_unsized_transmute = unsafe {
34-
slice_ptr as *const [u16]
35-
};
29+
let _ptr_to_unsized_transmute = unsafe { slice_ptr as *const [u16] };
3630
let _ptr_to_unsized = slice_ptr as *const [u16];
3731
// TODO: We could try testing vtable casts here too, but maybe
3832
// we should wait until std::raw::TraitObject is stabilized?
3933

4034
// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
41-
let _usize_from_int_ptr_transmute = unsafe {
42-
ptr_i32 as usize
43-
};
35+
let _usize_from_int_ptr_transmute = unsafe { ptr_i32 as usize };
4436
let _usize_from_int_ptr = ptr_i32 as usize;
4537

46-
let array_ref: &[i32; 4] = &[1,2,3,4];
38+
let array_ref: &[i32; 4] = &[1, 2, 3, 4];
4739

4840
// e has type &[T; n] and U is *const T; array-ptr-cast
49-
let _array_ptr_transmute = unsafe {
50-
array_ref as *const [i32; 4]
51-
};
41+
let _array_ptr_transmute = unsafe { array_ref as *const [i32; 4] };
5242
let _array_ptr = array_ref as *const [i32; 4];
5343

54-
fn foo(_: usize) -> u8 { 42 }
44+
fn foo(_: usize) -> u8 {
45+
42
46+
}
5547

5648
// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
57-
let _usize_ptr_transmute = unsafe {
58-
foo as *const usize
59-
};
49+
let _usize_ptr_transmute = unsafe { foo as *const usize };
6050
let _usize_ptr_transmute = foo as *const usize;
6151

6252
// e is a function pointer type and U is an integer; fptr-addr-cast
63-
let _usize_from_fn_ptr_transmute = unsafe {
64-
foo as usize
65-
};
53+
let _usize_from_fn_ptr_transmute = unsafe { foo as usize };
6654
let _usize_from_fn_ptr = foo as *const usize;
6755
}
6856

tests/ui/transmutes_expressible_as_ptr_casts.rs

+13-25
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,48 @@
99

1010
use std::mem::{size_of, transmute};
1111

12-
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
12+
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
1313
// valid, which we quote from below.
1414
fn main() {
1515
// We should see an error message for each transmute, and no error messages for
1616
// the casts, since the casts are the recommended fixes.
1717

1818
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
19-
let _ptr_i32_transmute = unsafe {
20-
transmute::<usize, *const i32>(usize::MAX)
21-
};
19+
let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
2220
let ptr_i32 = usize::MAX as *const i32;
2321

2422
// e has type *T, U is *U_0, and either U_0: Sized ...
25-
let _ptr_i8_transmute = unsafe {
26-
transmute::<*const i32, *const i8>(ptr_i32)
27-
};
23+
let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
2824
let _ptr_i8 = ptr_i32 as *const i8;
2925

30-
let slice_ptr = &[0,1,2,3] as *const [i32];
26+
let slice_ptr = &[0, 1, 2, 3] as *const [i32];
3127

3228
// ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
33-
let _ptr_to_unsized_transmute = unsafe {
34-
transmute::<*const [i32], *const [u16]>(slice_ptr)
35-
};
29+
let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u16]>(slice_ptr) };
3630
let _ptr_to_unsized = slice_ptr as *const [u16];
3731
// TODO: We could try testing vtable casts here too, but maybe
3832
// we should wait until std::raw::TraitObject is stabilized?
3933

4034
// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
41-
let _usize_from_int_ptr_transmute = unsafe {
42-
transmute::<*const i32, usize>(ptr_i32)
43-
};
35+
let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
4436
let _usize_from_int_ptr = ptr_i32 as usize;
4537

46-
let array_ref: &[i32; 4] = &[1,2,3,4];
38+
let array_ref: &[i32; 4] = &[1, 2, 3, 4];
4739

4840
// e has type &[T; n] and U is *const T; array-ptr-cast
49-
let _array_ptr_transmute = unsafe {
50-
transmute::<&[i32; 4], *const [i32; 4]>(array_ref)
51-
};
41+
let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
5242
let _array_ptr = array_ref as *const [i32; 4];
5343

54-
fn foo(_: usize) -> u8 { 42 }
44+
fn foo(_: usize) -> u8 {
45+
42
46+
}
5547

5648
// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
57-
let _usize_ptr_transmute = unsafe {
58-
transmute::<fn(usize) -> u8, *const usize>(foo)
59-
};
49+
let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
6050
let _usize_ptr_transmute = foo as *const usize;
6151

6252
// e is a function pointer type and U is an integer; fptr-addr-cast
63-
let _usize_from_fn_ptr_transmute = unsafe {
64-
transmute::<fn(usize) -> u8, usize>(foo)
65-
};
53+
let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
6654
let _usize_from_fn_ptr = foo as *const usize;
6755
}
6856

tests/ui/transmutes_expressible_as_ptr_casts.stderr

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
error: transmute from an integer to a pointer
2-
--> $DIR/transmutes_expressible_as_ptr_casts.rs:20:9
2+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:19:39
33
|
4-
LL | transmute::<usize, *const i32>(usize::MAX)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `usize::MAX as *const i32`
4+
LL | let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `usize::MAX as *const i32`
66
|
77
= note: `-D clippy::useless-transmute` implied by `-D warnings`
88

99
error: transmute from a pointer to a pointer
10-
--> $DIR/transmutes_expressible_as_ptr_casts.rs:26:9
10+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:23:38
1111
|
12-
LL | transmute::<*const i32, *const i8>(ptr_i32)
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
12+
LL | let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
1414
|
1515
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
1616

1717
error: transmute from a pointer to a pointer
18-
--> $DIR/transmutes_expressible_as_ptr_casts.rs:34:9
18+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:29:46
1919
|
20-
LL | transmute::<*const [i32], *const [u16]>(slice_ptr)
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]`
20+
LL | let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u16]>(slice_ptr) };
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]`
2222

2323
error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead
24-
--> $DIR/transmutes_expressible_as_ptr_casts.rs:42:9
24+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:35:50
2525
|
26-
LL | transmute::<*const i32, usize>(ptr_i32)
27-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
26+
LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
2828
|
2929
= note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings`
3030

3131
error: transmute from a reference to a pointer
32-
--> $DIR/transmutes_expressible_as_ptr_casts.rs:50:9
32+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:41:41
3333
|
34-
LL | transmute::<&[i32; 4], *const [i32; 4]>(array_ref)
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`
34+
LL | let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`
3636

3737
error: transmute from `fn(usize) -> u8 {main::foo}` to `*const usize` which could be expressed as a pointer cast instead
38-
--> $DIR/transmutes_expressible_as_ptr_casts.rs:58:9
38+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:49:41
3939
|
40-
LL | transmute::<fn(usize) -> u8, *const usize>(foo)
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
40+
LL | let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
4242

4343
error: transmute from `fn(usize) -> u8 {main::foo}` to `usize` which could be expressed as a pointer cast instead
44-
--> $DIR/transmutes_expressible_as_ptr_casts.rs:64:9
44+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:53:49
4545
|
46-
LL | transmute::<fn(usize) -> u8, usize>(foo)
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
46+
LL | let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
4848

4949
error: transmute from a reference to a pointer
50-
--> $DIR/transmutes_expressible_as_ptr_casts.rs:77:14
50+
--> $DIR/transmutes_expressible_as_ptr_casts.rs:65:14
5151
|
5252
LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8`

0 commit comments

Comments
 (0)