Skip to content

Commit e219c4e

Browse files
committed
Add tests involving generics
1 parent 397fc5c commit e219c4e

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

tests/ui/default_box_assignments.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
#![warn(clippy::default_box_assignments)]
22

3+
fn with_default<T: Default>(b: &mut Box<T>) {
4+
*(*b) = T::default();
5+
//~^ default_box_assignments
6+
}
7+
8+
fn with_sized<T>(b: &mut Box<T>, t: T) {
9+
*(*b) = t;
10+
//~^ default_box_assignments
11+
}
12+
13+
fn with_unsized<const N: usize>(b: &mut Box<[u32]>) {
14+
// No lint for assigning to Box<T> where T: !Default
15+
*b = Box::new([42; N]);
16+
}
17+
318
macro_rules! create_default {
419
() => {
520
Default::default()

tests/ui/default_box_assignments.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
#![warn(clippy::default_box_assignments)]
22

3+
fn with_default<T: Default>(b: &mut Box<T>) {
4+
*b = Box::new(T::default());
5+
//~^ default_box_assignments
6+
}
7+
8+
fn with_sized<T>(b: &mut Box<T>, t: T) {
9+
*b = Box::new(t);
10+
//~^ default_box_assignments
11+
}
12+
13+
fn with_unsized<const N: usize>(b: &mut Box<[u32]>) {
14+
// No lint for assigning to Box<T> where T: !Default
15+
*b = Box::new([42; N]);
16+
}
17+
318
macro_rules! create_default {
419
() => {
520
Default::default()
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
1+
error: creating a new box
2+
--> tests/ui/default_box_assignments.rs:4:5
3+
|
4+
LL | *b = Box::new(T::default());
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*(*b) = T::default()`
6+
|
7+
= note: this creates a needless allocation
8+
= note: `-D clippy::default-box-assignments` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::default_box_assignments)]`
10+
11+
error: creating a new box
12+
--> tests/ui/default_box_assignments.rs:9:5
13+
|
14+
LL | *b = Box::new(t);
15+
| ^^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*(*b) = t`
16+
|
17+
= note: this creates a needless allocation
18+
119
error: creating a new box with default content
2-
--> tests/ui/default_box_assignments.rs:11:5
20+
--> tests/ui/default_box_assignments.rs:26:5
321
|
422
LL | b = Default::default();
523
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace existing content with default instead: `*b = Default::default()`
624
|
725
= note: this creates a needless allocation
8-
= note: `-D clippy::default-box-assignments` implied by `-D warnings`
9-
= help: to override `-D warnings` add `#[allow(clippy::default_box_assignments)]`
1026

1127
error: creating a new box with default content
12-
--> tests/ui/default_box_assignments.rs:13:5
28+
--> tests/ui/default_box_assignments.rs:28:5
1329
|
1430
LL | b = Box::default();
1531
| ^^^^^^^^^^^^^^^^^^ help: replace existing content with default instead: `*b = Default::default()`
1632
|
1733
= note: this creates a needless allocation
1834

1935
error: creating a new box
20-
--> tests/ui/default_box_assignments.rs:23:5
36+
--> tests/ui/default_box_assignments.rs:38:5
2137
|
2238
LL | b = Box::new(5);
2339
| ^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*b = 5`
2440
|
2541
= note: this creates a needless allocation
2642

27-
error: aborting due to 3 previous errors
43+
error: aborting due to 5 previous errors
2844

0 commit comments

Comments
 (0)