Skip to content

Commit 0f5a38f

Browse files
committed
Auto merge of #9170 - Rqnsom:box_collection, r=Jarcho
[`box_collection`]: raise warn for all std collections So far, only [`Vec`, `String`, `HashMap`] were considered. Extend collection checklist for this lint with: - `HashSet` - `VecDeque` - `LinkedList` - `BTreeMap` - `BTreeSet` - `BinaryHeap` changelog: [`box_collection`]: raise warn for all std collections
2 parents 10e85ef + 467e1b2 commit 0f5a38f

File tree

3 files changed

+84
-15
lines changed

3 files changed

+84
-15
lines changed

clippy_lints/src/types/box_collection.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
1515
sym::String => "",
1616
_ => "<..>",
1717
};
18+
19+
let box_content = format!("{outer}{generic}", outer = item_type);
1820
span_lint_and_help(
1921
cx,
2022
BOX_COLLECTION,
2123
hir_ty.span,
2224
&format!(
23-
"you seem to be trying to use `Box<{outer}{generic}>`. Consider using just `{outer}{generic}`",
24-
outer=item_type,
25-
generic = generic),
25+
"you seem to be trying to use `Box<{box_content}>`. Consider using just `{box_content}`"),
2626
None,
2727
&format!(
28-
"`{outer}{generic}` is already on the heap, `Box<{outer}{generic}>` makes an extra allocation",
29-
outer=item_type,
30-
generic = generic)
28+
"`{box_content}` is already on the heap, `Box<{box_content}>` makes an extra allocation")
3129
);
3230
true
3331
} else {
@@ -39,7 +37,18 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
3937
fn get_std_collection(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<Symbol> {
4038
let param = qpath_generic_tys(qpath).next()?;
4139
let id = path_def_id(cx, param)?;
42-
cx.tcx
43-
.get_diagnostic_name(id)
44-
.filter(|&name| matches!(name, sym::HashMap | sym::String | sym::Vec))
40+
cx.tcx.get_diagnostic_name(id).filter(|&name| {
41+
matches!(
42+
name,
43+
sym::HashMap
44+
| sym::String
45+
| sym::Vec
46+
| sym::HashSet
47+
| sym::VecDeque
48+
| sym::LinkedList
49+
| sym::BTreeMap
50+
| sym::BTreeSet
51+
| sym::BinaryHeap
52+
)
53+
})
4554
}

tests/ui/box_collection.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
unused
77
)]
88

9-
use std::collections::HashMap;
9+
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
1010

1111
macro_rules! boxit {
1212
($init:expr, $x:ty) => {
@@ -18,7 +18,7 @@ fn test_macro() {
1818
boxit!(Vec::new(), Vec<u8>);
1919
}
2020

21-
fn test(foo: Box<Vec<bool>>) {}
21+
fn test1(foo: Box<Vec<bool>>) {}
2222

2323
fn test2(foo: Box<dyn Fn(Vec<u32>)>) {
2424
// pass if #31 is fixed
@@ -29,6 +29,18 @@ fn test3(foo: Box<String>) {}
2929

3030
fn test4(foo: Box<HashMap<String, String>>) {}
3131

32+
fn test5(foo: Box<HashSet<i64>>) {}
33+
34+
fn test6(foo: Box<VecDeque<i32>>) {}
35+
36+
fn test7(foo: Box<LinkedList<i16>>) {}
37+
38+
fn test8(foo: Box<BTreeMap<i8, String>>) {}
39+
40+
fn test9(foo: Box<BTreeSet<u64>>) {}
41+
42+
fn test10(foo: Box<BinaryHeap<u32>>) {}
43+
3244
fn test_local_not_linted() {
3345
let _: Box<Vec<bool>>;
3446
}

tests/ui/box_collection.stderr

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: you seem to be trying to use `Box<Vec<..>>`. Consider using just `Vec<..>`
2-
--> $DIR/box_collection.rs:21:14
2+
--> $DIR/box_collection.rs:21:15
33
|
4-
LL | fn test(foo: Box<Vec<bool>>) {}
5-
| ^^^^^^^^^^^^^^
4+
LL | fn test1(foo: Box<Vec<bool>>) {}
5+
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::box-collection` implied by `-D warnings`
88
= help: `Vec<..>` is already on the heap, `Box<Vec<..>>` makes an extra allocation
@@ -23,5 +23,53 @@ LL | fn test4(foo: Box<HashMap<String, String>>) {}
2323
|
2424
= help: `HashMap<..>` is already on the heap, `Box<HashMap<..>>` makes an extra allocation
2525

26-
error: aborting due to 3 previous errors
26+
error: you seem to be trying to use `Box<HashSet<..>>`. Consider using just `HashSet<..>`
27+
--> $DIR/box_collection.rs:32:15
28+
|
29+
LL | fn test5(foo: Box<HashSet<i64>>) {}
30+
| ^^^^^^^^^^^^^^^^^
31+
|
32+
= help: `HashSet<..>` is already on the heap, `Box<HashSet<..>>` makes an extra allocation
33+
34+
error: you seem to be trying to use `Box<VecDeque<..>>`. Consider using just `VecDeque<..>`
35+
--> $DIR/box_collection.rs:34:15
36+
|
37+
LL | fn test6(foo: Box<VecDeque<i32>>) {}
38+
| ^^^^^^^^^^^^^^^^^^
39+
|
40+
= help: `VecDeque<..>` is already on the heap, `Box<VecDeque<..>>` makes an extra allocation
41+
42+
error: you seem to be trying to use `Box<LinkedList<..>>`. Consider using just `LinkedList<..>`
43+
--> $DIR/box_collection.rs:36:15
44+
|
45+
LL | fn test7(foo: Box<LinkedList<i16>>) {}
46+
| ^^^^^^^^^^^^^^^^^^^^
47+
|
48+
= help: `LinkedList<..>` is already on the heap, `Box<LinkedList<..>>` makes an extra allocation
49+
50+
error: you seem to be trying to use `Box<BTreeMap<..>>`. Consider using just `BTreeMap<..>`
51+
--> $DIR/box_collection.rs:38:15
52+
|
53+
LL | fn test8(foo: Box<BTreeMap<i8, String>>) {}
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
55+
|
56+
= help: `BTreeMap<..>` is already on the heap, `Box<BTreeMap<..>>` makes an extra allocation
57+
58+
error: you seem to be trying to use `Box<BTreeSet<..>>`. Consider using just `BTreeSet<..>`
59+
--> $DIR/box_collection.rs:40:15
60+
|
61+
LL | fn test9(foo: Box<BTreeSet<u64>>) {}
62+
| ^^^^^^^^^^^^^^^^^^
63+
|
64+
= help: `BTreeSet<..>` is already on the heap, `Box<BTreeSet<..>>` makes an extra allocation
65+
66+
error: you seem to be trying to use `Box<BinaryHeap<..>>`. Consider using just `BinaryHeap<..>`
67+
--> $DIR/box_collection.rs:42:16
68+
|
69+
LL | fn test10(foo: Box<BinaryHeap<u32>>) {}
70+
| ^^^^^^^^^^^^^^^^^^^^
71+
|
72+
= help: `BinaryHeap<..>` is already on the heap, `Box<BinaryHeap<..>>` makes an extra allocation
73+
74+
error: aborting due to 9 previous errors
2775

0 commit comments

Comments
 (0)