]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/vec_box_sized.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / vec_box_sized.rs
CommitLineData
4b012472
FG
1//@no-rustfix
2
f20569fa 3#![allow(dead_code)]
4b012472
FG
4#![feature(allocator_api)]
5
6use std::alloc::{AllocError, Allocator, Layout};
7use std::ptr::NonNull;
f20569fa
XL
8
9struct SizedStruct(i32);
10struct UnsizedStruct([i32]);
11struct BigStruct([i32; 10000]);
12
4b012472
FG
13struct DummyAllocator;
14unsafe impl Allocator for DummyAllocator {
15 fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
16 todo!()
17 }
18 unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
19 todo!()
20 }
21}
22
f20569fa
XL
23/// The following should trigger the lint
24mod should_trigger {
4b012472 25 use super::{DummyAllocator, SizedStruct};
cdc7bbd5
XL
26 const C: Vec<Box<i32>> = Vec::new();
27 static S: Vec<Box<i32>> = Vec::new();
f20569fa
XL
28
29 struct StructWithVecBox {
30 sized_type: Vec<Box<SizedStruct>>,
31 }
32
33 struct A(Vec<Box<SizedStruct>>);
34 struct B(Vec<Vec<Box<(u32)>>>);
4b012472
FG
35
36 fn allocator_global_defined_vec() -> Vec<Box<i32>, std::alloc::Global> {
37 Vec::new()
38 }
39 fn allocator_global_defined_box() -> Vec<Box<i32, std::alloc::Global>> {
40 Vec::new()
41 }
42 fn allocator_match() -> Vec<Box<i32, DummyAllocator>, DummyAllocator> {
43 Vec::new_in(DummyAllocator)
44 }
f20569fa
XL
45}
46
47/// The following should not trigger the lint
48mod should_not_trigger {
4b012472 49 use super::{BigStruct, DummyAllocator, UnsizedStruct};
f20569fa
XL
50
51 struct C(Vec<Box<UnsizedStruct>>);
52 struct D(Vec<Box<BigStruct>>);
53
54 struct StructWithVecBoxButItsUnsized {
55 unsized_type: Vec<Box<UnsizedStruct>>,
56 }
57
58 struct TraitVec<T: ?Sized> {
59 // Regression test for #3720. This was causing an ICE.
60 inner: Vec<Box<T>>,
61 }
4b012472
FG
62
63 fn allocator_mismatch() -> Vec<Box<i32, DummyAllocator>> {
64 Vec::new()
65 }
66 fn allocator_mismatch_2() -> Vec<Box<i32>, DummyAllocator> {
67 Vec::new_in(DummyAllocator)
68 }
f20569fa
XL
69}
70
71mod inner_mod {
72 mod inner {
73 pub struct S;
74 }
75
76 mod inner2 {
77 use super::inner::S;
78
79 pub fn f() -> Vec<Box<S>> {
80 vec![]
81 }
82 }
83}
84
781aab86
FG
85// https://github.com/rust-lang/rust-clippy/issues/11417
86fn in_closure() {
87 let _ = |_: Vec<Box<dyn ToString>>| {};
88}
89
f20569fa 90fn main() {}