Skip to content

Commit 8ba4101

Browse files
committed
add documentation to functions that call do_check and add a test against lint ordering changing
1 parent 6fdd1db commit 8ba4101

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,10 @@ fn is_layout_incompatible<'tcx>(cx: &LateContext<'tcx>, from: Ty<'tcx>, to: Ty<'
694694
}
695695
}
696696

697-
/// Check if the the type conversion can be expressed as a pointer cast, instead of a transmute.
697+
/// Check if the 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.
698701
fn can_be_expressed_as_pointer_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
699702
use CastKind::*;
700703
matches!(
@@ -710,7 +713,10 @@ fn can_be_expressed_as_pointer_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<
710713
)
711714
}
712715

713-
/// If a cast from from_ty to to_ty is valid, returns an Ok containing the kind of the cast.
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.
714720
fn check_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> Option<CastKind> {
715721
let hir_id = e.hir_id;
716722
let local_def_id = hir_id.owner;

src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs

+11
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,14 @@ fn main() {
6363
};
6464
let usize_from_fn_ptr = foo as *const usize;
6565
}
66+
67+
// If a ref-to-ptr cast of this form where the pointer type points to a type other
68+
// than the referenced type, calling `CastCheck::do_check` has been observed to
69+
// cause an ICE error message. `do_check` is currently called inside the
70+
// `transmutes_expressible_as_ptr_casts` check, but other, more specific lints
71+
// currently prevent it from being called in these cases. This test is meant to
72+
// fail if the ordering of the checks ever changes enough to cause these cases to
73+
// fall through into `do_check`.
74+
fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 {
75+
unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
76+
}

0 commit comments

Comments
 (0)