Skip to content

Commit 09bd400

Browse files
committed
Auto merge of rust-lang#5891 - flip1995:rustup, r=flip1995
Rustup r? @ghost Sync back rust-lang#75098 changelog: none
2 parents cc5bfd4 + 9311c11 commit 09bd400

8 files changed

+332
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,7 @@ Released 2018-09-13
17351735
[`transmute_int_to_float`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_float
17361736
[`transmute_ptr_to_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr
17371737
[`transmute_ptr_to_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ref
1738+
[`transmutes_expressible_as_ptr_casts`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmutes_expressible_as_ptr_casts
17381739
[`transmuting_null`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmuting_null
17391740
[`trivial_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#trivial_regex
17401741
[`trivially_copy_pass_by_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref

clippy_lints/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
791791
&trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS,
792792
&trait_bounds::TYPE_REPETITION_IN_BOUNDS,
793793
&transmute::CROSSPOINTER_TRANSMUTE,
794+
&transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
794795
&transmute::TRANSMUTE_BYTES_TO_STR,
795796
&transmute::TRANSMUTE_FLOAT_TO_INT,
796797
&transmute::TRANSMUTE_INT_TO_BOOL,
@@ -1427,6 +1428,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14271428
LintId::of(&temporary_assignment::TEMPORARY_ASSIGNMENT),
14281429
LintId::of(&to_digit_is_some::TO_DIGIT_IS_SOME),
14291430
LintId::of(&transmute::CROSSPOINTER_TRANSMUTE),
1431+
LintId::of(&transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS),
14301432
LintId::of(&transmute::TRANSMUTE_BYTES_TO_STR),
14311433
LintId::of(&transmute::TRANSMUTE_FLOAT_TO_INT),
14321434
LintId::of(&transmute::TRANSMUTE_INT_TO_BOOL),
@@ -1629,6 +1631,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16291631
LintId::of(&swap::MANUAL_SWAP),
16301632
LintId::of(&temporary_assignment::TEMPORARY_ASSIGNMENT),
16311633
LintId::of(&transmute::CROSSPOINTER_TRANSMUTE),
1634+
LintId::of(&transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS),
16321635
LintId::of(&transmute::TRANSMUTE_BYTES_TO_STR),
16331636
LintId::of(&transmute::TRANSMUTE_FLOAT_TO_INT),
16341637
LintId::of(&transmute::TRANSMUTE_INT_TO_BOOL),

clippy_lints/src/transmute.rs

+105-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use rustc_ast::ast;
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind, GenericArg, Mutability, QPath, TyKind, UnOp};
99
use rustc_lint::{LateContext, LateLintPass};
10-
use rustc_middle::ty::{self, Ty};
10+
use rustc_middle::ty::{self, cast::CastKind, Ty};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
12+
use rustc_span::DUMMY_SP;
13+
use rustc_typeck::check::{cast::CastCheck, FnCtxt, Inherited};
1214
use std::borrow::Cow;
1315

1416
declare_clippy_lint! {
@@ -48,6 +50,31 @@ declare_clippy_lint! {
4850
"transmutes that have the same to and from types or could be a cast/coercion"
4951
}
5052

53+
// FIXME: Merge this lint with USELESS_TRANSMUTE once that is out of the nursery.
54+
declare_clippy_lint! {
55+
/// **What it does:**Checks for transmutes that could be a pointer cast.
56+
///
57+
/// **Why is this bad?** Readability. The code tricks people into thinking that
58+
/// something complex is going on.
59+
///
60+
/// **Known problems:** None.
61+
///
62+
/// **Example:**
63+
///
64+
/// ```rust
65+
/// # let p: *const [i32] = &[];
66+
/// unsafe { std::mem::transmute::<*const [i32], *const [u16]>(p) };
67+
/// ```
68+
/// Use instead:
69+
/// ```rust
70+
/// # let p: *const [i32] = &[];
71+
/// p as *const [u16];
72+
/// ```
73+
pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
74+
complexity,
75+
"transmutes that could be a pointer cast"
76+
}
77+
5178
declare_clippy_lint! {
5279
/// **What it does:** Checks for transmutes between a type `T` and `*T`.
5380
///
@@ -269,6 +296,7 @@ declare_clippy_lint! {
269296
correctness,
270297
"transmute between collections of layout-incompatible types"
271298
}
299+
272300
declare_lint_pass!(Transmute => [
273301
CROSSPOINTER_TRANSMUTE,
274302
TRANSMUTE_PTR_TO_REF,
@@ -281,6 +309,7 @@ declare_lint_pass!(Transmute => [
281309
TRANSMUTE_INT_TO_FLOAT,
282310
TRANSMUTE_FLOAT_TO_INT,
283311
UNSOUND_COLLECTION_TRANSMUTE,
312+
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
284313
]);
285314

286315
// used to check for UNSOUND_COLLECTION_TRANSMUTE
@@ -601,7 +630,25 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
601630
);
602631
}
603632
},
604-
_ => return,
633+
(_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then(
634+
cx,
635+
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
636+
e.span,
637+
&format!(
638+
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
639+
from_ty,
640+
to_ty
641+
),
642+
|diag| {
643+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
644+
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
645+
diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
646+
}
647+
}
648+
),
649+
_ => {
650+
return;
651+
},
605652
}
606653
}
607654
}
@@ -648,3 +695,59 @@ fn is_layout_incompatible<'tcx>(cx: &LateContext<'tcx>, from: Ty<'tcx>, to: Ty<'
648695
false
649696
}
650697
}
698+
699+
/// Check if the type conversion can be expressed as a pointer cast, instead of
700+
/// a transmute. In certain cases, including some invalid casts from array
701+
/// references to pointers, this may cause additional errors to be emitted and/or
702+
/// ICE error messages. This function will panic if that occurs.
703+
fn can_be_expressed_as_pointer_cast<'tcx>(
704+
cx: &LateContext<'tcx>,
705+
e: &'tcx Expr<'_>,
706+
from_ty: Ty<'tcx>,
707+
to_ty: Ty<'tcx>,
708+
) -> bool {
709+
use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
710+
matches!(
711+
check_cast(cx, e, from_ty, to_ty),
712+
Some(PtrPtrCast | PtrAddrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast)
713+
)
714+
}
715+
716+
/// If a cast from `from_ty` to `to_ty` is valid, returns an Ok containing the kind of
717+
/// the cast. In certain cases, including some invalid casts from array references
718+
/// to pointers, this may cause additional errors to be emitted and/or ICE error
719+
/// messages. This function will panic if that occurs.
720+
fn check_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> Option<CastKind> {
721+
let hir_id = e.hir_id;
722+
let local_def_id = hir_id.owner;
723+
724+
Inherited::build(cx.tcx, local_def_id).enter(|inherited| {
725+
let fn_ctxt = FnCtxt::new(&inherited, cx.param_env, hir_id);
726+
727+
// If we already have errors, we can't be sure we can pointer cast.
728+
assert!(
729+
!fn_ctxt.errors_reported_since_creation(),
730+
"Newly created FnCtxt contained errors"
731+
);
732+
733+
if let Ok(check) = CastCheck::new(
734+
&fn_ctxt, e, from_ty, to_ty,
735+
// We won't show any error to the user, so we don't care what the span is here.
736+
DUMMY_SP, DUMMY_SP,
737+
) {
738+
let res = check.do_check(&fn_ctxt);
739+
740+
// do_check's documentation says that it might return Ok and create
741+
// errors in the fcx instead of returing Err in some cases. Those cases
742+
// should be filtered out before getting here.
743+
assert!(
744+
!fn_ctxt.errors_reported_since_creation(),
745+
"`fn_ctxt` contained errors after cast check!"
746+
);
747+
748+
res.ok()
749+
} else {
750+
None
751+
}
752+
})
753+
}

