Skip to content

Commit 140d9a5

Browse files
committed
redundant_allocation: add note to Box<&T>,Box<Box<T>>
1 parent cccc5e6 commit 140d9a5

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::{get_qpath_generic_tys, is_ty_param_diagnostic_item, is_ty_param_lang_item};
44
use rustc_errors::Applicability;
@@ -21,14 +21,23 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
2121

2222
if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
2323
let mut applicability = Applicability::MaybeIncorrect;
24-
span_lint_and_sugg(
24+
span_lint_and_then(
2525
cx,
2626
REDUNDANT_ALLOCATION,
2727
hir_ty.span,
2828
&format!("usage of `{}<&T>`", outer_sym),
29-
"try",
30-
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
31-
applicability,
29+
|diag| {
30+
diag.span_suggestion(
31+
hir_ty.span,
32+
"try",
33+
format!("{}", snippet_with_applicability(cx, span, "..", &mut applicability)),
34+
applicability,
35+
);
36+
diag.note(&format!(
37+
"`&T` is already a pointer, `{}<&T>` allocate a pointer on heap",
38+
outer_sym,
39+
));
40+
},
3241
);
3342
return true;
3443
}
@@ -53,18 +62,27 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
5362
};
5463
if inner_sym == outer_sym {
5564
let mut applicability = Applicability::MaybeIncorrect;
56-
span_lint_and_sugg(
65+
span_lint_and_then(
5766
cx,
5867
REDUNDANT_ALLOCATION,
5968
hir_ty.span,
6069
&format!("usage of `{}<{}<T>>`", outer_sym, inner_sym),
61-
"try",
62-
format!(
63-
"{}<{}>",
64-
outer_sym,
65-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
66-
),
67-
applicability,
70+
|diag| {
71+
diag.span_suggestion(
72+
hir_ty.span,
73+
"try",
74+
format!(
75+
"{}<{}>",
76+
outer_sym,
77+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
78+
),
79+
applicability,
80+
);
81+
diag.note(&format!(
82+
"`{}<T>` is already on the heap, `{}<{}<T>>` makes an extra allocation",
83+
inner_sym, outer_sym, inner_sym,
84+
));
85+
},
6886
);
6987
} else {
7088
span_lint_and_then(

tests/ui/redundant_allocation_fixable.stderr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,95 @@ LL | pub fn box_test1<T>(foo: Box<&T>) {}
55
| ^^^^^^^ help: try: `&T`
66
|
77
= note: `-D clippy::redundant-allocation` implied by `-D warnings`
8+
= note: `&T` is already a pointer, `Box<&T>` allocate a pointer on heap
89

910
error: usage of `Box<&T>`
1011
--> $DIR/redundant_allocation_fixable.rs:28:27
1112
|
1213
LL | pub fn box_test2(foo: Box<&MyStruct>) {}
1314
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`
15+
|
16+
= note: `&T` is already a pointer, `Box<&T>` allocate a pointer on heap
1417

1518
error: usage of `Box<&T>`
1619
--> $DIR/redundant_allocation_fixable.rs:30:27
1720
|
1821
LL | pub fn box_test3(foo: Box<&MyEnum>) {}
1922
| ^^^^^^^^^^^^ help: try: `&MyEnum`
23+
|
24+
= note: `&T` is already a pointer, `Box<&T>` allocate a pointer on heap
2025

2126
error: usage of `Box<Box<T>>`
2227
--> $DIR/redundant_allocation_fixable.rs:34:30
2328
|
2429
LL | pub fn box_test5<T>(foo: Box<Box<T>>) {}
2530
| ^^^^^^^^^^^ help: try: `Box<T>`
31+
|
32+
= note: `Box<T>` is already on the heap, `Box<Box<T>>` makes an extra allocation
2633

2734
error: usage of `Rc<&T>`
2835
--> $DIR/redundant_allocation_fixable.rs:45:29
2936
|
3037
LL | pub fn rc_test1<T>(foo: Rc<&T>) {}
3138
| ^^^^^^ help: try: `&T`
39+
|
40+
= note: `&T` is already a pointer, `Rc<&T>` allocate a pointer on heap
3241

3342
error: usage of `Rc<&T>`
3443
--> $DIR/redundant_allocation_fixable.rs:47:26
3544
|
3645
LL | pub fn rc_test2(foo: Rc<&MyStruct>) {}
3746
| ^^^^^^^^^^^^^ help: try: `&MyStruct`
47+
|
48+
= note: `&T` is already a pointer, `Rc<&T>` allocate a pointer on heap
3849

3950
error: usage of `Rc<&T>`
4051
--> $DIR/redundant_allocation_fixable.rs:49:26
4152
|
4253
LL | pub fn rc_test3(foo: Rc<&MyEnum>) {}
4354
| ^^^^^^^^^^^ help: try: `&MyEnum`
55+
|
56+
= note: `&T` is already a pointer, `Rc<&T>` allocate a pointer on heap
4457

4558
error: usage of `Rc<Rc<T>>`
4659
--> $DIR/redundant_allocation_fixable.rs:53:24
4760
|
4861
LL | pub fn rc_test6(a: Rc<Rc<bool>>) {}
4962
| ^^^^^^^^^^^^ help: try: `Rc<bool>`
63+
|
64+
= note: `Rc<T>` is already on the heap, `Rc<Rc<T>>` makes an extra allocation
5065

5166
error: usage of `Arc<&T>`
5267
--> $DIR/redundant_allocation_fixable.rs:64:30
5368
|
5469
LL | pub fn arc_test1<T>(foo: Arc<&T>) {}
5570
| ^^^^^^^ help: try: `&T`
71+
|
72+
= note: `&T` is already a pointer, `Arc<&T>` allocate a pointer on heap
5673

5774
error: usage of `Arc<&T>`
5875
--> $DIR/redundant_allocation_fixable.rs:66:27
5976
|
6077
LL | pub fn arc_test2(foo: Arc<&MyStruct>) {}
6178
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`
79+
|
80+
= note: `&T` is already a pointer, `Arc<&T>` allocate a pointer on heap
6281

6382
error: usage of `Arc<&T>`
6483
--> $DIR/redundant_allocation_fixable.rs:68:27
6584
|
6685
LL | pub fn arc_test3(foo: Arc<&MyEnum>) {}
6786
| ^^^^^^^^^^^^ help: try: `&MyEnum`
87+
|
88+
= note: `&T` is already a pointer, `Arc<&T>` allocate a pointer on heap
6889

6990
error: usage of `Arc<Arc<T>>`
7091
--> $DIR/redundant_allocation_fixable.rs:72:25
7192
|
7293
LL | pub fn arc_test7(a: Arc<Arc<bool>>) {}
7394
| ^^^^^^^^^^^^^^ help: try: `Arc<bool>`
95+
|
96+
= note: `Arc<T>` is already on the heap, `Arc<Arc<T>>` makes an extra allocation
7497

7598
error: aborting due to 12 previous errors
7699

0 commit comments

Comments
 (0)