diff --git a/clippy_lints/src/types/box_collection.rs b/clippy_lints/src/types/box_collection.rs index 21a9558ec..ba51404d2 100644 --- a/clippy_lints/src/types/box_collection.rs +++ b/clippy_lints/src/types/box_collection.rs @@ -15,19 +15,17 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_ sym::String => "", _ => "<..>", }; + + let box_content = format!("{outer}{generic}", outer = item_type); span_lint_and_help( cx, BOX_COLLECTION, hir_ty.span, &format!( - "you seem to be trying to use `Box<{outer}{generic}>`. Consider using just `{outer}{generic}`", - outer=item_type, - generic = generic), + "you seem to be trying to use `Box<{box_content}>`. Consider using just `{box_content}`"), None, &format!( - "`{outer}{generic}` is already on the heap, `Box<{outer}{generic}>` makes an extra allocation", - outer=item_type, - generic = generic) + "`{box_content}` is already on the heap, `Box<{box_content}>` makes an extra allocation") ); true } else { @@ -39,7 +37,18 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_ fn get_std_collection(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option { let param = qpath_generic_tys(qpath).next()?; let id = path_def_id(cx, param)?; - cx.tcx - .get_diagnostic_name(id) - .filter(|&name| matches!(name, sym::HashMap | sym::String | sym::Vec)) + cx.tcx.get_diagnostic_name(id).filter(|&name| { + matches!( + name, + sym::HashMap + | sym::String + | sym::Vec + | sym::HashSet + | sym::VecDeque + | sym::LinkedList + | sym::BTreeMap + | sym::BTreeSet + | sym::BinaryHeap + ) + }) } diff --git a/tests/ui/box_collection.rs b/tests/ui/box_collection.rs index e00f061f2..1a74cdb3f 100644 --- a/tests/ui/box_collection.rs +++ b/tests/ui/box_collection.rs @@ -6,7 +6,7 @@ unused )] -use std::collections::HashMap; +use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; macro_rules! boxit { ($init:expr, $x:ty) => { @@ -18,7 +18,7 @@ fn test_macro() { boxit!(Vec::new(), Vec); } -fn test(foo: Box>) {} +fn test1(foo: Box>) {} fn test2(foo: Box)>) { // pass if #31 is fixed @@ -29,6 +29,18 @@ fn test3(foo: Box) {} fn test4(foo: Box>) {} +fn test5(foo: Box>) {} + +fn test6(foo: Box>) {} + +fn test7(foo: Box>) {} + +fn test8(foo: Box>) {} + +fn test9(foo: Box>) {} + +fn test10(foo: Box>) {} + fn test_local_not_linted() { let _: Box>; } diff --git a/tests/ui/box_collection.stderr b/tests/ui/box_collection.stderr index 6de85d05a..2b28598de 100644 --- a/tests/ui/box_collection.stderr +++ b/tests/ui/box_collection.stderr @@ -1,8 +1,8 @@ error: you seem to be trying to use `Box>`. Consider using just `Vec<..>` - --> $DIR/box_collection.rs:21:14 + --> $DIR/box_collection.rs:21:15 | -LL | fn test(foo: Box>) {} - | ^^^^^^^^^^^^^^ +LL | fn test1(foo: Box>) {} + | ^^^^^^^^^^^^^^ | = note: `-D clippy::box-collection` implied by `-D warnings` = help: `Vec<..>` is already on the heap, `Box>` makes an extra allocation @@ -23,5 +23,53 @@ LL | fn test4(foo: Box>) {} | = help: `HashMap<..>` is already on the heap, `Box>` makes an extra allocation -error: aborting due to 3 previous errors +error: you seem to be trying to use `Box>`. Consider using just `HashSet<..>` + --> $DIR/box_collection.rs:32:15 + | +LL | fn test5(foo: Box>) {} + | ^^^^^^^^^^^^^^^^^ + | + = help: `HashSet<..>` is already on the heap, `Box>` makes an extra allocation + +error: you seem to be trying to use `Box>`. Consider using just `VecDeque<..>` + --> $DIR/box_collection.rs:34:15 + | +LL | fn test6(foo: Box>) {} + | ^^^^^^^^^^^^^^^^^^ + | + = help: `VecDeque<..>` is already on the heap, `Box>` makes an extra allocation + +error: you seem to be trying to use `Box>`. Consider using just `LinkedList<..>` + --> $DIR/box_collection.rs:36:15 + | +LL | fn test7(foo: Box>) {} + | ^^^^^^^^^^^^^^^^^^^^ + | + = help: `LinkedList<..>` is already on the heap, `Box>` makes an extra allocation + +error: you seem to be trying to use `Box>`. Consider using just `BTreeMap<..>` + --> $DIR/box_collection.rs:38:15 + | +LL | fn test8(foo: Box>) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: `BTreeMap<..>` is already on the heap, `Box>` makes an extra allocation + +error: you seem to be trying to use `Box>`. Consider using just `BTreeSet<..>` + --> $DIR/box_collection.rs:40:15 + | +LL | fn test9(foo: Box>) {} + | ^^^^^^^^^^^^^^^^^^ + | + = help: `BTreeSet<..>` is already on the heap, `Box>` makes an extra allocation + +error: you seem to be trying to use `Box>`. Consider using just `BinaryHeap<..>` + --> $DIR/box_collection.rs:42:16 + | +LL | fn test10(foo: Box>) {} + | ^^^^^^^^^^^^^^^^^^^^ + | + = help: `BinaryHeap<..>` is already on the heap, `Box>` makes an extra allocation + +error: aborting due to 9 previous errors