src/lintlist/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
22502250
deprecation: None,
22512251
module: "transmute",
22522252
},
2253+
Lint {
2254+
name: "transmutes_expressible_as_ptr_casts",
2255+
group: "complexity",
2256+
desc: "transmutes that could be a pointer cast",
2257+
deprecation: None,
2258+
module: "transmute",
2259+
},
22532260
Lint {
22542261
name: "transmuting_null",
22552262
group: "correctness",

tests/compile-test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
216216
Ok(result)
217217
}
218218

219+
if cargo::is_rustc_test_suite() {
220+
return;
221+
}
222+
219223
config.mode = TestMode::Ui;
220224
config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
221225

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// run-rustfix
2+
#![warn(clippy::transmutes_expressible_as_ptr_casts)]
3+
// These two warnings currrently cover the cases transmutes_expressible_as_ptr_casts
4+
// would otherwise be responsible for
5+
#![warn(clippy::useless_transmute)]
6+
#![warn(clippy::transmute_ptr_to_ptr)]
7+
#![allow(unused_unsafe)]
8+
#![allow(dead_code)]
9+
10+
use std::mem::{size_of, transmute};
11+
12+
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
13+
// valid, which we quote from below.
14+
fn main() {
15+
// We should see an error message for each transmute, and no error messages for
16+
// the casts, since the casts are the recommended fixes.
17+
18+
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
19+
let _ptr_i32_transmute = unsafe { usize::MAX as *const i32 };
20+
let ptr_i32 = usize::MAX as *const i32;
21+
22+
// e has type *T, U is *U_0, and either U_0: Sized ...
23+
let _ptr_i8_transmute = unsafe { ptr_i32 as *const i8 };
24+
let _ptr_i8 = ptr_i32 as *const i8;
25+
26+
let slice_ptr = &[0, 1, 2, 3] as *const [i32];
27+
28+
// ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
29+
let _ptr_to_unsized_transmute = unsafe { slice_ptr as *const [u16] };
30+
let _ptr_to_unsized = slice_ptr as *const [u16];
31+
// TODO: We could try testing vtable casts here too, but maybe
32+
// we should wait until std::raw::TraitObject is stabilized?
33+
34+
// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
35+
let _usize_from_int_ptr_transmute = unsafe { ptr_i32 as usize };
36+
let _usize_from_int_ptr = ptr_i32 as usize;
37+
38+
let array_ref: &[i32; 4] = &[1, 2, 3, 4];
39+
40+
// e has type &[T; n] and U is *const T; array-ptr-cast
41+
let _array_ptr_transmute = unsafe { array_ref as *const [i32; 4] };
42+
let _array_ptr = array_ref as *const [i32; 4];
43+
44+
fn foo(_: usize) -> u8 {
45+
42
46+
}
47+
48+
// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
49+
let _usize_ptr_transmute = unsafe { foo as *const usize };
50+
let _usize_ptr_transmute = foo as *const usize;
51+
52+
// e is a function pointer type and U is an integer; fptr-addr-cast
53+
let _usize_from_fn_ptr_transmute = unsafe { foo as usize };
54+
let _usize_from_fn_ptr = foo as *const usize;
55+
}
56+
57+
// If a ref-to-ptr cast of this form where the pointer type points to a type other
58+
// than the referenced type, calling `CastCheck::do_check` has been observed to
59+
// cause an ICE error message. `do_check` is currently called inside the
60+
// `transmutes_expressible_as_ptr_casts` check, but other, more specific lints
61+
// currently prevent it from being called in these cases. This test is meant to
62+
// fail if the ordering of the checks ever changes enough to cause these cases to
63+
// fall through into `do_check`.
64+
fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 {
65+
unsafe { in_param as *const [i32; 1] as *const u8 }
66+
}
67+
68+
#[repr(C)]
69+
struct Single(u64);
70+
71+
#[repr(C)]
72+
struct Pair(u32, u32);
73+
74+
fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {
75+
assert_eq!(size_of::<Single>(), size_of::<Pair>());
76+
77+
unsafe { transmute::<Single, Pair>(in_param) }
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// run-rustfix
2+
#![warn(clippy::transmutes_expressible_as_ptr_casts)]
3+
// These two warnings currrently cover the cases transmutes_expressible_as_ptr_casts
4+
// would otherwise be responsible for
5+
#![warn(clippy::useless_transmute)]
6+
#![warn(clippy::transmute_ptr_to_ptr)]
7+
#![allow(unused_unsafe)]
8+
#![allow(dead_code)]
9+
10+
use std::mem::{size_of, transmute};
11+
12+
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
13+
// valid, which we quote from below.
14+
fn main() {
15+
// We should see an error message for each transmute, and no error messages for
16+
// the casts, since the casts are the recommended fixes.
17+
18+
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
19+
let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
20+
let ptr_i32 = usize::MAX as *const i32;
21+
22+
// e has type *T, U is *U_0, and either U_0: Sized ...
23+
let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
24+
let _ptr_i8 = ptr_i32 as *const i8;
25+
26+
let slice_ptr = &[0, 1, 2, 3] as *const [i32];
27+
28+
// ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
29+
let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u16]>(slice_ptr) };
30+
let _ptr_to_unsized = slice_ptr as *const [u16];
31+
// TODO: We could try testing vtable casts here too, but maybe
32+
// we should wait until std::raw::TraitObject is stabilized?
33+
34+
// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
35+
let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
36+
let _usize_from_int_ptr = ptr_i32 as usize;
37+
38+
let array_ref: &[i32; 4] = &[1, 2, 3, 4];
39+
40+
// e has type &[T; n] and U is *const T; array-ptr-cast
41+
let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
42+
let _array_ptr = array_ref as *const [i32; 4];
43+
44+
fn foo(_: usize) -> u8 {
45+
42
46+
}
47+
48+
// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
49+
let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
50+
let _usize_ptr_transmute = foo as *const usize;
51+
52+
// e is a function pointer type and U is an integer; fptr-addr-cast
53+
let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
54+
let _usize_from_fn_ptr = foo as *const usize;
55+
}
56+
57+
// If a ref-to-ptr cast of this form where the pointer type points to a type other
58+
// than the referenced type, calling `CastCheck::do_check` has been observed to
59+
// cause an ICE error message. `do_check` is currently called inside the
60+
// `transmutes_expressible_as_ptr_casts` check, but other, more specific lints
61+
// currently prevent it from being called in these cases. This test is meant to
62+
// fail if the ordering of the checks ever changes enough to cause these cases to
63+
// fall through into `do_check`.
64+
fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 {
65+
unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
66+
}
67+
68+
#[repr(C)]
69+
struct Single(u64);
70+
71+
#[repr(C)]
72+
struct Pair(u32, u32);
73+
74+
fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {
75+
assert_eq!(size_of::<Single>(), size_of::<Pair>());
76+
77+
unsafe { transmute::<Single, Pair>(in_param) }
78+
}

0 commit comments

Comments
 (0)