Skip to content

Commit 7932560

Browse files
committed
update testcases, cleanup
1 parent 3a91a11 commit 7932560

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

clippy_lints/src/types/vec_box.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub(super) fn check(
3232
&& Some(def_id) == cx.tcx.lang_items().owned_box()
3333
// At this point, we know ty is Box<T>, now get T
3434
&& let Some(last) = last_path_segment(ty_qpath).args
35-
// extract allocator from thr Box for later
3635
&& let Some(GenericArg::Type(boxed_ty)) = last.args.first()
36+
// extract allocator from the Box for later
3737
&& let boxed_alloc_ty = last.args.get(1)
3838
&& let ty_ty = hir_ty_to_ty(cx.tcx, boxed_ty)
3939
&& !ty_ty.has_escaping_bound_vars()

tests/ui/vec_box_sized.fixed

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(dead_code)]
2+
#![feature(allocator_api)]
23

34
struct SizedStruct(i32);
45
struct UnsizedStruct([i32]);
@@ -21,6 +22,8 @@ mod should_trigger {
2122
/// The following should not trigger the lint
2223
mod should_not_trigger {
2324
use super::{BigStruct, UnsizedStruct};
25+
use std::alloc::{Layout, AllocError, Allocator};
26+
use std::ptr::NonNull;
2427

2528
struct C(Vec<Box<UnsizedStruct>>);
2629
struct D(Vec<Box<BigStruct>>);
@@ -33,6 +36,20 @@ mod should_not_trigger {
3336
// Regression test for #3720. This was causing an ICE.
3437
inner: Vec<Box<T>>,
3538
}
39+
40+
struct DummyAllocator;
41+
unsafe impl Allocator for DummyAllocator {
42+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
43+
todo!()
44+
}
45+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
46+
todo!()
47+
}
48+
}
49+
50+
fn allocator_mismatch() -> Vec<Box<i32, DummyAllocator>> {
51+
vec![]
52+
}
3653
}
3754

3855
mod inner_mod {

tests/ui/vec_box_sized.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(dead_code)]
2+
#![feature(allocator_api)]
23

34
struct SizedStruct(i32);
45
struct UnsizedStruct([i32]);
@@ -21,6 +22,8 @@ mod should_trigger {
2122
/// The following should not trigger the lint
2223
mod should_not_trigger {
2324
use super::{BigStruct, UnsizedStruct};
25+
use std::alloc::{Layout, AllocError, Allocator};
26+
use std::ptr::NonNull;
2427

2528
struct C(Vec<Box<UnsizedStruct>>);
2629
struct D(Vec<Box<BigStruct>>);
@@ -33,6 +36,20 @@ mod should_not_trigger {
3336
// Regression test for #3720. This was causing an ICE.
3437
inner: Vec<Box<T>>,
3538
}
39+
40+
struct DummyAllocator;
41+
unsafe impl Allocator for DummyAllocator {
42+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
43+
todo!()
44+
}
45+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
46+
todo!()
47+
}
48+
}
49+
50+
fn allocator_mismatch() -> Vec<Box<i32, DummyAllocator>> {
51+
vec![]
52+
}
3653
}
3754

3855
mod inner_mod {

tests/ui/vec_box_sized.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `Vec<T>` is already on the heap, the boxing is unnecessary
2-
--> $DIR/vec_box_sized.rs:10:14
2+
--> $DIR/vec_box_sized.rs:11:14
33
|
44
LL | const C: Vec<Box<i32>> = Vec::new();
55
| ^^^^^^^^^^^^^ help: try: `Vec<i32>`
@@ -8,31 +8,31 @@ LL | const C: Vec<Box<i32>> = Vec::new();
88
= help: to override `-D warnings` add `#[allow(clippy::vec_box)]`
99

1010
error: `Vec<T>` is already on the heap, the boxing is unnecessary
11-
--> $DIR/vec_box_sized.rs:11:15
11+
--> $DIR/vec_box_sized.rs:12:15
1212
|
1313
LL | static S: Vec<Box<i32>> = Vec::new();
1414
| ^^^^^^^^^^^^^ help: try: `Vec<i32>`
1515

1616
error: `Vec<T>` is already on the heap, the boxing is unnecessary
17-
--> $DIR/vec_box_sized.rs:14:21
17+
--> $DIR/vec_box_sized.rs:15:21
1818
|
1919
LL | sized_type: Vec<Box<SizedStruct>>,
2020
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
2121

2222
error: `Vec<T>` is already on the heap, the boxing is unnecessary
23-
--> $DIR/vec_box_sized.rs:17:14
23+
--> $DIR/vec_box_sized.rs:18:14
2424
|
2525
LL | struct A(Vec<Box<SizedStruct>>);
2626
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
2727

2828
error: `Vec<T>` is already on the heap, the boxing is unnecessary
29-
--> $DIR/vec_box_sized.rs:18:18
29+
--> $DIR/vec_box_sized.rs:19:18
3030
|
3131
LL | struct B(Vec<Vec<Box<(u32)>>>);
3232
| ^^^^^^^^^^^^^^^ help: try: `Vec<u32>`
3333

3434
error: `Vec<T>` is already on the heap, the boxing is unnecessary
35-
--> $DIR/vec_box_sized.rs:46:23
35+
--> $DIR/vec_box_sized.rs:63:23
3636
|
3737
LL | pub fn f() -> Vec<Box<S>> {
3838
| ^^^^^^^^^^^ help: try: `Vec<S>`

0 commit comments

Comments
 (0)