Skip to content

Commit b5762a3

Browse files
committed
useless_conversion: use multipart suggestion to make adjustments more visible
1 parent 4630916 commit b5762a3

File tree

3 files changed

+69
-31
lines changed

3 files changed

+69
-31
lines changed
12 KB
Binary file not shown.

clippy_lints/src/useless_conversion.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
2-
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_context};
2+
use clippy_utils::source::{snippet, snippet_with_context};
33
use clippy_utils::sugg::{DiagExt as _, Sugg};
44
use clippy_utils::ty::{is_copy, is_type_diagnostic_item, same_type_and_consts};
55
use clippy_utils::{
@@ -252,30 +252,25 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
252252
// ^^^
253253
let (into_iter_recv, depth) = into_iter_deep_call(cx, into_iter_recv);
254254

255-
// The receiver may not implement `IntoIterator`, it may have been
256-
// auto-dereferenced.
257-
let adjustments = adjustments(cx, into_iter_recv);
258-
259-
let plural = if depth == 0 { "" } else { "s" };
260-
let mut applicability = Applicability::MachineApplicable;
261-
let sugg = snippet_with_applicability(
262-
cx,
263-
into_iter_recv.span.source_callsite(),
264-
"<expr>",
265-
&mut applicability,
266-
);
267-
let sugg = format!("{adjustments}{sugg}");
268255
span_lint_and_then(
269256
cx,
270257
USELESS_CONVERSION,
271258
e.span,
272259
"explicit call to `.into_iter()` in function argument accepting `IntoIterator`",
273260
|diag| {
274-
diag.span_suggestion(
275-
e.span,
261+
let receiver_span = into_iter_recv.span.source_callsite();
262+
let adjustments = adjustments(cx, into_iter_recv);
263+
let mut sugg = if adjustments.is_empty() {
264+
vec![]
265+
} else {
266+
vec![(receiver_span.shrink_to_lo(), adjustments)]
267+
};
268+
let plural = if depth == 0 { "" } else { "s" };
269+
sugg.push((e.span.with_lo(receiver_span.hi()), String::new()));
270+
diag.multipart_suggestion(
276271
format!("consider removing the `.into_iter()`{plural}"),
277272
sugg,
278-
applicability,
273+
Applicability::MachineApplicable,
279274
);
280275
diag.span_note(span, "this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`");
281276
},

tests/ui/useless_conversion.stderr

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
122122
--> tests/ui/useless_conversion.rs:189:7
123123
|
124124
LL | b(vec![1, 2].into_iter());
125-
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]`
125+
| ^^^^^^^^^^------------
126+
| |
127+
| help: consider removing the `.into_iter()`
126128
|
127129
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
128130
--> tests/ui/useless_conversion.rs:179:13
@@ -134,7 +136,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
134136
--> tests/ui/useless_conversion.rs:190:7
135137
|
136138
LL | c(vec![1, 2].into_iter());
137-
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]`
139+
| ^^^^^^^^^^------------
140+
| |
141+
| help: consider removing the `.into_iter()`
138142
|
139143
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
140144
--> tests/ui/useless_conversion.rs:180:18
@@ -146,7 +150,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
146150
--> tests/ui/useless_conversion.rs:191:7
147151
|
148152
LL | d(vec![1, 2].into_iter());
149-
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]`
153+
| ^^^^^^^^^^------------
154+
| |
155+
| help: consider removing the `.into_iter()`
150156
|
151157
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
152158
--> tests/ui/useless_conversion.rs:183:12
@@ -158,7 +164,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
158164
--> tests/ui/useless_conversion.rs:194:7
159165
|
160166
LL | b(vec![1, 2].into_iter().into_iter());
161-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`s: `vec![1, 2]`
167+
| ^^^^^^^^^^------------------------
168+
| |
169+
| help: consider removing the `.into_iter()`s
162170
|
163171
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
164172
--> tests/ui/useless_conversion.rs:179:13
@@ -170,7 +178,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
170178
--> tests/ui/useless_conversion.rs:195:7
171179
|
172180
LL | b(vec![1, 2].into_iter().into_iter().into_iter());
173-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`s: `vec![1, 2]`
181+
| ^^^^^^^^^^------------------------------------
182+
| |
183+
| help: consider removing the `.into_iter()`s
174184
|
175185
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
176186
--> tests/ui/useless_conversion.rs:179:13
@@ -182,7 +192,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
182192
--> tests/ui/useless_conversion.rs:241:24
183193
|
184194
LL | foo2::<i32, _>([1, 2, 3].into_iter());
185-
| ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]`
195+
| ^^^^^^^^^------------
196+
| |
197+
| help: consider removing the `.into_iter()`
186198
|
187199
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
188200
--> tests/ui/useless_conversion.rs:220:12
@@ -194,7 +206,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
194206
--> tests/ui/useless_conversion.rs:249:14
195207
|
196208
LL | foo3([1, 2, 3].into_iter());
197-
| ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]`
209+
| ^^^^^^^^^------------
210+
| |
211+
| help: consider removing the `.into_iter()`
198212
|
199213
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
200214
--> tests/ui/useless_conversion.rs:229:12
@@ -206,7 +220,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
206220
--> tests/ui/useless_conversion.rs:258:16
207221
|
208222
LL | S1.foo([1, 2].into_iter());
209-
| ^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2]`
223+
| ^^^^^^------------
224+
| |
225+
| help: consider removing the `.into_iter()`
210226
|
211227
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
212228
--> tests/ui/useless_conversion.rs:255:27
@@ -218,7 +234,9 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
218234
--> tests/ui/useless_conversion.rs:277:44
219235
|
220236
LL | v0.into_iter().interleave_shortest(v1.into_iter());
221-
| ^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `v1`
237+
| ^^------------
238+
| |
239+
| help: consider removing the `.into_iter()`
222240
|
223241
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
224242
--> tests/ui/useless_conversion.rs:264:20
@@ -278,25 +296,35 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
278296
--> tests/ui/useless_conversion.rs:356:32
279297
|
280298
LL | let _ = iter.chain(self.my_field.into_iter());
281-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `&self.my_field`
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
282300
|
283301
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
284302
--> /home/sam/.rustup/toolchains/nightly-2025-01-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:471:12
285303
|
286304
LL | U: IntoIterator<Item = Self::Item>,
287305
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
306+
help: consider removing the `.into_iter()`
307+
|
308+
LL - let _ = iter.chain(self.my_field.into_iter());
309+
LL + let _ = iter.chain(&self.my_field);
310+
|
288311

