From: Evan Typanski Date: Thu, 12 May 2022 23:06:23 +0000 (-0400) Subject: Catch other thinning allocations using Ty X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=0def44a1c15e624a6e51206048895021f5b1ee92;hp=631b4ee7a26e4e68469a607376005eb4064b3e6f;p=rust.git Catch other thinning allocations using Ty --- diff --git a/clippy_lints/src/types/redundant_allocation.rs b/clippy_lints/src/types/redundant_allocation.rs index 5fdf1731495..6c231fa2510 100644 --- a/clippy_lints/src/types/redundant_allocation.rs +++ b/clippy_lints/src/types/redundant_allocation.rs @@ -2,9 +2,10 @@ use clippy_utils::source::{snippet, snippet_with_applicability}; use clippy_utils::{path_def_id, qpath_generic_tys}; use rustc_errors::Applicability; -use rustc_hir::{self as hir, def, def_id::DefId, PrimTy, QPath, TyKind}; +use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind}; use rustc_lint::LateContext; use rustc_span::symbol::sym; +use rustc_typeck::hir_ty_to_ty; use super::{utils, REDUNDANT_ALLOCATION}; @@ -54,7 +55,8 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_ }; let inner_span = match qpath_generic_tys(inner_qpath).next() { Some(ty) => { - if alloc_makes_pointer_thin(cx, ty) { + // Reallocation of a fat pointer causes it to become thin + if !hir_ty_to_ty(cx.tcx, ty).is_sized(cx.tcx.at(ty.span), cx.param_env) { return false; } ty.span @@ -109,20 +111,3 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_ } true } - -/// Returns `true` if the allocation would make `hir_ty` go from fat to thin. -fn alloc_makes_pointer_thin(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) -> bool { - match &hir_ty.kind { - TyKind::TraitObject(..) => true, - TyKind::Path(ty_qpath) => { - let ty_res = cx.qpath_res(ty_qpath, hir_ty.hir_id); - if let def::Res::PrimTy(prim_ty) = ty_res { - if matches!(prim_ty, PrimTy::Str) { - return true; - } - } - false - }, - _ => false, - } -} diff --git a/tests/ui/redundant_allocation.rs b/tests/ui/redundant_allocation.rs index b830a3771d5..cf7d8c6e349 100644 --- a/tests/ui/redundant_allocation.rs +++ b/tests/ui/redundant_allocation.rs @@ -98,21 +98,38 @@ pub fn test_rc_box(_: Rc>>) {} } // https://github.com/rust-lang/rust-clippy/issues/8604 -mod box_str { +mod box_fat_ptr { use std::boxed::Box; + use std::path::Path; use std::rc::Rc; use std::sync::Arc; + pub struct DynSized { + foo: [usize], + } + struct S { a: Box>, b: Rc>, c: Arc>, + + e: Box>, + f: Box>, + g: Box>, } - pub fn test_box(_: Box>) {} - pub fn test_rc(_: Rc>) {} - pub fn test_arc(_: Arc>) {} - pub fn test_rc_box(_: Rc>>) {} + pub fn test_box_str(_: Box>) {} + pub fn test_rc_str(_: Rc>) {} + pub fn test_arc_str(_: Arc>) {} + + pub fn test_box_slice(_: Box>) {} + pub fn test_box_path(_: Box>) {} + pub fn test_box_custom(_: Box>) {} + + pub fn test_rc_box_str(_: Rc>>) {} + pub fn test_rc_box_slice(_: Rc>>) {} + pub fn test_rc_box_path(_: Rc>>) {} + pub fn test_rc_box_custom(_: Rc>>) {} } fn main() {} diff --git a/tests/ui/redundant_allocation.stderr b/tests/ui/redundant_allocation.stderr index ae213cb8975..fab1b069fcb 100644 --- a/tests/ui/redundant_allocation.stderr +++ b/tests/ui/redundant_allocation.stderr @@ -144,13 +144,40 @@ LL | pub fn test_rc_box(_: Rc>>) {} = help: consider using just `Rc>` or `Box>` error: usage of `Rc>>` - --> $DIR/redundant_allocation.rs:115:27 + --> $DIR/redundant_allocation.rs:129:31 | -LL | pub fn test_rc_box(_: Rc>>) {} - | ^^^^^^^^^^^^^^^^^ +LL | pub fn test_rc_box_str(_: Rc>>) {} + | ^^^^^^^^^^^^^^^^^ | = note: `Box>` is already on the heap, `Rc>>` makes an extra allocation = help: consider using just `Rc>` or `Box>` -error: aborting due to 17 previous errors +error: usage of `Rc>>` + --> $DIR/redundant_allocation.rs:130:33 + | +LL | pub fn test_rc_box_slice(_: Rc>>) {} + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: `Box>` is already on the heap, `Rc>>` makes an extra allocation + = help: consider using just `Rc>` or `Box>` + +error: usage of `Rc>>` + --> $DIR/redundant_allocation.rs:131:32 + | +LL | pub fn test_rc_box_path(_: Rc>>) {} + | ^^^^^^^^^^^^^^^^^^ + | + = note: `Box>` is already on the heap, `Rc>>` makes an extra allocation + = help: consider using just `Rc>` or `Box>` + +error: usage of `Rc>>` + --> $DIR/redundant_allocation.rs:132:34 + | +LL | pub fn test_rc_box_custom(_: Rc>>) {} + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `Box>` is already on the heap, `Rc>>` makes an extra allocation + = help: consider using just `Rc>` or `Box>` + +error: aborting due to 20 previous errors