Skip to content

Commit c4dbb9e

Browse files
committed
Auto merge of #122852 - compiler-errors:raw-ptr, r=lcnr
Remove `TypeAndMut` from `ty::RawPtr` variant, make it take `Ty` and `Mutability` Pretty much mechanically converting `ty::RawPtr(ty::TypeAndMut { ty, mutbl })` to `ty::RawPtr(ty, mutbl)` and its fallout. r? lcnr cc rust-lang/types-team#124
2 parents 4525ce3 + c051eee commit c4dbb9e

29 files changed

+70
-99
lines changed

clippy_lints/src/casts/as_ptr_cast_mut.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ use rustc_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::LateContext;
66
use rustc_middle::mir::Mutability;
7-
use rustc_middle::ty::{self, Ty, TypeAndMut};
7+
use rustc_middle::ty::{self, Ty};
88

99
use super::AS_PTR_CAST_MUT;
1010

1111
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>) {
12-
if let ty::RawPtr(TypeAndMut {
13-
mutbl: Mutability::Mut,
14-
ty: ptrty,
15-
}) = cast_to.kind()
16-
&& let ty::RawPtr(TypeAndMut {
17-
mutbl: Mutability::Not, ..
18-
}) = cx.typeck_results().node_type(cast_expr.hir_id).kind()
12+
if let ty::RawPtr(ptrty, Mutability::Mut) = cast_to.kind()
13+
&& let ty::RawPtr(_, Mutability::Not) = cx.typeck_results().node_type(cast_expr.hir_id).kind()
1914
&& let ExprKind::MethodCall(method_name, receiver, [], _) = cast_expr.peel_blocks().kind
2015
&& method_name.ident.name == rustc_span::sym::as_ptr
2116
&& let Some(as_ptr_did) = cx

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
3333
}
3434

3535
fn lint_cast_ptr_alignment<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, cast_from: Ty<'tcx>, cast_to: Ty<'tcx>) {
36-
if let ty::RawPtr(from_ptr_ty) = &cast_from.kind()
37-
&& let ty::RawPtr(to_ptr_ty) = &cast_to.kind()
38-
&& let Ok(from_layout) = cx.layout_of(from_ptr_ty.ty)
39-
&& let Ok(to_layout) = cx.layout_of(to_ptr_ty.ty)
36+
if let ty::RawPtr(from_ptr_ty, _) = *cast_from.kind()
37+
&& let ty::RawPtr(to_ptr_ty, _) = *cast_to.kind()
38+
&& let Ok(from_layout) = cx.layout_of(from_ptr_ty)
39+
&& let Ok(to_layout) = cx.layout_of(to_ptr_ty)
4040
&& from_layout.align.abi < to_layout.align.abi
4141
// with c_void, we inherently need to trust the user
42-
&& !is_c_void(cx, from_ptr_ty.ty)
42+
&& !is_c_void(cx, from_ptr_ty)
4343
// when casting from a ZST, we don't know enough to properly lint
4444
&& !from_layout.is_zst()
4545
&& !is_used_as_unaligned(cx, expr)

clippy_lints/src/casts/cast_slice_different_sizes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn is_child_of_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
8787
/// the type is one of those slices
8888
fn get_raw_slice_ty_mut(ty: Ty<'_>) -> Option<TypeAndMut<'_>> {
8989
match ty.kind() {
90-
ty::RawPtr(TypeAndMut { ty: slice_ty, mutbl }) => match slice_ty.kind() {
90+
ty::RawPtr(slice_ty, mutbl) => match slice_ty.kind() {
9191
ty::Slice(ty) => Some(TypeAndMut { ty: *ty, mutbl: *mutbl }),
9292
_ => None,
9393
},

clippy_lints/src/casts/cast_slice_from_raw_parts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ fn raw_parts_kind(cx: &LateContext<'_>, did: DefId) -> Option<RawPartsKind> {
2525

2626
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>, msrv: &Msrv) {
2727
if msrv.meets(msrvs::PTR_SLICE_RAW_PARTS)
28-
&& let ty::RawPtr(ptrty) = cast_to.kind()
29-
&& let ty::Slice(_) = ptrty.ty.kind()
28+
&& let ty::RawPtr(ptrty, _) = cast_to.kind()
29+
&& let ty::Slice(_) = ptrty.kind()
3030
&& let ExprKind::Call(fun, [ptr_arg, len_arg]) = cast_expr.peel_blocks().kind
3131
&& let ExprKind::Path(ref qpath) = fun.kind
3232
&& let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id()

clippy_lints/src/casts/ptr_as_ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, Mutability, QPath, TyKind};
77
use rustc_hir_pretty::qpath_to_string;
88
use rustc_lint::LateContext;
9-
use rustc_middle::ty::{self, TypeAndMut};
9+
use rustc_middle::ty;
1010
use rustc_span::sym;
1111

1212
use super::PTR_AS_PTR;
@@ -33,8 +33,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) {
3333

3434
if let ExprKind::Cast(cast_expr, cast_to_hir_ty) = expr.kind
3535
&& let (cast_from, cast_to) = (cx.typeck_results().expr_ty(cast_expr), cx.typeck_results().expr_ty(expr))
36-
&& let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, .. }) = cast_from.kind()
37-
&& let ty::RawPtr(TypeAndMut { ty: to_pointee_ty, mutbl: to_mutbl }) = cast_to.kind()
36+
&& let ty::RawPtr(_, from_mutbl) = cast_from.kind()
37+
&& let ty::RawPtr(to_pointee_ty, to_mutbl) = cast_to.kind()
3838
&& matches!((from_mutbl, to_mutbl),
3939
(Mutability::Not, Mutability::Not) | (Mutability::Mut, Mutability::Mut))
4040
// The `U` in `pointer::cast` have to be `Sized`

clippy_lints/src/casts/ptr_cast_constness.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::sugg::Sugg;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, Mutability};
66
use rustc_lint::LateContext;
7-
use rustc_middle::ty::{self, Ty, TypeAndMut};
7+
use rustc_middle::ty::{self, Ty};
88

99
use super::PTR_CAST_CONSTNESS;
1010

@@ -17,14 +17,8 @@ pub(super) fn check<'tcx>(
1717
msrv: &Msrv,
1818
) {
1919
if msrv.meets(msrvs::POINTER_CAST_CONSTNESS)
20-
&& let ty::RawPtr(TypeAndMut {
21-
mutbl: from_mutbl,
22-
ty: from_ty,
23-
}) = cast_from.kind()
24-
&& let ty::RawPtr(TypeAndMut {
25-
mutbl: to_mutbl,
26-
ty: to_ty,
27-
}) = cast_to.kind()
20+
&& let ty::RawPtr(from_ty, from_mutbl) = cast_from.kind()
21+
&& let ty::RawPtr(to_ty, to_mutbl) = cast_to.kind()
2822
&& matches!(
2923
(from_mutbl, to_mutbl),
3024
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not)

clippy_lints/src/casts/ref_as_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::{expr_use_ctxt, is_no_std_crate, ExprUseNode};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, Mutability, Ty, TyKind};
77
use rustc_lint::LateContext;
8-
use rustc_middle::ty::{self, TypeAndMut};
8+
use rustc_middle::ty;
99

1010
use super::REF_AS_PTR;
1111

@@ -21,7 +21,7 @@ pub(super) fn check<'tcx>(
2121
);
2222

