Programmatically convert some of the pat ctors

This commit is contained in:
Michael Goulet 2024-03-21 17:11:06 -04:00
parent 0b810866ef
commit c92b350581
19 changed files with 29 additions and 29 deletions

View File

@ -87,7 +87,7 @@ fn is_child_of_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
/// the type is one of those slices
fn get_raw_slice_ty_mut(ty: Ty<'_>) -> Option<TypeAndMut<'_>> {
match ty.kind() {
ty::RawPtr(TypeAndMut { ty: slice_ty, mutbl }) => match slice_ty.kind() {
ty::RawPtr(slice_ty, mutbl) => match slice_ty.kind() {
ty::Slice(ty) => Some(TypeAndMut { ty: *ty, mutbl: *mutbl }),
_ => None,
},

View File

@ -33,8 +33,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) {
if let ExprKind::Cast(cast_expr, cast_to_hir_ty) = expr.kind
&& let (cast_from, cast_to) = (cx.typeck_results().expr_ty(cast_expr), cx.typeck_results().expr_ty(expr))
&& let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, .. }) = cast_from.kind()
&& let ty::RawPtr(TypeAndMut { ty: to_pointee_ty, mutbl: to_mutbl }) = cast_to.kind()
&& let ty::RawPtr(_, from_mutbl) = cast_from.kind()
&& let ty::RawPtr(to_pointee_ty, to_mutbl) = cast_to.kind()
&& matches!((from_mutbl, to_mutbl),
(Mutability::Not, Mutability::Not) | (Mutability::Mut, Mutability::Mut))
// The `U` in `pointer::cast` have to be `Sized`

View File

@ -21,7 +21,7 @@ pub(super) fn check<'tcx>(
);
if matches!(cast_from.kind(), ty::Ref(..))
&& let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, .. }) = cast_to.kind()
&& let ty::RawPtr(_, to_mutbl) = cast_to.kind()
&& let Some(use_cx) = expr_use_ctxt(cx, expr)
// TODO: only block the lint if `cast_expr` is a temporary
&& !matches!(use_cx.node, ExprUseNode::Local(_) | ExprUseNode::ConstStatic(_))

View File

@ -44,7 +44,7 @@ impl LateLintPass<'_> for FromRawWithVoidPtr {
&& seg.ident.name == sym!(from_raw)
&& let Some(type_str) = path_def_id(cx, ty).and_then(|id| def_id_matches_type(cx, id))
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind()
&& let RawPtr(TypeAndMut { ty, .. }) = arg_kind
&& let ty::RawPtr(ty, _) = arg_kind
&& is_c_void(cx, *ty)
{
let msg = format!("creating a `{type_str}` from a void raw pointer");

View File

@ -207,7 +207,7 @@ fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, tys: &mut DefIdSet)
},
ty::Tuple(args) => args.iter().any(|ty| is_mutable_ty(cx, ty, tys)),
ty::Array(ty, _) | ty::Slice(ty) => is_mutable_ty(cx, ty, tys),
ty::RawPtr(ty::TypeAndMut { ty, mutbl }) | ty::Ref(_, ty, mutbl) => {
ty::RawPtr(ty, mutbl) | ty::Ref(_, ty, mutbl) => {
mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, tys)
},
// calling something constitutes a side effect, so return true on all callables

View File

