Fix ICE in `redundant_allocation`

This commit is contained in:
Alex Macleod 2022-11-02 13:24:34 +00:00
parent 7600535511
commit 517605e1fd
2 changed files with 20 additions and 3 deletions

View File

@ -5,6 +5,7 @@ use rustc_errors::Applicability;
use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind};
use rustc_hir_analysis::hir_ty_to_ty;
use rustc_lint::LateContext;
use rustc_middle::ty::TypeVisitable;
use rustc_span::symbol::sym;
use super::{utils, REDUNDANT_ALLOCATION};
@ -51,14 +52,15 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
return false
};
let inner_span = match qpath_generic_tys(inner_qpath).next() {
Some(ty) => {
Some(hir_ty) => {
// Reallocation of a fat pointer causes it to become thin. `hir_ty_to_ty` is safe to use
// here because `mod.rs` guarantees this lint is only run on types outside of bodies and
// is not run on locals.
if !hir_ty_to_ty(cx.tcx, ty).is_sized(cx.tcx.at(ty.span), cx.param_env) {
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
if ty.has_escaping_bound_vars() || !ty.is_sized(cx.tcx.at(hir_ty.span), cx.param_env) {
return false;
}
ty.span
hir_ty.span
},
None => return false,
};

View File

@ -0,0 +1,15 @@
//! <https://github.com/rust-lang/rust-clippy/issues/9746#issuecomment-1297132880>
trait Trait {}
struct Struct<'a> {
_inner: &'a Struct<'a>,
}
impl Trait for Struct<'_> {}
fn example<'a>(s: &'a Struct) -> Box<Box<dyn Trait + 'a>> {
Box::new(Box::new(Struct { _inner: s }))
}
fn main() {}