Skip to content

Commit 6b54194

Browse files
committed
Auto merge of rust-lang#5081 - Areredify:vec_box_threshold, r=flip1995
add size parameter for `vec_box` lint changelog: add size threshold for the `vec_box` lint, currently 4096 bytes (one page) (subject to change). Closes rust-lang#3547. diff is a little bit confusing due to some refactoring (moving free functions to lint struct functions), relevant portion is [this](https://github.com/rust-lang/rust-clippy/compare/master...Areredify:vec_box_threshold?expand=1#diff-1096120ca9143af89dcc9175ea92b54aR294-R298). In hindsight should've been different commits, but oh well.
2 parents 50f23ea + 2b7bc26 commit 6b54194

File tree

10 files changed

+251
-188
lines changed

10 files changed

+251
-188
lines changed

clippy_lints/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
819819
store.register_late_pass(|| box utils::internal_lints::OuterExpnDataPass);
820820
store.register_late_pass(|| box utils::inspector::DeepCodeInspector);
821821
store.register_late_pass(|| box utils::author::Author);
822-
store.register_late_pass(|| box types::Types);
822+
let vec_box_size_threshold = conf.vec_box_size_threshold;
823+
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold));
823824
store.register_late_pass(|| box booleans::NonminimalBool);
824825
store.register_late_pass(|| box eq_op::EqOp);
825826
store.register_late_pass(|| box enum_glob_use::EnumGlobUse);

clippy_lints/src/types.rs

+199-181
Large diffs are not rendered by default.

clippy_lints/src/utils/conf.rs

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ define_Conf! {
152152
(too_many_lines_threshold, "too_many_lines_threshold", 100 => u64),
153153
/// Lint: LARGE_STACK_ARRAYS. The maximum allowed size for arrays on the stack
154154
(array_size_threshold, "array_size_threshold", 512_000 => u64),
155+
/// Lint: VEC_BOX. The size of the boxed type in bytes, where boxing in a `Vec` is allowed
156+
(vec_box_size_threshold, "vec_box_size_threshold", 4096 => u64),
155157
}
156158

157159
impl Default for Conf {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `third-party` at line 5 column 1
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `third-party` at line 5 column 1
22

33
error: aborting due to previous error
44

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vec-box-size-threshold = 4

tests/ui-toml/vec_box_sized/test.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct S {
2+
x: u64,
3+
}
4+
5+
struct C {
6+
y: u16,
7+
}
8+
9+
struct Foo(Vec<Box<u8>>);
10+
struct Bar(Vec<Box<u32>>);
11+
struct Baz(Vec<Box<(u32, u32)>>);
12+
struct BarBaz(Vec<Box<S>>);
13+
struct FooBarBaz(Vec<Box<C>>);
14+
15+
fn main() {}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: `Vec<T>` is already on the heap, the boxing is unnecessary.
2+
--> $DIR/test.rs:9:12
3+
|
4+
LL | struct Foo(Vec<Box<u8>>);
5+
| ^^^^^^^^^^^^ help: try: `Vec<u8>`
6+
|
7+
= note: `-D clippy::vec-box` implied by `-D warnings`
8+
9+
error: `Vec<T>` is already on the heap, the boxing is unnecessary.
10+
--> $DIR/test.rs:10:12
11+
|
12+
LL | struct Bar(Vec<Box<u32>>);
13+
| ^^^^^^^^^^^^^ help: try: `Vec<u32>`
14+
15+
error: `Vec<T>` is already on the heap, the boxing is unnecessary.
16+
--> $DIR/test.rs:13:18
17+
|
18+
LL | struct FooBarBaz(Vec<Box<C>>);
19+
| ^^^^^^^^^^^ help: try: `Vec<C>`
20+
21+
error: aborting due to 3 previous errors
22+

tests/ui/vec_box_sized.fixed

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
struct SizedStruct(i32);
66
struct UnsizedStruct([i32]);
7+
struct BigStruct([i32; 10000]);
78

89
/// The following should trigger the lint
910
mod should_trigger {
@@ -19,9 +20,10 @@ mod should_trigger {
1920

2021
/// The following should not trigger the lint
2122
mod should_not_trigger {
22-
use super::UnsizedStruct;
23+
use super::{BigStruct, UnsizedStruct};
2324

2425
struct C(Vec<Box<UnsizedStruct>>);
26+
struct D(Vec<Box<BigStruct>>);
2527

2628
struct StructWithVecBoxButItsUnsized {
2729
unsized_type: Vec<Box<UnsizedStruct>>,

tests/ui/vec_box_sized.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
struct SizedStruct(i32);
66
struct UnsizedStruct([i32]);
7+
struct BigStruct([i32; 10000]);
78

89
/// The following should trigger the lint
910
mod should_trigger {
@@ -19,9 +20,10 @@ mod should_trigger {
1920

2021
/// The following should not trigger the lint
2122
mod should_not_trigger {
22-
use super::UnsizedStruct;
23+
use super::{BigStruct, UnsizedStruct};
2324

2425
struct C(Vec<Box<UnsizedStruct>>);
26+
struct D(Vec<Box<BigStruct>>);
2527

2628
struct StructWithVecBoxButItsUnsized {
2729
unsized_type: Vec<Box<UnsizedStruct>>,

tests/ui/vec_box_sized.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: `Vec<T>` is already on the heap, the boxing is unnecessary.
2-
--> $DIR/vec_box_sized.rs:13:21
2+
--> $DIR/vec_box_sized.rs:14:21
33
|
44
LL | sized_type: Vec<Box<SizedStruct>>,
55
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
66
|
77
= note: `-D clippy::vec-box` implied by `-D warnings`
88

99
error: `Vec<T>` is already on the heap, the boxing is unnecessary.
10-
--> $DIR/vec_box_sized.rs:16:14
10+
--> $DIR/vec_box_sized.rs:17:14
1111
|
1212
LL | struct A(Vec<Box<SizedStruct>>);
1313
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
1414

1515
error: `Vec<T>` is already on the heap, the boxing is unnecessary.
16-
--> $DIR/vec_box_sized.rs:17:18
16+
--> $DIR/vec_box_sized.rs:18:18
1717
|
1818
LL | struct B(Vec<Vec<Box<(u32)>>>);
1919
| ^^^^^^^^^^^^^^^ help: try: `Vec<u32>`

0 commit comments

Comments
 (0)