@ -75,7 +75,7 @@ fn check_raw_ptr<'tcx>(
}
fn raw_ptr_arg(cx: &LateContext<'_>, arg: &hir::Param<'_>) -> Option<hir::HirId> {
if let (&hir::PatKind::Binding(_, id, _, _), Some(&ty::RawPtr(_))) = (
if let (&hir::PatKind::Binding(_, id, _, _), Some(&ty::RawPtr(_, _))) = (
&arg.pat.kind,
cx.maybe_typeck_results()
.map(|typeck_results| typeck_results.pat_ty(arg.pat).kind()),

View File

@ -149,7 +149,7 @@ impl<'a, 'tcx> SigDropChecker<'a, 'tcx> {
false
},
rustc_middle::ty::Array(ty, _)
| rustc_middle::ty::RawPtr(TypeAndMut { ty, .. })
| rustc_middle::ty::RawPtr(ty, _)
| rustc_middle::ty::Ref(_, ty, _)
| rustc_middle::ty::Slice(ty) => self.has_sig_drop_attr(cx, *ty),
_ => false,

View File

@ -6,7 +6,7 @@ use rustc_middle::ty;
use super::ZST_OFFSET;
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
if let ty::RawPtr(ty::TypeAndMut { ty, .. }) = cx.typeck_results().expr_ty(recv).kind()
if let ty::RawPtr(ty, _) = cx.typeck_results().expr_ty(recv).kind()
&& let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(*ty))
&& layout.is_zst()
{

View File

@ -127,7 +127,7 @@ fn get_atomic_name(ty: Ty<'_>) -> Option<&'static str> {
IntTy::I128 => None,
}
},
ty::RawPtr(_) => Some("AtomicPtr"),
ty::RawPtr(_, _) => Some("AtomicPtr"),
_ => None,
}
}

View File

@ -219,7 +219,7 @@ fn ty_allowed_with_raw_pointer_heuristic<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'t
}
},
// Raw pointers are `!Send` but allowed by the heuristic
ty::RawPtr(_) => true,
ty::RawPtr(_, _) => true,
_ => false,
}
}
@ -229,7 +229,7 @@ fn contains_pointer_like<'tcx>(cx: &LateContext<'tcx>, target_ty: Ty<'tcx>) -> b
for ty_node in target_ty.walk() {
if let GenericArgKind::Type(inner_ty) = ty_node.unpack() {
match inner_ty.kind() {
ty::RawPtr(_) => {
ty::RawPtr(_, _) => {
return true;
},
ty::Adt(adt_def, _) => {

View File

@ -199,7 +199,7 @@ impl<'cx, 'others, 'tcx> AttrChecker<'cx, 'others, 'tcx> {
false
},
rustc_middle::ty::Array(ty, _)
| rustc_middle::ty::RawPtr(TypeAndMut { ty, .. })
| rustc_middle::ty::RawPtr(ty, _)
| rustc_middle::ty::Ref(_, ty, _)
| rustc_middle::ty::Slice(ty) => self.has_sig_drop_attr(*ty),
_ => false,

View File

@ -107,7 +107,7 @@ fn get_pointee_ty_and_count_expr<'tcx>(
&& METHODS.iter().any(|m| *m == method_ident)
// Get the pointee type
&& let ty::RawPtr(TypeAndMut { ty: pointee_ty, .. }) =
&& let ty::RawPtr(pointee_ty, _) =
cx.typeck_results().expr_ty(ptr_self).kind()
{
return Some((*pointee_ty, count));

View File

@ -16,7 +16,7 @@ pub(super) fn check<'tcx>(
arg: &'tcx Expr<'_>,
) -> bool {
match (&from_ty.kind(), &to_ty.kind()) {
(ty::RawPtr(_), ty::RawPtr(to_ty)) => {
(ty::RawPtr(_, _), ty::RawPtr(to_ty)) => {
span_lint_and_then(
cx,
TRANSMUTE_PTR_TO_PTR,

View File

@ -45,8 +45,8 @@ pub(super) fn check<'tcx>(
// ptr <-> ptr
(ReducedTy::Other(from_sub_ty), ReducedTy::Other(to_sub_ty))
if matches!(from_sub_ty.kind(), ty::Ref(..) | ty::RawPtr(_))
&& matches!(to_sub_ty.kind(), ty::Ref(..) | ty::RawPtr(_)) =>
if matches!(from_sub_ty.kind(), ty::Ref(..) | ty::RawPtr(_, _))
&& matches!(to_sub_ty.kind(), ty::Ref(..) | ty::RawPtr(_, _)) =>
{
from_ty = from_sub_ty;
to_ty = to_sub_ty;
@ -196,21 +196,21 @@ fn reduce_refs<'tcx>(cx: &LateContext<'tcx>, mut from_ty: Ty<'tcx>, mut to_ty: T
let (from_fat_ptr, to_fat_ptr) = loop {
break match (from_ty.kind(), to_ty.kind()) {
(
&(ty::Ref(_, from_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: from_sub_ty, .. })),
&(ty::Ref(_, to_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: to_sub_ty, .. })),
&(ty::Ref(_, from_sub_ty, _) | ty::RawPtr(from_sub_ty, _)),
&(ty::Ref(_, to_sub_ty, _) | ty::RawPtr(to_sub_ty, _)),
) => {
from_raw_ptr = matches!(*from_ty.kind(), ty::RawPtr(_));
from_raw_ptr = matches!(*from_ty.kind(), ty::RawPtr(_, _));
from_ty = from_sub_ty;
to_raw_ptr = matches!(*to_ty.kind(), ty::RawPtr(_));
to_raw_ptr = matches!(*to_ty.kind(), ty::RawPtr(_, _));
to_ty = to_sub_ty;
continue;
},
(&(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. })), _)
(&(ty::Ref(_, unsized_ty, _) | ty::RawPtr(unsized_ty, _)), _)
if !unsized_ty.is_sized(cx.tcx, cx.param_env) =>
{
(true, false)
},
(_, &(ty::Ref(_, unsized_ty, _) | ty::RawPtr(TypeAndMut { ty: unsized_ty, .. })))
(_, &(ty::Ref(_, unsized_ty, _) | ty::RawPtr(unsized_ty, _)))
if !unsized_ty.is_sized(cx.tcx, cx.param_env) =>
{
(false, true)

View File

@ -53,7 +53,7 @@ pub(super) fn check<'tcx>(
}
true
},
(ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => {
(ty::Int(_) | ty::Uint(_), ty::RawPtr(_, _)) => {
span_lint_and_then(
cx,
USELESS_TRANSMUTE,

View File

@ -8,7 +8,7 @@ use rustc_middle::ty::{self, Ty};
/// Returns `true` if it's triggered, otherwise returns `false`.
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
match (&from_ty.kind(), &to_ty.kind()) {
(ty::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_)) => {
(ty::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_, _)) => {
span_lint(
cx,
WRONG_TRANSMUTE,

View File

@ -819,7 +819,7 @@ pub fn mir_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::Const<'tcx>) ->
ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(
int.try_into().expect("invalid f64 bit representation"),
))),
ty::RawPtr(_) => Some(Constant::RawPtr(int.assert_bits(int.size()))),
ty::RawPtr(_, _) => Some(Constant::RawPtr(int.assert_bits(int.size()))),
_ => None,
},
(_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Str) => {

View File

@ -1040,7 +1040,7 @@ pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind {
.get(child_id)
.map_or(&[][..], |x| &**x)
{
if let rustc_ty::RawPtr(TypeAndMut { mutbl: mutability, .. }) | rustc_ty::Ref(_, _, mutability) =
if let rustc_ty::RawPtr(_, mutability) | rustc_ty::Ref(_, _, mutability) =
*adjust.last().map_or(target, |a| a.target).kind()
{
return CaptureKind::Ref(mutability);
@ -3235,7 +3235,7 @@ fn get_path_to_ty<'tcx>(tcx: TyCtxt<'tcx>, from: LocalDefId, ty: Ty<'tcx>, args:
rustc_ty::Array(..)
| rustc_ty::Dynamic(..)
| rustc_ty::Never
| rustc_ty::RawPtr(_)
| rustc_ty::RawPtr(_, _)
| rustc_ty::Ref(..)
| rustc_ty::Slice(_)
| rustc_ty::Tuple(_) => format!("<{}>", EarlyBinder::bind(ty).instantiate(tcx, args)),

View File

@ -321,7 +321,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
match ty.kind() {
ty::Adt(adt, _) => cx.tcx.has_attr(adt.did(), sym::must_use),
ty::Foreign(did) => cx.tcx.has_attr(*did, sym::must_use),
ty::Slice(ty) | ty::Array(ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) | ty::Ref(_, ty, _) => {
ty::Slice(ty) | ty::Array(ty, _) | ty::RawPtr(ty, _) | ty::Ref(_, ty, _) => {
// for the Array case we don't need to care for the len == 0 case
// because we don't want to lint functions returning empty arrays
is_must_use_ty(cx, *ty)