Skip to content

Commit 0b8cb4a

Browse files
committed
Make Ty::boxed_ty return an Option
1 parent 663f200 commit 0b8cb4a

File tree

5 files changed

+11
-13
lines changed

5 files changed

+11
-13
lines changed

clippy_lints/src/escape.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ declare_clippy_lint! {
5050
}
5151

5252
fn is_non_trait_box(ty: Ty<'_>) -> bool {
53-
ty.is_box() && !ty.boxed_ty().is_trait()
53+
ty.boxed_ty().is_some_and(|boxed| !boxed.is_trait())
5454
}
5555

5656
struct EscapeDelegate<'a, 'tcx> {
@@ -191,8 +191,8 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
191191
impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
192192
fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
193193
// Large types need to be boxed to avoid stack overflows.
194-
if ty.is_box() {
195-
self.cx.layout_of(ty.boxed_ty()).map_or(0, |l| l.size.bytes()) > self.too_large_for_stack
194+
if let Some(boxed_ty) = ty.boxed_ty() {
195+
self.cx.layout_of(boxed_ty).map_or(0, |l| l.size.bytes()) > self.too_large_for_stack
196196
} else {
197197
false
198198
}

clippy_lints/src/methods/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5187,8 +5187,8 @@ impl SelfKind {
51875187
fn matches_value<'a>(cx: &LateContext<'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool {
51885188
if ty == parent_ty {
51895189
true
5190-
} else if ty.is_box() {
5191-
ty.boxed_ty() == parent_ty
5190+
} else if let Some(boxed_ty) = ty.boxed_ty() {
5191+
boxed_ty == parent_ty
51925192
} else if is_type_diagnostic_item(cx, ty, sym::Rc) || is_type_diagnostic_item(cx, ty, sym::Arc) {
51935193
if let ty::Adt(_, args) = ty.kind() {
51945194
args.types().next().map_or(false, |t| t == parent_ty)

clippy_lints/src/methods/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(super) fn derefs_to_slice<'tcx>(
1616
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
1717
match ty.kind() {
1818
ty::Slice(_) => true,
19-
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
19+
ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => may_slice(cx, boxed),
2020
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec),
2121
ty::Array(_, size) => size.try_eval_target_usize(cx.tcx, cx.param_env).is_some(),
2222
ty::Ref(_, inner, _) => may_slice(cx, *inner),
@@ -33,7 +33,7 @@ pub(super) fn derefs_to_slice<'tcx>(
3333
} else {
3434
match ty.kind() {
3535
ty::Slice(_) => Some(expr),
36-
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
36+
_ if ty.boxed_ty().is_some_and(|boxed| may_slice(cx, boxed)) => Some(expr),
3737
ty::Ref(_, inner, _) => {
3838
if may_slice(cx, *inner) {
3939
Some(expr)

clippy_lints/src/unnecessary_box_returns.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ impl UnnecessaryBoxReturns {
7575
.instantiate_bound_regions_with_erased(cx.tcx.fn_sig(def_id).skip_binder())
7676
.output();
7777

78-
if !return_ty.is_box() {
78+
let Some(boxed_ty) = return_ty.boxed_ty() else {
7979
return;
80-
}
81-
82-
let boxed_ty = return_ty.boxed_ty();
80+
};
8381

8482
// It's sometimes useful to return Box<T> if T is unsized, so don't lint those.
8583
// Also, don't lint if we know that T is very large, in which case returning

clippy_utils/src/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ pub fn expr_sig<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<ExprFnS
704704

705705
/// If the type is function like, get the signature for it.
706706
pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'tcx>> {
707-
if ty.is_box() {
708-
return ty_sig(cx, ty.boxed_ty());
707+
if let Some(boxed_ty) = ty.boxed_ty() {
708+
return ty_sig(cx, boxed_ty);
709709
}
710710
match *ty.kind() {
711711
ty::Closure(id, subs) => {

0 commit comments

Comments
 (0)