Commit 91e537b
authored
Don't trigger
Fixes #17202.
`unnecessary_box_returns` fired on return types whose size depends on
generic
parameters, e.g.:
```rust
fn strip<const N: usize, T>(value: Box<[SyncUnsafeCell<T>; N]>) -> Box<[T; N]> { /* ... */ }
// ^^^^^^^^^^^ help: try: `[T; N]`
```
The size of `[T; N]` can't be determined here (it depends on `T` and the
const
parameter `N`), but `approx_ty_size` falls back to `0` for such arrays,
so the
`<= unnecessary_box_size` check always passed and the lint fired.
Returning a
`Box` is often deliberate in these cases to avoid copying a potentially
large
value.
The lint now queries the type's layout directly and only fires when the
size can
actually be computed. Concrete types and generic types with a known
layout (e.g.
`Box<Vec<T>>`) are unaffected.
changelog: [`unnecessary_box_returns`]: no longer fires when the boxed
type's size depends on generic parameters (e.g. `Box<[T; N]>`)unnecessary_box_returns when the size depends on generics (#17249)3 files changed
Lines changed: 34 additions & 4 deletions
File tree
- clippy_lints/src
- tests/ui
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
85 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
86 | 91 | | |
87 | 92 | | |
88 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
74 | 91 | | |
75 | 92 | | |
76 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
36 | 44 | | |
0 commit comments