Skip to content

Commit 2d0d913

Browse files
committed
Use more accurate suggestion for boxing in clippy
1 parent 2495aae commit 2d0d913

File tree

3 files changed

+34
-57
lines changed

3 files changed

+34
-57
lines changed

src/tools/clippy/clippy_lints/src/large_enum_variant.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! lint when there is a large size difference between variants on an enum
22
33
use clippy_utils::diagnostics::span_lint_and_then;
4-
use clippy_utils::source::snippet_with_applicability;
54
use clippy_utils::ty::{approx_ty_size, is_copy, AdtVariantInfo};
65
use rustc_errors::Applicability;
76
use rustc_hir::{Item, ItemKind};
@@ -115,7 +114,6 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
115114
);
116115

117116
let fields = def.variants[variants_size[0].ind].data.fields();
118-
let mut applicability = Applicability::MaybeIncorrect;
119117
if is_copy(cx, ty) || maybe_copy(cx, ty) {
120118
diag.span_note(
121119
item.ident.span,
@@ -129,23 +127,16 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
129127
.map_while(|&(ind, size)| {
130128
if difference > self.maximum_size_difference_allowed {
131129
difference = difference.saturating_sub(size);
132-
Some((
133-
fields[ind].ty.span,
134-
format!(
135-
"Box<{}>",
136-
snippet_with_applicability(
137-
cx,
138-
fields[ind].ty.span,
139-
"..",
140-
&mut applicability
141-
)
142-
.into_owned()
143-
),
144-
))
130+
let sp = fields[ind].ty.span;
131+
Some(vec![
132+
(sp.shrink_to_lo(), "Box<".to_string()),
133+
(sp.shrink_to_hi(), ">".to_string()),
134+
].into_iter())
145135
} else {
146136
None
147137
}
148138
})
139+
.flat_map(|x| x)
149140
.collect();
150141

151142
if !sugg.is_empty() {

src/tools/clippy/tests/ui-toml/enum_variant_size/enum_variant_size.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ LL | | }
1414
= help: to override `-D warnings` add `#[allow(clippy::large_enum_variant)]`
1515
help: consider boxing the large fields to reduce the total size of the enum
1616
|
17-
LL - B([u8; 501]),
18-
LL + B(Box<[u8; 501]>),
19-
|
17+
LL | B(Box<[u8; 501]>),
18+
| ++++ +
2019

2120
error: aborting due to 1 previous error
2221

src/tools/clippy/tests/ui/large_enum_variant.64bit.stderr

+26-39
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ LL | | }
1313
= help: to override `-D warnings` add `#[allow(clippy::large_enum_variant)]`
1414
help: consider boxing the large fields to reduce the total size of the enum
1515
|
16-
LL - B([i32; 8000]),
17-
LL + B(Box<[i32; 8000]>),
18-
|
16+
LL | B(Box<[i32; 8000]>),
17+
| ++++ +
1918

2019
error: large size difference between variants
2120
--> tests/ui/large_enum_variant.rs:35:1
@@ -30,9 +29,8 @@ LL | | }
3029
|
3130
help: consider boxing the large fields to reduce the total size of the enum
3231
|
33-
LL - ContainingLargeEnum(LargeEnum),
34-
LL + ContainingLargeEnum(Box<LargeEnum>),
35-
|
32+
LL | ContainingLargeEnum(Box<LargeEnum>),
33+
| ++++ +
3634

3735
error: large size difference between variants
3836
--> tests/ui/large_enum_variant.rs:40:1
@@ -48,9 +46,8 @@ LL | | }
4846
|
4947
help: consider boxing the large fields to reduce the total size of the enum
5048
|
51-
LL - ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]),
52-
LL + ContainingMoreThanOneField(i32, Box<[i32; 8000]>, Box<[i32; 9500]>),
53-
|
49+
LL | ContainingMoreThanOneField(i32, Box<[i32; 8000]>, Box<[i32; 9500]>),
50+
| ++++ + ++++ +
5451

5552
error: large size difference between variants
5653
--> tests/ui/large_enum_variant.rs:46:1
@@ -65,9 +62,8 @@ LL | | }
6562
|
6663
help: consider boxing the large fields to reduce the total size of the enum
6764
|
68-
LL - StructLikeLarge { x: [i32; 8000], y: i32 },
69-
LL + StructLikeLarge { x: Box<[i32; 8000]>, y: i32 },
70-
|
65+
LL | StructLikeLarge { x: Box<[i32; 8000]>, y: i32 },
66+
| ++++ +
7167

