Skip to content

Commit 63d1e37

Browse files
authored
Rollup merge of #75098 - Ryan1729:clippy-pointer-cast-lint-experiment, r=oli-obk
Clippy pointer cast lint experiment This PR is an experiment about exposing more parts of `rustc_typeck` for use in `clippy`. In particular, the code that checks where a cast is valid or not was exposed, which necessitated exposing [`FnCtxt`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/check/struct.FnCtxt.html), and figuring out how to create an instance of that type inside `clippy`. This was prompted by [this clippy issue](rust-lang/rust-clippy#2064). r? @oli-obk
2 parents 770bd3d + d2e7293 commit 63d1e37

File tree

10 files changed

+356
-8
lines changed

10 files changed

+356
-8
lines changed

src/librustc_typeck/check/cast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
147147
}
148148

149149
#[derive(Copy, Clone)]
150-
enum CastError {
150+
pub enum CastError {
151151
ErrorReported,
152152

153153
CastToBool,
@@ -593,7 +593,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
593593
/// Checks a cast, and report an error if one exists. In some cases, this
594594
/// can return Ok and create type errors in the fcx rather than returning
595595
/// directly. coercion-cast is handled in check instead of here.
596-
fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError> {
596+
pub fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError> {
597597
use rustc_middle::ty::cast::CastTy::*;
598598
use rustc_middle::ty::cast::IntTy::*;
599599

src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type parameter).
6767
pub mod _match;
6868
mod autoderef;
6969
mod callee;
70-
mod cast;
70+
pub mod cast;
7171
mod closure;
7272
pub mod coercion;
7373
mod compare_method;
@@ -648,7 +648,7 @@ impl Inherited<'_, 'tcx> {
648648
}
649649

650650
impl<'tcx> InheritedBuilder<'tcx> {
651-
fn enter<F, R>(&mut self, f: F) -> R
651+
pub fn enter<F, R>(&mut self, f: F) -> R
652652
where
653653
F: for<'a> FnOnce(Inherited<'a, 'tcx>) -> R,
654654
{

src/librustc_typeck/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ extern crate log;
7474
#[macro_use]
7575
extern crate rustc_middle;
7676

77-
// This is used by Clippy.
77+
// These are used by Clippy.
78+
pub mod check;
7879
pub mod expr_use_visitor;
7980

8081
mod astconv;
81-
mod check;
8282
mod check_unused;
8383
mod coherence;
8484
mod collect;

src/tools/clippy/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,7 @@ Released 2018-09-13
17301730
[`transmute_int_to_float`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_float
17311731
[`transmute_ptr_to_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr
17321732
[`transmute_ptr_to_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ref
1733+
[`transmutes_expressible_as_ptr_casts`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmutes_expressible_as_ptr_casts
17331734
[`transmuting_null`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmuting_null
17341735
[`trivial_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#trivial_regex
17351736
[`trivially_copy_pass_by_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref

src/tools/clippy/clippy_lints/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
788788
&to_digit_is_some::TO_DIGIT_IS_SOME,
789789
&trait_bounds::TYPE_REPETITION_IN_BOUNDS,
790790
&transmute::CROSSPOINTER_TRANSMUTE,
791+
&transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
791792
&transmute::TRANSMUTE_BYTES_TO_STR,
792793
&transmute::TRANSMUTE_FLOAT_TO_INT,
793794
&transmute::TRANSMUTE_INT_TO_BOOL,
@@ -1417,6 +1418,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14171418
LintId::of(&temporary_assignment::TEMPORARY_ASSIGNMENT),
14181419
LintId::of(&to_digit_is_some::TO_DIGIT_IS_SOME),
14191420
LintId::of(&transmute::CROSSPOINTER_TRANSMUTE),
1421+
LintId::of(&transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS),
14201422
LintId::of(&transmute::TRANSMUTE_BYTES_TO_STR),
14211423
LintId::of(&transmute::TRANSMUTE_FLOAT_TO_INT),
14221424
LintId::of(&transmute::TRANSMUTE_INT_TO_BOOL),
@@ -1617,6 +1619,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16171619
LintId::of(&swap::MANUAL_SWAP),
16181620
LintId::of(&temporary_assignment::TEMPORARY_ASSIGNMENT),
16191621
LintId::of(&transmute::CROSSPOINTER_TRANSMUTE),
1622+
LintId::of(&transmute::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS),
16201623
LintId::of(&transmute::TRANSMUTE_BYTES_TO_STR),
16211624
LintId::of(&transmute::TRANSMUTE_FLOAT_TO_INT),
16221625
LintId::of(&transmute::TRANSMUTE_INT_TO_BOOL),

src/tools/clippy/clippy_lints/src/transmute.rs

+103-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,29 @@ 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,ignore
65+
/// core::intrinsics::transmute::<*const [i32], *const [u16]>(p)
66+
/// ```
67+
/// Use instead:
68+
/// ```rust
69+
/// p as *const [u16]
70+
/// ```
71+
pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
72+
complexity,
73+
"transmutes that could be a pointer cast"
74+
}
75+
5176
declare_clippy_lint! {
5277
/// **What it does:** Checks for transmutes between a type `T` and `*T`.
5378
///
@@ -269,6 +294,7 @@ declare_clippy_lint! {
269294
correctness,
270295
"transmute between collections of layout-incompatible types"
271296
}
297+
272298
declare_lint_pass!(Transmute => [
273299
CROSSPOINTER_TRANSMUTE,
274300
TRANSMUTE_PTR_TO_REF,
@@ -281,6 +307,7 @@ declare_lint_pass!(Transmute => [
281307
TRANSMUTE_INT_TO_FLOAT,
282308
TRANSMUTE_FLOAT_TO_INT,
283309
UNSOUND_COLLECTION_TRANSMUTE,
310+
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
284311
]);
285312

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

src/tools/clippy/src/lintlist/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
22152215
deprecation: None,
22162216
module: "transmute",
22172217
},
2218+
Lint {
2219+
name: "transmutes_expressible_as_ptr_casts",
2220+
group: "complexity",
2221+
desc: "transmutes that could be a pointer cast",
2222+
deprecation: None,
2223+
module: "transmute",
2224+
},
22182225
Lint {
22192226
name: "transmuting_null",
22202227
group: "correctness",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 {
20+
usize::MAX as *const i32
21+
};
22+
let ptr_i32 = usize::MAX as *const i32;
23+
24+
// 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+
};
28+
let _ptr_i8 = ptr_i32 as *const i8;
29+
30+
let slice_ptr = &[0,1,2,3] as *const [i32];
31+
32+
// ... 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+
};
36+
let _ptr_to_unsized = slice_ptr as *const [u16];
37+
// TODO: We could try testing vtable casts here too, but maybe
38+
// we should wait until std::raw::TraitObject is stabilized?
39+
40+
// 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+
};
44+
let _usize_from_int_ptr = ptr_i32 as usize;
45+
46+
let array_ref: &[i32; 4] = &[1,2,3,4];
47+
48+
// 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+
};
52+
let _array_ptr = array_ref as *const [i32; 4];
53+
54+
fn foo(_: usize) -> u8 { 42 }
55+
56+
// 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+
};
60+
let _usize_ptr_transmute = foo as *const usize;
61+
62+
// 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+
};
66+
let _usize_from_fn_ptr = foo as *const usize;
67+
}
68+
69+
// If a ref-to-ptr cast of this form where the pointer type points to a type other
70+
// than the referenced type, calling `CastCheck::do_check` has been observed to
71+
// cause an ICE error message. `do_check` is currently called inside the
72+
// `transmutes_expressible_as_ptr_casts` check, but other, more specific lints
73+
// currently prevent it from being called in these cases. This test is meant to
74+
// fail if the ordering of the checks ever changes enough to cause these cases to
75+
// fall through into `do_check`.
76+
fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 {
77+
unsafe { in_param as *const [i32; 1] as *const u8 }
78+
}
79+
80+
#[repr(C)]
81+
struct Single(u64);
82+
83+
#[repr(C)]
84+
struct Pair(u32, u32);
85+
86+
fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {
87+
assert_eq!(size_of::<Single>(), size_of::<Pair>());
88+
89+
unsafe { transmute::<Single, Pair>(in_param) }
90+
}

0 commit comments

Comments
 (0)