Skip to content

Commit b6aa612

Browse files
committed
redundant_allocation: generic
1 parent 140d9a5 commit b6aa612

File tree

3 files changed

+75
-71
lines changed

3 files changed

+75
-71
lines changed

clippy_lints/src/types/redundant_allocation.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::source::snippet_with_applicability;
2+
use clippy_utils::source::{snippet, 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;
55
use rustc_hir::{self as hir, def_id::DefId, LangItem, QPath, TyKind};
@@ -21,21 +21,18 @@ 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+
let generic_snippet = snippet_with_applicability(cx, span, "..", &mut applicability);
2425
span_lint_and_then(
2526
cx,
2627
REDUNDANT_ALLOCATION,
2728
hir_ty.span,
28-
&format!("usage of `{}<&T>`", outer_sym),
29+
&format!("usage of `{}<{}>`", outer_sym, generic_snippet),
2930
|diag| {
30-
diag.span_suggestion(
31-
hir_ty.span,
32-
"try",
33-
format!("{}", snippet_with_applicability(cx, span, "..", &mut applicability)),
34-
applicability,
35-
);
31+
diag.span_suggestion(hir_ty.span, "try", format!("{}", generic_snippet), applicability);
3632
diag.note(&format!(
37-
"`&T` is already a pointer, `{}<&T>` allocate a pointer on heap",
38-
outer_sym,
33+
"`{generic}` is already a pointer, `{outer}<{generic}>` allocates a pointer on the heap",
34+
outer = outer_sym,
35+
generic = generic_snippet
3936
));
4037
},
4138
);
@@ -62,40 +59,47 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
6259
};
6360
if inner_sym == outer_sym {
6461
let mut applicability = Applicability::MaybeIncorrect;
62+
let generic_snippet = snippet_with_applicability(cx, inner_span, "..", &mut applicability);
6563
span_lint_and_then(
6664
cx,
6765
REDUNDANT_ALLOCATION,
6866
hir_ty.span,
69-
&format!("usage of `{}<{}<T>>`", outer_sym, inner_sym),
67+
&format!("usage of `{}<{}<{}>>`", outer_sym, inner_sym, generic_snippet),
7068
|diag| {
7169
diag.span_suggestion(
7270
hir_ty.span,
7371
"try",
74-
format!(
75-
"{}<{}>",
76-
outer_sym,
77-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
78-
),
72+
format!("{}<{}>", outer_sym, generic_snippet),
7973
applicability,
8074
);
8175
diag.note(&format!(
82-
"`{}<T>` is already on the heap, `{}<{}<T>>` makes an extra allocation",
83-
inner_sym, outer_sym, inner_sym,
76+
"`{inner}<{generic}>` is already on the heap, `{outer}<{inner}<{generic}>>` makes an extra allocation",
77+
outer = outer_sym,
78+
inner = inner_sym,
79+
generic = generic_snippet
8480
));
8581
},
8682
);
8783
} else {
84+
let generic_snippet = snippet(cx, inner_span, "..");
8885
span_lint_and_then(
8986
cx,
9087
REDUNDANT_ALLOCATION,
9188
hir_ty.span,
92-
&format!("usage of `{}<{}<T>>`", outer_sym, inner_sym),
89+
&format!("usage of `{}<{}<{}>>`", outer_sym, inner_sym, generic_snippet),
9390
|diag| {
9491
diag.note(&format!(
95-
"`{}<T>` is already on the heap, `{}<{}<T>>` makes an extra allocation",
96-
inner_sym, outer_sym, inner_sym,
92+
"`{inner}<{generic}>` is already on the heap, `{outer}<{inner}<{generic}>>` makes an extra allocation",
93+
outer = outer_sym,
94+
inner = inner_sym,
95+
generic = generic_snippet
96+
));
97+
diag.help(&format!(
98+
"consider using just `{outer}<{generic}>` or `{inner}<{generic}>`",
99+
outer = outer_sym,
100+
inner = inner_sym,
101+
generic = generic_snippet
97102
));
98-
diag.help(&format!("consider using just `{}<T>` or `{}<T>`", outer_sym, inner_sym,));
99103
},
100104
);
101105
}

tests/ui/redundant_allocation.stderr

+30-30
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ LL | pub fn box_test7<T>(foo: Box<Arc<T>>) {}
1717
= note: `Arc<T>` is already on the heap, `Box<Arc<T>>` makes an extra allocation
1818
= help: consider using just `Box<T>` or `Arc<T>`
1919