7268
error: large size difference between variants
7369
--> tests/ui/large_enum_variant.rs:51:1
@@ -82,9 +78,8 @@ LL | | }
8278
|
8379
help: consider boxing the large fields to reduce the total size of the enum
8480
|
85-
LL - StructLikeLarge2 { x: [i32; 8000] },
86-
LL + StructLikeLarge2 { x: Box<[i32; 8000]> },
87-
|
81+
LL | StructLikeLarge2 { x: Box<[i32; 8000]> },
82+
| ++++ +
8883

8984
error: large size difference between variants
9085
--> tests/ui/large_enum_variant.rs:67:1
@@ -100,9 +95,8 @@ LL | | }
10095
|
10196
help: consider boxing the large fields to reduce the total size of the enum
10297
|
103-
LL - B([u8; 1255]),
104-
LL + B(Box<[u8; 1255]>),
105-
|
98+
LL | B(Box<[u8; 1255]>),
99+
| ++++ +
106100

107101
error: large size difference between variants
108102
--> tests/ui/large_enum_variant.rs:73:1
@@ -117,9 +111,8 @@ LL | | }
117111
|
118112
help: consider boxing the large fields to reduce the total size of the enum
119113
|
120-
LL - ContainingMoreThanOneField([i32; 8000], [i32; 2], [i32; 9500], [i32; 30]),
121-
LL + ContainingMoreThanOneField(Box<[i32; 8000]>, [i32; 2], Box<[i32; 9500]>, [i32; 30]),
122-
|
114+
LL | ContainingMoreThanOneField(Box<[i32; 8000]>, [i32; 2], Box<[i32; 9500]>, [i32; 30]),
115+
| ++++ + ++++ +
123116

124117
error: large size difference between variants
125118
--> tests/ui/large_enum_variant.rs:78:1
@@ -134,9 +127,8 @@ LL | | }
134127
|
135128
help: consider boxing the large fields to reduce the total size of the enum
136129
|
137-
LL - B(Struct2),
138-
LL + B(Box<Struct2>),
139-
|
130+
LL | B(Box<Struct2>),
131+
| ++++ +
140132

141133
error: large size difference between variants
142134
--> tests/ui/large_enum_variant.rs:83:1
@@ -151,9 +143,8 @@ LL | | }
151143
|
152144
help: consider boxing the large fields to reduce the total size of the enum
153145
|
154-
LL - B(Struct2),
155-
LL + B(Box<Struct2>),
156-
|
146+
LL | B(Box<Struct2>),
147+
| ++++ +
157148

158149
error: large size difference between variants
159150
--> tests/ui/large_enum_variant.rs:88:1
@@ -168,9 +159,8 @@ LL | | }
168159
|
169160
help: consider boxing the large fields to reduce the total size of the enum
170161
|
171-
LL - B(Struct2),
172-
LL + B(Box<Struct2>),
173-
|
162+
LL | B(Box<Struct2>),
163+
| ++++ +
174164

175165
error: large size difference between variants
176166
--> tests/ui/large_enum_variant.rs:103:1
@@ -251,9 +241,8 @@ LL | | }
251241
|
252242
help: consider boxing the large fields to reduce the total size of the enum
253243
|
254-
LL - Large((T, [u8; 512])),
255-
LL + Large(Box<(T, [u8; 512])>),
256-
|
244+
LL | Large(Box<(T, [u8; 512])>),
245+
| ++++ +
257246

258247
error: large size difference between variants
259248
--> tests/ui/large_enum_variant.rs:143:1
@@ -268,9 +257,8 @@ LL | | }
268257
|
269258
help: consider boxing the large fields to reduce the total size of the enum
270259
|
271-
LL - Large([Foo<u64>; 64]),
272-
LL + Large(Box<[Foo<u64>; 64]>),
273-
|
260+
LL | Large(Box<[Foo<u64>; 64]>),
261+
| ++++ +
274262

275263
error: large size difference between variants
276264
--> tests/ui/large_enum_variant.rs:153:1
@@ -285,9 +273,8 @@ LL | | }
285273
|
286274
help: consider boxing the large fields to reduce the total size of the enum
287275
|
288-
LL - Error(PossiblyLargeEnumWithConst<256>),
289-
LL + Error(Box<PossiblyLargeEnumWithConst<256>>),
290-
|
276+
LL | Error(Box<PossiblyLargeEnumWithConst<256>>),
277+
| ++++ +
291278

292279
error: aborting due to 16 previous errors
293280

0 commit comments

Comments
 (0)