Auto merge of #8813 - evantypanski:redundant_alloc_fat_ptr, r=Alexendoo

Fix redundant_allocation warning for Rc<Box<str>>

changelog: [`redundant_allocation`] Fixes #8604

Fixes false positives where a fat pointer with `str` type was made thin by another allocation, but that thinning allocation was marked as redundant
This commit is contained in:
bors 2022-05-13 15:02:58 +00:00
commit 2038084cf2
3 changed files with 77 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_lint::LateContext;
use rustc_span::symbol::sym;
use rustc_typeck::hir_ty_to_ty;
use super::{utils, REDUNDANT_ALLOCATION};
@ -54,8 +55,10 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
};
let inner_span = match qpath_generic_tys(inner_qpath).next() {
Some(ty) => {
// Box<Box<dyn T>> is smaller than Box<dyn T> because of wide pointers
if matches!(ty.kind, TyKind::TraitObject(..)) {
// 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) {
return false;
}
ty.span

View File

@ -97,4 +97,39 @@ mod box_dyn {
pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
}
// https://github.com/rust-lang/rust-clippy/issues/8604
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<Box<str>>,
b: Rc<Box<str>>,
c: Arc<Box<str>>,
e: Box<Box<[usize]>>,
f: Box<Box<Path>>,
g: Box<Box<DynSized>>,
}
pub fn test_box_str(_: Box<Box<str>>) {}
pub fn test_rc_str(_: Rc<Box<str>>) {}
pub fn test_arc_str(_: Arc<Box<str>>) {}
pub fn test_box_slice(_: Box<Box<[usize]>>) {}
pub fn test_box_path(_: Box<Box<Path>>) {}
pub fn test_box_custom(_: Box<Box<DynSized>>) {}
pub fn test_rc_box_str(_: Rc<Box<Box<str>>>) {}
pub fn test_rc_box_slice(_: Rc<Box<Box<[usize]>>>) {}
pub fn test_rc_box_path(_: Rc<Box<Box<Path>>>) {}
pub fn test_rc_box_custom(_: Rc<Box<Box<DynSized>>>) {}
}
fn main() {}

View File

@ -143,5 +143,41 @@ LL | pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
= note: `Box<Box<dyn T>>` is already on the heap, `Rc<Box<Box<dyn T>>>` makes an extra allocation
= help: consider using just `Rc<Box<dyn T>>` or `Box<Box<dyn T>>`
error: aborting due to 16 previous errors
error: usage of `Rc<Box<Box<str>>>`
--> $DIR/redundant_allocation.rs:129:31
|
LL | pub fn test_rc_box_str(_: Rc<Box<Box<str>>>) {}
| ^^^^^^^^^^^^^^^^^
|
= note: `Box<Box<str>>` is already on the heap, `Rc<Box<Box<str>>>` makes an extra allocation
= help: consider using just `Rc<Box<str>>` or `Box<Box<str>>`
error: usage of `Rc<Box<Box<[usize]>>>`
--> $DIR/redundant_allocation.rs:130:33
|
LL | pub fn test_rc_box_slice(_: Rc<Box<Box<[usize]>>>) {}
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: `Box<Box<[usize]>>` is already on the heap, `Rc<Box<Box<[usize]>>>` makes an extra allocation
= help: consider using just `Rc<Box<[usize]>>` or `Box<Box<[usize]>>`
error: usage of `Rc<Box<Box<Path>>>`
--> $DIR/redundant_allocation.rs:131:32
|
LL | pub fn test_rc_box_path(_: Rc<Box<Box<Path>>>) {}
| ^^^^^^^^^^^^^^^^^^
|
= note: `Box<Box<Path>>` is already on the heap, `Rc<Box<Box<Path>>>` makes an extra allocation
= help: consider using just `Rc<Box<Path>>` or `Box<Box<Path>>`
error: usage of `Rc<Box<Box<DynSized>>>`
--> $DIR/redundant_allocation.rs:132:34
|
LL | pub fn test_rc_box_custom(_: Rc<Box<Box<DynSized>>>) {}
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `Box<Box<DynSized>>` is already on the heap, `Rc<Box<Box<DynSized>>>` makes an extra allocation
= help: consider using just `Rc<Box<DynSized>>` or `Box<Box<DynSized>>`
error: aborting due to 20 previous errors