2323
if matches!(cast_from.kind(), ty::Ref(..))
24-
&& let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, .. }) = cast_to.kind()
24+
&& let ty::RawPtr(_, to_mutbl) = cast_to.kind()
2525
&& let Some(use_cx) = expr_use_ctxt(cx, expr)
2626
// TODO: only block the lint if `cast_expr` is a temporary
2727
&& !matches!(use_cx.node, ExprUseNode::Local(_) | ExprUseNode::ConstStatic(_))

clippy_lints/src/from_raw_with_void_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::ty::is_c_void;
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{Expr, ExprKind, QPath};
66
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_middle::ty::{RawPtr, TypeAndMut};
7+
use rustc_middle::ty::{RawPtr};
88
use rustc_session::declare_lint_pass;
99
use rustc_span::sym;
1010

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

clippy_lints/src/functions/must_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, tys: &mut DefIdSet)
207207
},
208208
ty::Tuple(args) => args.iter().any(|ty| is_mutable_ty(cx, ty, tys)),
209209
ty::Array(ty, _) | ty::Slice(ty) => is_mutable_ty(cx, ty, tys),
210-
ty::RawPtr(ty::TypeAndMut { ty, mutbl }) | ty::Ref(_, ty, mutbl) => {
210+
ty::RawPtr(ty, mutbl) | ty::Ref(_, ty, mutbl) => {
211211
mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, tys)
212212
},
213213
// calling something constitutes a side effect, so return true on all callables

clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn check_raw_ptr<'tcx>(
7575
}
7676

7777
fn raw_ptr_arg(cx: &LateContext<'_>, arg: &hir::Param<'_>) -> Option<hir::HirId> {
78-
if let (&hir::PatKind::Binding(_, id, _, _), Some(&ty::RawPtr(_))) = (
78+
if let (&hir::PatKind::Binding(_, id, _, _), Some(&ty::RawPtr(_, _))) = (
7979
&arg.pat.kind,
8080
cx.maybe_typeck_results()
8181
.map(|typeck_results| typeck_results.pat_ty(arg.pat).kind()),

0 commit comments

Comments
 (0)