Make Ty::boxed_ty return an Option

This commit is contained in:
Pavel Grigorenko 2024-09-05 00:34:04 +03:00
parent 663f20086a
commit 0b8cb4a1eb
5 changed files with 11 additions and 13 deletions

View File

@ -50,7 +50,7 @@ pub fn new(conf: &'static Conf) -> Self {
} }
fn is_non_trait_box(ty: Ty<'_>) -> bool { fn is_non_trait_box(ty: Ty<'_>) -> bool {
ty.is_box() && !ty.boxed_ty().is_trait() ty.boxed_ty().is_some_and(|boxed| !boxed.is_trait())
} }
struct EscapeDelegate<'a, 'tcx> { struct EscapeDelegate<'a, 'tcx> {
@ -191,8 +191,8 @@ fn fake_read(&mut self, _: &PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {}
impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> { impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
fn is_large_box(&self, ty: Ty<'tcx>) -> bool { fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
// Large types need to be boxed to avoid stack overflows. // Large types need to be boxed to avoid stack overflows.
if ty.is_box() { if let Some(boxed_ty) = ty.boxed_ty() {
self.cx.layout_of(ty.boxed_ty()).map_or(0, |l| l.size.bytes()) > self.too_large_for_stack self.cx.layout_of(boxed_ty).map_or(0, |l| l.size.bytes()) > self.too_large_for_stack
} else { } else {
false false
} }

View File

@ -5187,8 +5187,8 @@ fn matches<'a>(self, cx: &LateContext<'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> boo
fn matches_value<'a>(cx: &LateContext<'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool { fn matches_value<'a>(cx: &LateContext<'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool {
if ty == parent_ty { if ty == parent_ty {
true true
} else if ty.is_box() { } else if let Some(boxed_ty) = ty.boxed_ty() {
ty.boxed_ty() == parent_ty boxed_ty == parent_ty
} else if is_type_diagnostic_item(cx, ty, sym::Rc) || is_type_diagnostic_item(cx, ty, sym::Arc) { } else if is_type_diagnostic_item(cx, ty, sym::Rc) || is_type_diagnostic_item(cx, ty, sym::Arc) {
if let ty::Adt(_, args) = ty.kind() { if let ty::Adt(_, args) = ty.kind() {
args.types().next().map_or(false, |t| t == parent_ty) args.types().next().map_or(false, |t| t == parent_ty)

View File

@ -16,7 +16,7 @@ pub(super) fn derefs_to_slice<'tcx>(
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool { fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
match ty.kind() { match ty.kind() {
ty::Slice(_) => true, ty::Slice(_) => true,
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()), ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => may_slice(cx, boxed),
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec), ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec),
ty::Array(_, size) => size.try_eval_target_usize(cx.tcx, cx.param_env).is_some(), ty::Array(_, size) => size.try_eval_target_usize(cx.tcx, cx.param_env).is_some(),
ty::Ref(_, inner, _) => may_slice(cx, *inner), ty::Ref(_, inner, _) => may_slice(cx, *inner),
@ -33,7 +33,7 @@ fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
} else { } else {
match ty.kind() { match ty.kind() {
ty::Slice(_) => Some(expr), ty::Slice(_) => Some(expr),
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr), _ if ty.boxed_ty().is_some_and(|boxed| may_slice(cx, boxed)) => Some(expr),
ty::Ref(_, inner, _) => { ty::Ref(_, inner, _) => {
if may_slice(cx, *inner) { if may_slice(cx, *inner) {
Some(expr) Some(expr)

View File

@ -75,11 +75,9 @@ fn check_fn_item(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, def_id: Loc
.instantiate_bound_regions_with_erased(cx.tcx.fn_sig(def_id).skip_binder()) .instantiate_bound_regions_with_erased(cx.tcx.fn_sig(def_id).skip_binder())
.output(); .output();
if !return_ty.is_box() { let Some(boxed_ty) = return_ty.boxed_ty() else {
return; return;
} };
let boxed_ty = return_ty.boxed_ty();
// It's sometimes useful to return Box<T> if T is unsized, so don't lint those. // It's sometimes useful to return Box<T> if T is unsized, so don't lint those.
// Also, don't lint if we know that T is very large, in which case returning // Also, don't lint if we know that T is very large, in which case returning

View File

@ -704,8 +704,8 @@ pub fn expr_sig<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<ExprFnS
/// If the type is function like, get the signature for it. /// If the type is function like, get the signature for it.
pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'tcx>> { pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'tcx>> {
if ty.is_box() { if let Some(boxed_ty) = ty.boxed_ty() {
return ty_sig(cx, ty.boxed_ty()); return ty_sig(cx, boxed_ty);
} }
match *ty.kind() { match *ty.kind() {
ty::Closure(id, subs) => { ty::Closure(id, subs) => {