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 /// the type is one of those slices
fn get_raw_slice_ty_mut(ty: Ty<'_>) -> Option<TypeAndMut<'_>> { fn get_raw_slice_ty_mut(ty: Ty<'_>) -> Option<TypeAndMut<'_>> {
match ty.kind() { 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 }), ty::Slice(ty) => Some(TypeAndMut { ty: *ty, mutbl: *mutbl }),
_ => None, _ => 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 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 (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(_, from_mutbl) = cast_from.kind()
&& let ty::RawPtr(TypeAndMut { ty: to_pointee_ty, mutbl: to_mutbl }) = cast_to.kind() && let ty::RawPtr(to_pointee_ty, to_mutbl) = cast_to.kind()
&& matches!((from_mutbl, to_mutbl), && matches!((from_mutbl, to_mutbl),
(Mutability::Not, Mutability::Not) | (Mutability::Mut, Mutability::Mut)) (Mutability::Not, Mutability::Not) | (Mutability::Mut, Mutability::Mut))
// The `U` in `pointer::cast` have to be `Sized` // 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(..)) 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) && let Some(use_cx) = expr_use_ctxt(cx, expr)
// TODO: only block the lint if `cast_expr` is a temporary // TODO: only block the lint if `cast_expr` is a temporary
&& !matches!(use_cx.node, ExprUseNode::Local(_) | ExprUseNode::ConstStatic(_)) && !matches!(use_cx.node, ExprUseNode::Local(_) | ExprUseNode::ConstStatic(_))

View File

@ -44,7 +44,7 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
&& seg.ident.name == sym!(from_raw) && 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 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 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) && is_c_void(cx, *ty)
{ {
let msg = format!("creating a `{type_str}` from a void raw pointer"); 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::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::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) mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, tys)
}, },
// calling something constitutes a side effect, so return true on all callables // 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> { 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, &arg.pat.kind,
cx.maybe_typeck_results() cx.maybe_typeck_results()
.map(|typeck_results| typeck_results.pat_ty(arg.pat).kind()), .map(|typeck_results| typeck_results.pat_ty(arg.pat).kind()),

View File

@ -149,7 +149,7 @@ fn has_sig_drop_attr(&mut self, cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
false false
}, },
rustc_middle::ty::Array(ty, _) rustc_middle::ty::Array(ty, _)
| rustc_middle::ty::RawPtr(TypeAndMut { ty, .. }) | rustc_middle::ty::RawPtr(ty, _)
| rustc_middle::ty::Ref(_, ty, _) | rustc_middle::ty::Ref(_, ty, _)
| rustc_middle::ty::Slice(ty) => self.has_sig_drop_attr(cx, *ty), | rustc_middle::ty::Slice(ty) => self.has_sig_drop_attr(cx, *ty),
_ => false, _ => false,

View File

