Skip to content

Commit cccc5e6

Browse files
committed
redundant_allocation: use span_lint_and_then
1 parent fe9fbdc commit cccc5e6

5 files changed

+67
-53
lines changed

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
1+
use clippy_utils::diagnostics::{span_lint_and_sugg, 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;
@@ -67,19 +67,18 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
6767
applicability,
6868
);
6969
} else {
70-
span_lint_and_help(
70+
span_lint_and_then(
7171
cx,
7272
REDUNDANT_ALLOCATION,
7373
hir_ty.span,
74-
&format!(
75-
"you seem to be trying to use `{}<{}<T>>`. Consider using just `{}<T>` or `{}<T>`",
76-
outer_sym, inner_sym, outer_sym, inner_sym,
77-
),
78-
None,
79-
&format!(
80-
"`{}<T>` is already on the heap, `{}<{}<T>>` makes an extra allocation",
81-
inner_sym, outer_sym, inner_sym,
82-
),
74+
&format!("usage of `{}<{}<T>>`", outer_sym, inner_sym),
75+
|diag| {
76+
diag.note(&format!(
77+
"`{}<T>` is already on the heap, `{}<{}<T>>` makes an extra allocation",
78+
inner_sym, outer_sym, inner_sym,
79+
));
80+
diag.help(&format!("consider using just `{}<T>` or `{}<T>`", outer_sym, inner_sym,));
81+
},
8382
);
8483
}
8584
true

tests/ui/redundant_allocation.stderr

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,138 @@
1-
error: you seem to be trying to use `Box<Rc<T>>`. Consider using just `Box<T>` of `Rc<T>`
1+
error: usage of `Box<Rc<T>>`
22
--> $DIR/redundant_allocation.rs:25:30
33
|
44
LL | pub fn box_test6<T>(foo: Box<Rc<T>>) {}
55
| ^^^^^^^^^^
66
|
77
= note: `-D clippy::redundant-allocation` implied by `-D warnings`
8-
= help: `Rc<T>` is already on the heap, `Box<Rc<T>>` makes an extra allocation
8+
= note: `Rc<T>` is already on the heap, `Box<Rc<T>>` makes an extra allocation
9+
= help: consider using just `Box<T>` or `Rc<T>`
910

10-
error: you seem to be trying to use `Box<Arc<T>>`. Consider using just `Box<T>` of `Arc<T>`
11+
error: usage of `Box<Arc<T>>`
1112
--> $DIR/redundant_allocation.rs:27:30
1213
|
1314
LL | pub fn box_test7<T>(foo: Box<Arc<T>>) {}
1415
| ^^^^^^^^^^^
1516
|
16-
= help: `Arc<T>` is already on the heap, `Box<Arc<T>>` makes an extra allocation
17+
= note: `Arc<T>` is already on the heap, `Box<Arc<T>>` makes an extra allocation
18+
= help: consider using just `Box<T>` or `Arc<T>`
1719

18-
error: you seem to be trying to use `Box<Rc<T>>`. Consider using just `Box<T>` of `Rc<T>`
20+
error: usage of `Box<Rc<T>>`
1921
--> $DIR/redundant_allocation.rs:29:27
2022
|
2123
LL | pub fn box_test8() -> Box<Rc<SubT<usize>>> {
2224
| ^^^^^^^^^^^^^^^^^^^^
2325
|
24-
= help: `Rc<T>` is already on the heap, `Box<Rc<T>>` makes an extra allocation
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>`
2528

26-
error: you seem to be trying to use `Box<Arc<T>>`. Consider using just `Box<T>` of `Arc<T>`
29+
error: usage of `Box<Arc<T>>`
2730
--> $DIR/redundant_allocation.rs:33:30
2831
|
2932
LL | pub fn box_test9<T>(foo: Box<Arc<T>>) -> Box<Arc<SubT<T>>> {
3033
| ^^^^^^^^^^^
3134
|
32-
= help: `Arc<T>` is already on the heap, `Box<Arc<T>>` makes an extra allocation
35+
= note: `Arc<T>` is already on the heap, `Box<Arc<T>>` makes an extra allocation
36+
= help: consider using just `Box<T>` or `Arc<T>`
3337

34-
error: you seem to be trying to use `Box<Arc<T>>`. Consider using just `Box<T>` of `Arc<T>`
38+
error: usage of `Box<Arc<T>>`
3539
--> $DIR/redundant_allocation.rs:33:46
3640
|
3741
LL | pub fn box_test9<T>(foo: Box<Arc<T>>) -> Box<Arc<SubT<T>>> {
3842
| ^^^^^^^^^^^^^^^^^
3943
|
40-
= help: `Arc<T>` is already on the heap, `Box<Arc<T>>` makes an extra allocation
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>`
4146

42-
error: you seem to be trying to use `Rc<Box<T>>`. Consider using just `Rc<T>` of `Box<T>`
47+
error: usage of `Rc<Box<T>>`
4348
--> $DIR/redundant_allocation.rs:46:24
4449
|
4550
LL | pub fn rc_test5(a: Rc<Box<bool>>) {}
4651
| ^^^^^^^^^^^^^
4752
|
48-
= help: `Box<T>` is already on the heap, `Rc<Box<T>>` makes an extra allocation
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>`
4955

50-
error: you seem to be trying to use `Rc<Arc<T>>`. Consider using just `Rc<T>` of `Arc<T>`
56+
error: usage of `Rc<Arc<T>>`
5157
--> $DIR/redundant_allocation.rs:48:24
5258
|
5359
LL | pub fn rc_test7(a: Rc<Arc<bool>>) {}
5460
| ^^^^^^^^^^^^^
5561
|
56-
= help: `Arc<T>` is already on the heap, `Rc<Arc<T>>` makes an extra allocation
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>`
5764

58-
error: you seem to be trying to use `Rc<Box<T>>`. Consider using just `Rc<T>` of `Box<T>`
65+
error: usage of `Rc<Box<T>>`
5966
--> $DIR/redundant_allocation.rs:50:26
6067
|
6168
LL | pub fn rc_test8() -> Rc<Box<SubT<usize>>> {
6269
| ^^^^^^^^^^^^^^^^^^^^
6370
|
64-
= help: `Box<T>` is already on the heap, `Rc<Box<T>>` makes an extra allocation
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>`
6573

66-
error: you seem to be trying to use `Rc<Arc<T>>`. Consider using just `Rc<T>` of `Arc<T>`
74+
error: usage of `Rc<Arc<T>>`
6775
--> $DIR/redundant_allocation.rs:54:29
6876
|
6977
LL | pub fn rc_test9<T>(foo: Rc<Arc<T>>) -> Rc<Arc<SubT<T>>> {
7078
| ^^^^^^^^^^
7179
|
72-
= help: `Arc<T>` is already on the heap, `Rc<Arc<T>>` makes an extra allocation
80+
= note: `Arc<T>` is already on the heap, `Rc<Arc<T>>` makes an extra allocation
81+
= help: consider using just `Rc<T>` or `Arc<T>`
7382

74-
error: you seem to be trying to use `Rc<Arc<T>>`. Consider using just `Rc<T>` of `Arc<T>`
83+
error: usage of `Rc<Arc<T>>`
7584
--> $DIR/redundant_allocation.rs:54:44
7685
|
7786
LL | pub fn rc_test9<T>(foo: Rc<Arc<T>>) -> Rc<Arc<SubT<T>>> {
7887
| ^^^^^^^^^^^^^^^^
7988
|
80-
= help: `Arc<T>` is already on the heap, `Rc<Arc<T>>` makes an extra allocation
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>`
8191

82-
error: you seem to be trying to use `Arc<Box<T>>`. Consider using just `Arc<T>` of `Box<T>`
92+
error: usage of `Arc<Box<T>>`
8393
--> $DIR/redundant_allocation.rs:67:25
8494
|
8595
LL | pub fn arc_test5(a: Arc<Box<bool>>) {}
8696
| ^^^^^^^^^^^^^^
8797
|
88-
= help: `Box<T>` is already on the heap, `Arc<Box<T>>` makes an extra allocation
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>`
89100

90-
error: you seem to be trying to use `Arc<Rc<T>>`. Consider using just `Arc<T>` of `Rc<T>`
101+
error: usage of `Arc<Rc<T>>`
91102
--> $DIR/redundant_allocation.rs:69:25
92103
|
93104
LL | pub fn arc_test6(a: Arc<Rc<bool>>) {}
94105
| ^^^^^^^^^^^^^
95106
|
96-
= help: `Rc<T>` is already on the heap, `Arc<Rc<T>>` makes an extra allocation
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>`
97109

98-
error: you seem to be trying to use `Arc<Box<T>>`. Consider using just `Arc<T>` of `Box<T>`
110+
error: usage of `Arc<Box<T>>`
99111
--> $DIR/redundant_allocation.rs:71:27
100112
|
101113
LL | pub fn arc_test8() -> Arc<Box<SubT<usize>>> {
102114
| ^^^^^^^^^^^^^^^^^^^^^
103115
|
104-
= help: `Box<T>` is already on the heap, `Arc<Box<T>>` makes an extra allocation
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>`
105118

106-
error: you seem to be trying to use `Arc<Rc<T>>`. Consider using just `Arc<T>` of `Rc<T>`
119+
error: usage of `Arc<Rc<T>>`
107120
--> $DIR/redundant_allocation.rs:75:30
108121
|
109122
LL | pub fn arc_test9<T>(foo: Arc<Rc<T>>) -> Arc<Rc<SubT<T>>> {
110123
| ^^^^^^^^^^
111124
|
112-
= help: `Rc<T>` is already on the heap, `Arc<Rc<T>>` makes an extra allocation
125+
= note: `Rc<T>` is already on the heap, `Arc<Rc<T>>` makes an extra allocation
126+
= help: consider using just `Arc<T>` or `Rc<T>`
113127

114-
error: you seem to be trying to use `Arc<Rc<T>>`. Consider using just `Arc<T>` of `Rc<T>`
128+
error: usage of `Arc<Rc<T>>`
115129
--> $DIR/redundant_allocation.rs:75:45
116130
|
117131
LL | pub fn arc_test9<T>(foo: Arc<Rc<T>>) -> Arc<Rc<SubT<T>>> {
118132
| ^^^^^^^^^^^^^^^^
119133
|
120-
= help: `Rc<T>` is already on the heap, `Arc<Rc<T>>` makes an extra allocation
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>`
121136

122137
error: aborting due to 15 previous errors
123138

tests/ui/redundant_allocation_fixed.stderr renamed to tests/ui/redundant_allocation_fixable.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
error: usage of `Box<&T>`
2-
--> $DIR/redundant_allocation_fixed.rs:26:30
2+
--> $DIR/redundant_allocation_fixable.rs:26:30
33
|
44
LL | pub fn box_test1<T>(foo: Box<&T>) {}
55
| ^^^^^^^ help: try: `&T`
66
|
77
= note: `-D clippy::redundant-allocation` implied by `-D warnings`
88

99
error: usage of `Box<&T>`
10-
--> $DIR/redundant_allocation_fixed.rs:28:27
10+
--> $DIR/redundant_allocation_fixable.rs:28:27
1111
|
1212
LL | pub fn box_test2(foo: Box<&MyStruct>) {}
1313
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`
1414

1515
error: usage of `Box<&T>`
16-
--> $DIR/redundant_allocation_fixed.rs:30:27
16+
--> $DIR/redundant_allocation_fixable.rs:30:27
1717
|
1818
LL | pub fn box_test3(foo: Box<&MyEnum>) {}
1919
| ^^^^^^^^^^^^ help: try: `&MyEnum`
2020

2121
error: usage of `Box<Box<T>>`
22-
--> $DIR/redundant_allocation_fixed.rs:34:30
22+
--> $DIR/redundant_allocation_fixable.rs:34:30
2323
|
2424
LL | pub fn box_test5<T>(foo: Box<Box<T>>) {}
2525
| ^^^^^^^^^^^ help: try: `Box<T>`
2626

2727
error: usage of `Rc<&T>`
28-
--> $DIR/redundant_allocation_fixed.rs:45:29
28+
--> $DIR/redundant_allocation_fixable.rs:45:29
2929
|
3030
LL | pub fn rc_test1<T>(foo: Rc<&T>) {}
3131
| ^^^^^^ help: try: `&T`
3232

3333
error: usage of `Rc<&T>`
34-
--> $DIR/redundant_allocation_fixed.rs:47:26
34+
--> $DIR/redundant_allocation_fixable.rs:47:26
3535
|
3636
LL | pub fn rc_test2(foo: Rc<&MyStruct>) {}
3737
| ^^^^^^^^^^^^^ help: try: `&MyStruct`
3838

3939
error: usage of `Rc<&T>`
40-
--> $DIR/redundant_allocation_fixed.rs:49:26
40+
--> $DIR/redundant_allocation_fixable.rs:49:26
4141
|
4242
LL | pub fn rc_test3(foo: Rc<&MyEnum>) {}
4343
| ^^^^^^^^^^^ help: try: `&MyEnum`
4444

4545
error: usage of `Rc<Rc<T>>`
46-
--> $DIR/redundant_allocation_fixed.rs:53:24
46+
--> $DIR/redundant_allocation_fixable.rs:53:24
4747
|
4848
LL | pub fn rc_test6(a: Rc<Rc<bool>>) {}
4949
| ^^^^^^^^^^^^ help: try: `Rc<bool>`
5050

5151
error: usage of `Arc<&T>`
52-
--> $DIR/redundant_allocation_fixed.rs:64:30
52+
--> $DIR/redundant_allocation_fixable.rs:64:30
5353
|
5454
LL | pub fn arc_test1<T>(foo: Arc<&T>) {}
5555
| ^^^^^^^ help: try: `&T`
5656

5757
error: usage of `Arc<&T>`
58-
--> $DIR/redundant_allocation_fixed.rs:66:27
58+
--> $DIR/redundant_allocation_fixable.rs:66:27
5959
|
6060
LL | pub fn arc_test2(foo: Arc<&MyStruct>) {}
6161
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`
6262

6363
error: usage of `Arc<&T>`
64-
--> $DIR/redundant_allocation_fixed.rs:68:27
64+
--> $DIR/redundant_allocation_fixable.rs:68:27
6565
|
6666
LL | pub fn arc_test3(foo: Arc<&MyEnum>) {}
6767
| ^^^^^^^^^^^^ help: try: `&MyEnum`
6868

6969
error: usage of `Arc<Arc<T>>`
70-
--> $DIR/redundant_allocation_fixed.rs:72:25
70+
--> $DIR/redundant_allocation_fixable.rs:72:25
7171
|
7272
LL | pub fn arc_test7(a: Arc<Arc<bool>>) {}
7373
| ^^^^^^^^^^^^^^ help: try: `Arc<bool>`

0 commit comments

Comments
 (0)