Skip to content

Commit 417d4e6

Browse files
committed
needless_pass_by_value: make verbose suggestion
This reduces the number of snippet manipulations, and shows the new reference in context.
1 parent 35e6057 commit 417d4e6

File tree

3 files changed

+136
-29
lines changed

3 files changed

+136
-29
lines changed

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,10 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
279279
}
280280
}
281281

282-
let inner_input = peel_hir_ty_options(cx, input);
283-
let before_span = input.span.until(inner_input.span);
284-
let after_span = before_span.shrink_to_hi().to(input.span.shrink_to_hi());
285-
diag.span_suggestion(
286-
input.span,
282+
diag.span_suggestion_verbose(
283+
peel_hir_ty_options(cx, input).span.shrink_to_lo(),
287284
"consider taking a reference instead",
288-
format!("{}&{}", snippet(cx, before_span, "_"), snippet(cx, after_span, "_")),
285+
'&',
289286
Applicability::MaybeIncorrect,
290287
);
291288
};

tests/ui/crashes/needless_pass_by_value-w-late-bound.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: this argument is passed by value, but not consumed in the function body
22
--> tests/ui/crashes/needless_pass_by_value-w-late-bound.rs:7:12
33
|
44
LL | fn test(x: Foo<'_>) {}
5-
| ^^^^^^^ help: consider taking a reference instead: `&Foo<'_>`
5+
| ^^^^^^^
66
|
77
help: or consider marking this type as `Copy`
88
--> tests/ui/crashes/needless_pass_by_value-w-late-bound.rs:5:1
@@ -11,6 +11,10 @@ LL | struct Foo<'a>(&'a [(); 100]);
1111
| ^^^^^^^^^^^^^^
1212
= note: `-D clippy::needless-pass-by-value` implied by `-D warnings`
1313
= help: to override `-D warnings` add `#[allow(clippy::needless_pass_by_value)]`
14+
help: consider taking a reference instead
15+
|
16+
LL | fn test(x: &Foo<'_>) {}
17+
| +
1418

1519
error: aborting due to 1 previous error
1620

tests/ui/needless_pass_by_value.stderr

Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,78 @@ error: this argument is passed by value, but not consumed in the function body
1717
--> tests/ui/needless_pass_by_value.rs:35:22
1818
|
1919
LL | fn bar(x: String, y: Wrapper) {
20-
| ^^^^^^^ help: consider taking a reference instead: `&Wrapper`
20+
| ^^^^^^^
21+
|
22+
help: consider taking a reference instead
23+
|
24+
LL | fn bar(x: String, y: &Wrapper) {
25+
| +
2126

2227
error: this argument is passed by value, but not consumed in the function body
2328
--> tests/ui/needless_pass_by_value.rs:44:71
2429
|
2530
LL | fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: V) {
26-
| ^ help: consider taking a reference instead: `&V`
31+
| ^
32+
|
33+
help: consider taking a reference instead
34+
|
35+
LL | fn test_borrow_trait<T: Borrow<str>, U: AsRef<str>, V>(t: T, u: U, v: &V) {
36+
| +
2737

2838
error: this argument is passed by value, but not consumed in the function body
2939
--> tests/ui/needless_pass_by_value.rs:58:18
3040
|
3141
LL | fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
32-
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<Option<&String>>`
42+
| ^^^^^^^^^^^^^^^^^^^^^^
43+
|
44+
help: consider taking a reference instead
45+
|
46+
LL | fn test_match(x: Option<Option<&String>>, y: Option<Option<String>>) {
47+
| +
3348

3449
error: this argument is passed by value, but not consumed in the function body
3550
--> tests/ui/needless_pass_by_value.rs:73:24
3651
|
3752
LL | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
38-
| ^^^^^^^ help: consider taking a reference instead: `&Wrapper`
53+
| ^^^^^^^
54+
|
55+
help: consider taking a reference instead
56+
|
57+
LL | fn test_destructure(x: &Wrapper, y: Wrapper, z: Wrapper) {
58+
| +
3959

4060
error: this argument is passed by value, but not consumed in the function body
4161
--> tests/ui/needless_pass_by_value.rs:73:36
4262
|
4363
LL | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
44-
| ^^^^^^^ help: consider taking a reference instead: `&Wrapper`
64+
| ^^^^^^^
65+
|
66+
help: consider taking a reference instead
67+
|
68+
LL | fn test_destructure(x: Wrapper, y: &Wrapper, z: Wrapper) {
69+
| +
4570

4671
error: this argument is passed by value, but not consumed in the function body
4772
--> tests/ui/needless_pass_by_value.rs:92:49
4873
|
4974
LL | fn test_blanket_ref<T: Foo, S: Serialize>(vals: T, serializable: S) {}
50-
| ^ help: consider taking a reference instead: `&T`
75+
| ^
76+
|
77+
help: consider taking a reference instead
78+
|
79+
LL | fn test_blanket_ref<T: Foo, S: Serialize>(vals: &T, serializable: S) {}
80+
| +
5181

5282
error: this argument is passed by value, but not consumed in the function body
5383
--> tests/ui/needless_pass_by_value.rs:95:18
5484
|
5585
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
56-
| ^^^^^^ help: consider taking a reference instead: `&String`
86+
| ^^^^^^
87+
|
88+
help: consider taking a reference instead
89+
|
90+
LL | fn issue_2114(s: &String, t: String, u: Vec<i32>, v: Vec<i32>) {
91+
| +
5792

5893
error: this argument is passed by value, but not consumed in the function body
5994
--> tests/ui/needless_pass_by_value.rs:95:29
@@ -76,7 +111,12 @@ error: this argument is passed by value, but not consumed in the function body
76111
--> tests/ui/needless_pass_by_value.rs:95:40
77112
|
78113
LL | fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
79-
| ^^^^^^^^ help: consider taking a reference instead: `&Vec<i32>`
114+
| ^^^^^^^^
115+
|
116+
help: consider taking a reference instead
117+
|
118+
LL | fn issue_2114(s: String, t: String, u: &Vec<i32>, v: Vec<i32>) {
119+
| +
80120

81121
error: this argument is passed by value, but not consumed in the function body
82122
--> tests/ui/needless_pass_by_value.rs:95:53
@@ -105,109 +145,175 @@ error: this argument is passed by value, but not consumed in the function body
105145
--> tests/ui/needless_pass_by_value.rs:115:12
106146
|
107147
LL | t: String,
108-
| ^^^^^^ help: consider taking a reference instead: `&String`
148+
| ^^^^^^
149+
|
150+
help: consider taking a reference instead
151+
|
152+
LL | t: &String,
153+
| +
109154

110155
error: this argument is passed by value, but not consumed in the function body
111156
--> tests/ui/needless_pass_by_value.rs:125:23
112157
|
113158
LL | fn baz(&self, uu: U, ss: Self) {}
114-
| ^ help: consider taking a reference instead: `&U`
159+
| ^
160+
|
161+
help: consider taking a reference instead
162+
|
163+
LL | fn baz(&self, uu: &U, ss: Self) {}
164+
| +
115165

116166
error: this argument is passed by value, but not consumed in the function body
117167
--> tests/ui/needless_pass_by_value.rs:125:30
118168
|
119169
LL | fn baz(&self, uu: U, ss: Self) {}
120-
| ^^^^ help: consider taking a reference instead: `&Self`
170+
| ^^^^
171+
|
172+
help: consider taking a reference instead
173+
|
174+
LL | fn baz(&self, uu: U, ss: &Self) {}
175+
| +
121176

122177
error: this argument is passed by value, but not consumed in the function body
123178
--> tests/ui/needless_pass_by_value.rs:149:24
124179
|
125180
LL | fn bar_copy(x: u32, y: CopyWrapper) {
126-
| ^^^^^^^^^^^ help: consider taking a reference instead: `&CopyWrapper`
181+
| ^^^^^^^^^^^
127182
|
128183
help: or consider marking this type as `Copy`
129184
--> tests/ui/needless_pass_by_value.rs:147:1
130185
|
131186
LL | struct CopyWrapper(u32);
132187
| ^^^^^^^^^^^^^^^^^^
188+
help: consider taking a reference instead
189+
|
190+
LL | fn bar_copy(x: u32, y: &CopyWrapper) {
191+
| +
133192

134193
error: this argument is passed by value, but not consumed in the function body
135194
--> tests/ui/needless_pass_by_value.rs:157:29
136195
|
137196
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
138-
| ^^^^^^^^^^^ help: consider taking a reference instead: `&CopyWrapper`
197+
| ^^^^^^^^^^^
139198
|
140199
help: or consider marking this type as `Copy`
141200
--> tests/ui/needless_pass_by_value.rs:147:1
142201
|
143202
LL | struct CopyWrapper(u32);
144203
| ^^^^^^^^^^^^^^^^^^
204+
help: consider taking a reference instead
205+
|
206+
LL | fn test_destructure_copy(x: &CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
207+
| +
145208

146209
error: this argument is passed by value, but not consumed in the function body
147210
--> tests/ui/needless_pass_by_value.rs:157:45
148211
|
149212
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
150-
| ^^^^^^^^^^^ help: consider taking a reference instead: `&CopyWrapper`
213+
| ^^^^^^^^^^^
151214
|
152215
help: or consider marking this type as `Copy`
153216
--> tests/ui/needless_pass_by_value.rs:147:1
154217
|
155218
LL | struct CopyWrapper(u32);
156219
| ^^^^^^^^^^^^^^^^^^
220+
help: consider taking a reference instead
221+
|
222+
LL | fn test_destructure_copy(x: CopyWrapper, y: &CopyWrapper, z: CopyWrapper) {
223+
| +
157224

158225
error: this argument is passed by value, but not consumed in the function body
159226
--> tests/ui/needless_pass_by_value.rs:157:61
160227
|
161228
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
162-
| ^^^^^^^^^^^ help: consider taking a reference instead: `&CopyWrapper`
229+
| ^^^^^^^^^^^
163230
|
164231
help: or consider marking this type as `Copy`
165232
--> tests/ui/needless_pass_by_value.rs:147:1
166233
|
167234
LL | struct CopyWrapper(u32);
168235
| ^^^^^^^^^^^^^^^^^^
236+
help: consider taking a reference instead
237+
|
238+
LL | fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: &CopyWrapper) {
239+
| +
169240

170241
error: this argument is passed by value, but not consumed in the function body
171242
--> tests/ui/needless_pass_by_value.rs:173:40
172243
|
173244
LL | fn some_fun<'b, S: Bar<'b, ()>>(items: S) {}
174-
| ^ help: consider taking a reference instead: `&S`
245+
| ^
246+
|
247+
help: consider taking a reference instead
248+
|
249+
LL | fn some_fun<'b, S: Bar<'b, ()>>(items: &S) {}
250+
| +
175251

176252
error: this argument is passed by value, but not consumed in the function body
177253
--> tests/ui/needless_pass_by_value.rs:179:20
178254
|
179255
LL | fn more_fun(items: impl Club<'static, i32>) {}
180-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>`
256+
| ^^^^^^^^^^^^^^^^^^^^^^^
257+
|
258+
help: consider taking a reference instead
259+
|
260+
LL | fn more_fun(items: &impl Club<'static, i32>) {}
261+
| +
181262

182263
error: this argument is passed by value, but not consumed in the function body
183264
--> tests/ui/needless_pass_by_value.rs:194:24
184265
|
185266
LL | fn option_inner_ref(x: Option<String>) {
186-
| ^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&String>`
267+
| ^^^^^^^^^^^^^^
268+
|
269+
help: consider taking a reference instead
270+
|
271+
LL | fn option_inner_ref(x: Option<&String>) {
272+
| +
187273

188274
error: this argument is passed by value, but not consumed in the function body
189275
--> tests/ui/needless_pass_by_value.rs:204:27
190276
|
191277
LL | fn non_standard_option(x: non_standard::Option<String>) {
192-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&non_standard::Option<String>`
278+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
279+
|
280+
help: consider taking a reference instead
281+
|
282+
LL | fn non_standard_option(x: &non_standard::Option<String>) {
283+
| +
193284

194285
error: this argument is passed by value, but not consumed in the function body
195286
--> tests/ui/needless_pass_by_value.rs:209:22
196287
|
197288
LL | fn option_by_name(x: Option<std::option::Option<core::option::Option<non_standard::Option<String>>>>) {
198-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<std::option::Option<core::option::Option<&non_standard::Option<String>>>>`
289+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
290+
|
291+
help: consider taking a reference instead
292+
|
293+
LL | fn option_by_name(x: Option<std::option::Option<core::option::Option<&non_standard::Option<String>>>>) {
294+
| +
199295

200296
error: this argument is passed by value, but not consumed in the function body
201297
--> tests/ui/needless_pass_by_value.rs:216:18
202298
|
203299
LL | fn non_option(x: OptStr) {
204-
| ^^^^^^ help: consider taking a reference instead: `&OptStr`
300+
| ^^^^^^
301+
|
302+
help: consider taking a reference instead
303+
|
304+
LL | fn non_option(x: &OptStr) {
305+
| +
205306

206307
error: this argument is passed by value, but not consumed in the function body
207308
--> tests/ui/needless_pass_by_value.rs:223:25
208309
|
209310
LL | fn non_option_either(x: Opt<String>) {
210-
| ^^^^^^^^^^^ help: consider taking a reference instead: `&Opt<String>`
311+
| ^^^^^^^^^^^
312+
|
313+
help: consider taking a reference instead
314+
|
315+
LL | fn non_option_either(x: &Opt<String>) {
316+
| +
211317

212318
error: aborting due to 27 previous errors
213319

0 commit comments

Comments
 (0)