@ -6,7 +6,7 @@
use super::ZST_OFFSET; use super::ZST_OFFSET;
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) { 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)) && let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(*ty))
&& layout.is_zst() && layout.is_zst()
{ {

View File

@ -127,7 +127,7 @@ fn get_atomic_name(ty: Ty<'_>) -> Option<&'static str> {
IntTy::I128 => None, IntTy::I128 => None,
} }
}, },
ty::RawPtr(_) => Some("AtomicPtr"), ty::RawPtr(_, _) => Some("AtomicPtr"),
_ => None, _ => 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 // Raw pointers are `!Send` but allowed by the heuristic
ty::RawPtr(_) => true, ty::RawPtr(_, _) => true,
_ => false, _ => 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() { for ty_node in target_ty.walk() {
if let GenericArgKind::Type(inner_ty) = ty_node.unpack() { if let GenericArgKind::Type(inner_ty) = ty_node.unpack() {
match inner_ty.kind() { match inner_ty.kind() {
ty::RawPtr(_) => { ty::RawPtr(_, _) => {
return true; return true;
}, },
ty::Adt(adt_def, _) => { ty::Adt(adt_def, _) => {

View File

@ -199,7 +199,7 @@ fn has_sig_drop_attr_uncached(&mut self, ty: Ty<'tcx>) -> bool {
false false
}, },
rustc_middle::ty::Array(ty, _) rustc_middle::ty::Array(ty, _)
| rustc_middle::ty::RawPtr(TypeAndMut { ty, .. }) | rustc_middle::ty::RawPtr(ty, _)
| rustc_middle::ty::Ref(_, ty, _) | rustc_middle::ty::Ref(_, ty, _)
| rustc_middle::ty::Slice(ty) => self.has_sig_drop_attr(*ty), | rustc_middle::ty::Slice(ty) => self.has_sig_drop_attr(*ty),
_ => false, _ => false,

View File

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

View File

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

View File

@ -45,8 +45,8 @@ pub(super) fn check<'tcx>(
// ptr <-> ptr // ptr <-> ptr
(ReducedTy::Other(from_sub_ty), ReducedTy::Other(to_sub_ty)) (ReducedTy::Other(from_sub_ty), ReducedTy::Other(to_sub_ty))
if matches!(from_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(_)) => && matches!(to_sub_ty.kind(), ty::Ref(..) | ty::RawPtr(_, _)) =>
{ {
from_ty = from_sub_ty; from_ty = from_sub_ty;
to_ty = to_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 { let (from_fat_ptr, to_fat_ptr) = loop {
break match (from_ty.kind(), to_ty.kind()) { break match (from_ty.kind(), to_ty.kind()) {
( (
&(ty::Ref(_, from_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: from_sub_ty, .. })), &(ty::Ref(_, from_sub_ty, _) | ty::RawPtr(from_sub_ty, _)),
&(ty::Ref(_, to_sub_ty, _) | ty::RawPtr(TypeAndMut { ty: to_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; 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; to_ty = to_sub_ty;
continue; 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) => if !unsized_ty.is_sized(cx.tcx, cx.param_env) =>
{ {
(true, false) (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) => if !unsized_ty.is_sized(cx.tcx, cx.param_env) =>
{ {
(false, true) (false, true)

View File

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

View File

@ -8,7 +8,7 @@
/// Returns `true` if it's triggered, otherwise returns `false`. /// 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 { 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()) { 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( span_lint(
cx, cx,
WRONG_TRANSMUTE, 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( ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(
int.try_into().expect("invalid f64 bit representation"), 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, _ => None,
}, },
(_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Str) => { (_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Str) => {

View File

@ -1040,7 +1040,7 @@ fn pat_capture_kind(cx: &LateContext<'_>, pat: &Pat<'_>) -> CaptureKind {
.get(child_id) .get(child_id)
.map_or(&[][..], |x| &**x) .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() *adjust.last().map_or(target, |a| a.target).kind()
{ {
return CaptureKind::Ref(mutability); 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::Array(..)
| rustc_ty::Dynamic(..) | rustc_ty::Dynamic(..)
| rustc_ty::Never | rustc_ty::Never
| rustc_ty::RawPtr(_) | rustc_ty::RawPtr(_, _)
| rustc_ty::Ref(..) | rustc_ty::Ref(..)
| rustc_ty::Slice(_) | rustc_ty::Slice(_)
| rustc_ty::Tuple(_) => format!("<{}>", EarlyBinder::bind(ty).instantiate(tcx, args)), | 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() { match ty.kind() {
ty::Adt(adt, _) => cx.tcx.has_attr(adt.did(), sym::must_use), ty::Adt(adt, _) => cx.tcx.has_attr(adt.did(), sym::must_use),
ty::Foreign(did) => cx.tcx.has_attr(*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 // 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 // because we don't want to lint functions returning empty arrays
is_must_use_ty(cx, *ty) is_must_use_ty(cx, *ty)