289312
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
290313
--> tests/ui/useless_conversion.rs:365:32
291314
|
292315
LL | let _ = iter.chain(self.my_field.into_iter());
293-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `&mut self.my_field`
316+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
294317
|
295318
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
296319
--> /home/sam/.rustup/toolchains/nightly-2025-01-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:471:12
297320
|
298321
LL | U: IntoIterator<Item = Self::Item>,
299322
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
323+
help: consider removing the `.into_iter()`
324+
|
325+
LL - let _ = iter.chain(self.my_field.into_iter());
326+
LL + let _ = iter.chain(&mut self.my_field);
327+
|
300328

301329
error: the following explicit lifetimes could be elided: 'a
302330
--> tests/ui/useless_conversion.rs:369:27
@@ -316,37 +344,52 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera
316344
--> tests/ui/useless_conversion.rs:375:32
317345
|
318346
LL | let _ = iter.chain(self.my_field.into_iter());
319-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `*self.my_field`
347+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
320348
|
321349
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
322350
--> /home/sam/.rustup/toolchains/nightly-2025-01-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:471:12
323351
|
324352
LL | U: IntoIterator<Item = Self::Item>,
325353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
354+
help: consider removing the `.into_iter()`
355+
|
356+
LL - let _ = iter.chain(self.my_field.into_iter());
357+
LL + let _ = iter.chain(*self.my_field);
358+
|
326359

327360
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
328361
--> tests/ui/useless_conversion.rs:385:32
329362
|
330363
LL | let _ = iter.chain(self.my_field.into_iter());
331-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `&*self.my_field`
364+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
332365
|
333366
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
334367
--> /home/sam/.rustup/toolchains/nightly-2025-01-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:471:12
335368
|
336369
LL | U: IntoIterator<Item = Self::Item>,
337370
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
371+
help: consider removing the `.into_iter()`
372+
|
373+
LL - let _ = iter.chain(self.my_field.into_iter());
374+
LL + let _ = iter.chain(&*self.my_field);
375+
|
338376

339377
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
340378
--> tests/ui/useless_conversion.rs:395:32
341379
|
342380
LL | let _ = iter.chain(self.my_field.into_iter());
343-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `&mut *self.my_field`
381+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
344382
|
345383
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
346384
--> /home/sam/.rustup/toolchains/nightly-2025-01-09-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:471:12
347385
|
348386
LL | U: IntoIterator<Item = Self::Item>,
349387
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
388+
help: consider removing the `.into_iter()`
389+
|
390+
LL - let _ = iter.chain(self.my_field.into_iter());
391+
LL + let _ = iter.chain(&mut *self.my_field);
392+
|
350393

351394
error: aborting due to 42 previous errors
352395

0 commit comments

Comments
 (0)