20-
error: usage of `Box<Rc<T>>`
20+
error: usage of `Box<Rc<SubT<usize>>>`
2121
--> $DIR/redundant_allocation.rs:29:27
2222
|
2323
LL | pub fn box_test8() -> Box<Rc<SubT<usize>>> {
2424
| ^^^^^^^^^^^^^^^^^^^^
2525
|
26-
= note: `Rc<T>` is already on the heap, `Box<Rc<T>>` makes an extra allocation
27-
= help: consider using just `Box<T>` or `Rc<T>`
26+
= note: `Rc<SubT<usize>>` is already on the heap, `Box<Rc<SubT<usize>>>` makes an extra allocation
27+
= help: consider using just `Box<SubT<usize>>` or `Rc<SubT<usize>>`
2828

2929
error: usage of `Box<Arc<T>>`
3030
--> $DIR/redundant_allocation.rs:33:30
@@ -35,41 +35,41 @@ LL | pub fn box_test9<T>(foo: Box<Arc<T>>) -> Box<Arc<SubT<T>>> {
3535
= note: `Arc<T>` is already on the heap, `Box<Arc<T>>` makes an extra allocation
3636
= help: consider using just `Box<T>` or `Arc<T>`
3737

38-
error: usage of `Box<Arc<T>>`
38+
error: usage of `Box<Arc<SubT<T>>>`
3939
--> $DIR/redundant_allocation.rs:33:46
4040
|
4141
LL | pub fn box_test9<T>(foo: Box<Arc<T>>) -> Box<Arc<SubT<T>>> {
4242
| ^^^^^^^^^^^^^^^^^
4343
|
44-
= note: `Arc<T>` is already on the heap, `Box<Arc<T>>` makes an extra allocation
45-
= help: consider using just `Box<T>` or `Arc<T>`
44+
= note: `Arc<SubT<T>>` is already on the heap, `Box<Arc<SubT<T>>>` makes an extra allocation
45+
= help: consider using just `Box<SubT<T>>` or `Arc<SubT<T>>`
4646

47-
error: usage of `Rc<Box<T>>`
47+
error: usage of `Rc<Box<bool>>`
4848
--> $DIR/redundant_allocation.rs:46:24
4949
|
5050
LL | pub fn rc_test5(a: Rc<Box<bool>>) {}
5151
| ^^^^^^^^^^^^^
5252
|
53-
= note: `Box<T>` is already on the heap, `Rc<Box<T>>` makes an extra allocation
54-
= help: consider using just `Rc<T>` or `Box<T>`
53+
= note: `Box<bool>` is already on the heap, `Rc<Box<bool>>` makes an extra allocation
54+
= help: consider using just `Rc<bool>` or `Box<bool>`
5555

56-
error: usage of `Rc<Arc<T>>`
56+
error: usage of `Rc<Arc<bool>>`
5757
--> $DIR/redundant_allocation.rs:48:24
5858
|
5959
LL | pub fn rc_test7(a: Rc<Arc<bool>>) {}
6060
| ^^^^^^^^^^^^^
6161
|
62-
= note: `Arc<T>` is already on the heap, `Rc<Arc<T>>` makes an extra allocation
63-
= help: consider using just `Rc<T>` or `Arc<T>`
62+
= note: `Arc<bool>` is already on the heap, `Rc<Arc<bool>>` makes an extra allocation
63+
= help: consider using just `Rc<bool>` or `Arc<bool>`
6464

65-
error: usage of `Rc<Box<T>>`
65+
error: usage of `Rc<Box<SubT<usize>>>`
6666
--> $DIR/redundant_allocation.rs:50:26
6767
|
6868
LL | pub fn rc_test8() -> Rc<Box<SubT<usize>>> {
6969
| ^^^^^^^^^^^^^^^^^^^^
7070
|
71-
= note: `Box<T>` is already on the heap, `Rc<Box<T>>` makes an extra allocation
72-
= help: consider using just `Rc<T>` or `Box<T>`
71+
= note: `Box<SubT<usize>>` is already on the heap, `Rc<Box<SubT<usize>>>` makes an extra allocation
72+
= help: consider using just `Rc<SubT<usize>>` or `Box<SubT<usize>>`
7373

7474
error: usage of `Rc<Arc<T>>`
7575
--> $DIR/redundant_allocation.rs:54:29
@@ -80,41 +80,41 @@ LL | pub fn rc_test9<T>(foo: Rc<Arc<T>>) -> Rc<Arc<SubT<T>>> {
8080
= note: `Arc<T>` is already on the heap, `Rc<Arc<T>>` makes an extra allocation
8181
= help: consider using just `Rc<T>` or `Arc<T>`
8282

83-
error: usage of `Rc<Arc<T>>`
83+
error: usage of `Rc<Arc<SubT<T>>>`
8484
--> $DIR/redundant_allocation.rs:54:44
8585
|
8686
LL | pub fn rc_test9<T>(foo: Rc<Arc<T>>) -> Rc<Arc<SubT<T>>> {
8787
| ^^^^^^^^^^^^^^^^
8888
|
89-
= note: `Arc<T>` is already on the heap, `Rc<Arc<T>>` makes an extra allocation
90-
= help: consider using just `Rc<T>` or `Arc<T>`
89+
= note: `Arc<SubT<T>>` is already on the heap, `Rc<Arc<SubT<T>>>` makes an extra allocation
90+
= help: consider using just `Rc<SubT<T>>` or `Arc<SubT<T>>`
9191

92-
error: usage of `Arc<Box<T>>`
92+
error: usage of `Arc<Box<bool>>`
9393
--> $DIR/redundant_allocation.rs:67:25
9494
|
9595
LL | pub fn arc_test5(a: Arc<Box<bool>>) {}
9696
| ^^^^^^^^^^^^^^
9797
|
98-
= note: `Box<T>` is already on the heap, `Arc<Box<T>>` makes an extra allocation
99-
= help: consider using just `Arc<T>` or `Box<T>`
98+
= note: `Box<bool>` is already on the heap, `Arc<Box<bool>>` makes an extra allocation
99+
= help: consider using just `Arc<bool>` or `Box<bool>`
100100

101-
error: usage of `Arc<Rc<T>>`
101+
error: usage of `Arc<Rc<bool>>`
102102
--> $DIR/redundant_allocation.rs:69:25
103103
|
104104
LL | pub fn arc_test6(a: Arc<Rc<bool>>) {}
105105
| ^^^^^^^^^^^^^
106106
|
107-
= note: `Rc<T>` is already on the heap, `Arc<Rc<T>>` makes an extra allocation
108-
= help: consider using just `Arc<T>` or `Rc<T>`
107+
= note: `Rc<bool>` is already on the heap, `Arc<Rc<bool>>` makes an extra allocation
108+
= help: consider using just `Arc<bool>` or `Rc<bool>`
109109

110-
error: usage of `Arc<Box<T>>`
110+
error: usage of `Arc<Box<SubT<usize>>>`
111111
--> $DIR/redundant_allocation.rs:71:27
112112
|
113113
LL | pub fn arc_test8() -> Arc<Box<SubT<usize>>> {
114114
| ^^^^^^^^^^^^^^^^^^^^^
115115
|
116-
= note: `Box<T>` is already on the heap, `Arc<Box<T>>` makes an extra allocation
117-
= help: consider using just `Arc<T>` or `Box<T>`
116+
= note: `Box<SubT<usize>>` is already on the heap, `Arc<Box<SubT<usize>>>` makes an extra allocation
117+
= help: consider using just `Arc<SubT<usize>>` or `Box<SubT<usize>>`
118118

119119
error: usage of `Arc<Rc<T>>`
120120
--> $DIR/redundant_allocation.rs:75:30
@@ -125,14 +125,14 @@ LL | pub fn arc_test9<T>(foo: Arc<Rc<T>>) -> Arc<Rc<SubT<T>>> {
125125
= note: `Rc<T>` is already on the heap, `Arc<Rc<T>>` makes an extra allocation
126126
= help: consider using just `Arc<T>` or `Rc<T>`
127127

128-
error: usage of `Arc<Rc<T>>`
128+
error: usage of `Arc<Rc<SubT<T>>>`
129129
--> $DIR/redundant_allocation.rs:75:45
130130
|
131131
LL | pub fn arc_test9<T>(foo: Arc<Rc<T>>) -> Arc<Rc<SubT<T>>> {
132132
| ^^^^^^^^^^^^^^^^
133133
|
134-
= note: `Rc<T>` is already on the heap, `Arc<Rc<T>>` makes an extra allocation
135-
= help: consider using just `Arc<T>` or `Rc<T>`
134+
= note: `Rc<SubT<T>>` is already on the heap, `Arc<Rc<SubT<T>>>` makes an extra allocation
135+
= help: consider using just `Arc<SubT<T>>` or `Rc<SubT<T>>`
136136

137137
error: aborting due to 15 previous errors
138138

tests/ui/redundant_allocation_fixable.stderr

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ 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
8+
= note: `&T` is already a pointer, `Box<&T>` allocates a pointer on the heap
99

10-
error: usage of `Box<&T>`
10+
error: usage of `Box<&MyStruct>`
1111
--> $DIR/redundant_allocation_fixable.rs:28:27
1212
|
1313
LL | pub fn box_test2(foo: Box<&MyStruct>) {}
1414
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`
1515
|
16-
= note: `&T` is already a pointer, `Box<&T>` allocate a pointer on heap
16+
= note: `&MyStruct` is already a pointer, `Box<&MyStruct>` allocates a pointer on the heap
1717

18-
error: usage of `Box<&T>`
18+
error: usage of `Box<&MyEnum>`
1919
--> $DIR/redundant_allocation_fixable.rs:30:27
2020
|
2121
LL | pub fn box_test3(foo: Box<&MyEnum>) {}
2222
| ^^^^^^^^^^^^ help: try: `&MyEnum`
2323
|
24-
= note: `&T` is already a pointer, `Box<&T>` allocate a pointer on heap
24+
= note: `&MyEnum` is already a pointer, `Box<&MyEnum>` allocates a pointer on the heap
2525

2626
error: usage of `Box<Box<T>>`
2727
--> $DIR/redundant_allocation_fixable.rs:34:30
@@ -37,63 +37,63 @@ error: usage of `Rc<&T>`
3737
LL | pub fn rc_test1<T>(foo: Rc<&T>) {}
3838
| ^^^^^^ help: try: `&T`
3939
|
40-
= note: `&T` is already a pointer, `Rc<&T>` allocate a pointer on heap
40+
= note: `&T` is already a pointer, `Rc<&T>` allocates a pointer on the heap
4141

42-
error: usage of `Rc<&T>`
42+
error: usage of `Rc<&MyStruct>`
4343
--> $DIR/redundant_allocation_fixable.rs:47:26
4444
|
4545
LL | pub fn rc_test2(foo: Rc<&MyStruct>) {}
4646
| ^^^^^^^^^^^^^ help: try: `&MyStruct`
4747
|
48-
= note: `&T` is already a pointer, `Rc<&T>` allocate a pointer on heap
48+
= note: `&MyStruct` is already a pointer, `Rc<&MyStruct>` allocates a pointer on the heap
4949

50-
error: usage of `Rc<&T>`
50+
error: usage of `Rc<&MyEnum>`
5151
--> $DIR/redundant_allocation_fixable.rs:49:26
5252
|
5353
LL | pub fn rc_test3(foo: Rc<&MyEnum>) {}
5454
| ^^^^^^^^^^^ help: try: `&MyEnum`
5555
|
56-
= note: `&T` is already a pointer, `Rc<&T>` allocate a pointer on heap
56+
= note: `&MyEnum` is already a pointer, `Rc<&MyEnum>` allocates a pointer on the heap
5757

58-
error: usage of `Rc<Rc<T>>`
58+
error: usage of `Rc<Rc<bool>>`
5959
--> $DIR/redundant_allocation_fixable.rs:53:24
6060
|
6161
LL | pub fn rc_test6(a: Rc<Rc<bool>>) {}
6262
| ^^^^^^^^^^^^ help: try: `Rc<bool>`
6363
|
64-
= note: `Rc<T>` is already on the heap, `Rc<Rc<T>>` makes an extra allocation
64+
= note: `Rc<bool>` is already on the heap, `Rc<Rc<bool>>` makes an extra allocation
6565

6666
error: usage of `Arc<&T>`
6767
--> $DIR/redundant_allocation_fixable.rs:64:30
6868
|
6969
LL | pub fn arc_test1<T>(foo: Arc<&T>) {}
7070
| ^^^^^^^ help: try: `&T`
7171
|
72-
= note: `&T` is already a pointer, `Arc<&T>` allocate a pointer on heap
72+
= note: `&T` is already a pointer, `Arc<&T>` allocates a pointer on the heap
7373

74-
error: usage of `Arc<&T>`
74+
error: usage of `Arc<&MyStruct>`
7575
--> $DIR/redundant_allocation_fixable.rs:66:27
7676
|
7777
LL | pub fn arc_test2(foo: Arc<&MyStruct>) {}
7878
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`
7979
|
80-
= note: `&T` is already a pointer, `Arc<&T>` allocate a pointer on heap
80+
= note: `&MyStruct` is already a pointer, `Arc<&MyStruct>` allocates a pointer on the heap
8181

82-
error: usage of `Arc<&T>`
82+
error: usage of `Arc<&MyEnum>`
8383
--> $DIR/redundant_allocation_fixable.rs:68:27
8484
|
8585
LL | pub fn arc_test3(foo: Arc<&MyEnum>) {}
8686
| ^^^^^^^^^^^^ help: try: `&MyEnum`
8787
|
88-
= note: `&T` is already a pointer, `Arc<&T>` allocate a pointer on heap
88+
= note: `&MyEnum` is already a pointer, `Arc<&MyEnum>` allocates a pointer on the heap
8989

90-
error: usage of `Arc<Arc<T>>`
90+
error: usage of `Arc<Arc<bool>>`
9191
--> $DIR/redundant_allocation_fixable.rs:72:25
9292
|
9393
LL | pub fn arc_test7(a: Arc<Arc<bool>>) {}
9494
| ^^^^^^^^^^^^^^ help: try: `Arc<bool>`
9595
|
96-
= note: `Arc<T>` is already on the heap, `Arc<Arc<T>>` makes an extra allocation
96+
= note: `Arc<bool>` is already on the heap, `Arc<Arc<bool>>` makes an extra allocation
9797

9898
error: aborting due to 12 previous errors
9999

0 commit comments

Comments
 (0)