Skip to content

Commit efbc9eb

Browse files
committed
Add ControlFlow.{map_break,map_continue} to useless_conversion
1 parent 9a692ec commit efbc9eb

File tree

5 files changed

+93
-46
lines changed

5 files changed

+93
-46
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4984,6 +4984,9 @@ impl Methods {
49844984
manual_inspect::check(cx, expr, m_arg, name, span, &self.msrv);
49854985
crate::useless_conversion::check_function_application(cx, expr, recv, m_arg);
49864986
},
4987+
("map_break" | "map_continue", [m_arg]) => {
4988+
crate::useless_conversion::check_function_application(cx, expr, recv, m_arg);
4989+
},
49874990
("map_or", [def, map]) => {
49884991
option_map_or_none::check(cx, expr, recv, def, map);
49894992
manual_ok_or::check(cx, expr, recv, def, map);

clippy_lints/src/useless_conversion.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_middle::traits::ObligationCause;
1515
use rustc_middle::ty::{self, AdtDef, EarlyBinder, GenericArg, GenericArgsRef, Ty, TypeVisitableExt};
1616
use rustc_session::impl_lint_pass;
17-
use rustc_span::{Span, sym};
17+
use rustc_span::{Span, Symbol, sym};
1818
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
1919

2020
declare_clippy_lint! {
@@ -415,10 +415,24 @@ fn has_eligible_receiver(cx: &LateContext<'_>, recv: &Expr<'_>, expr: &Expr<'_>)
415415
let recv_ty = cx.typeck_results().expr_ty(recv);
416416
if is_inherent_method_call(cx, expr)
417417
&& let Some(recv_ty_defid) = recv_ty.ty_adt_def().map(AdtDef::did)
418-
&& let Some(diag_name) = cx.tcx.get_diagnostic_name(recv_ty_defid)
419-
&& matches!(diag_name, sym::Option | sym::Result)
420418
{
421-
return true;
419+
if let Some(diag_name) = cx.tcx.get_diagnostic_name(recv_ty_defid)
420+
&& matches!(diag_name, sym::Option | sym::Result)
421+
{
422+
return true;
423+
}
424+
425+
// Necessary for `core::ops::control_flow::ControlFlow` until a diagnostic item
426+
// has been added to rustc. See
427+
let def_path = cx.get_def_path(recv_ty_defid);
428+
if def_path
429+
.iter()
430+
.map(Symbol::as_str)
431+
.zip(["core", "ops", "control_flow", "ControlFlow"])
432+
.all(|(sym, s)| sym == s)
433+
{
434+
return true;
435+
}
422436
}
423437
false
424438
}

tests/ui/useless_conversion.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
44
#![allow(static_mut_refs)]
55

6+
use std::ops::ControlFlow;
7+
68
fn test_generic<T: Copy>(val: T) -> T {
79
let _ = val;
810
val
@@ -308,6 +310,13 @@ fn direct_application() {
308310
let _: Result<(), std::io::Error> = test_issue_3913();
309311
//~^ useless_conversion
310312

313+
let c: ControlFlow<()> = ControlFlow::Continue(());
314+
let _: ControlFlow<()> = c;
315+
//~^ useless_conversion
316+
let c: ControlFlow<()> = ControlFlow::Continue(());
317+
let _: ControlFlow<()> = c;
318+
//~^ useless_conversion
319+
311320
struct Absorb;
312321
impl From<()> for Absorb {
313322
fn from(_: ()) -> Self {

tests/ui/useless_conversion.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
44
#![allow(static_mut_refs)]
55

6+
use std::ops::ControlFlow;
7+
68
fn test_generic<T: Copy>(val: T) -> T {
79
let _ = T::from(val);
810
val.into()
@@ -308,6 +310,13 @@ fn direct_application() {
308310
let _: Result<(), std::io::Error> = test_issue_3913().map_err(From::from);
309311
//~^ useless_conversion
310312

313+
let c: ControlFlow<()> = ControlFlow::Continue(());
314+
let _: ControlFlow<()> = c.map_break(Into::into);
315+
//~^ useless_conversion
316+
let c: ControlFlow<()> = ControlFlow::Continue(());
317+
let _: ControlFlow<()> = c.map_continue(Into::into);
318+
//~^ useless_conversion
319+
311320
struct Absorb;
312321
impl From<()> for Absorb {
313322
fn from(_: ()) -> Self {

tests/ui/useless_conversion.stderr

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: useless conversion to the same type: `T`
2-
--> tests/ui/useless_conversion.rs:7:13
2+
--> tests/ui/useless_conversion.rs:9:13
33
|
44
LL | let _ = T::from(val);
55
| ^^^^^^^^^^^^ help: consider removing `T::from()`: `val`
@@ -11,244 +11,256 @@ LL | #![deny(clippy::useless_conversion)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: useless conversion to the same type: `T`
14-
--> tests/ui/useless_conversion.rs:8:5
14+
--> tests/ui/useless_conversion.rs:10:5
1515
|
1616
LL | val.into()
1717
| ^^^^^^^^^^ help: consider removing `.into()`: `val`
1818

1919
error: useless conversion to the same type: `i32`
20-
--> tests/ui/useless_conversion.rs:20:22
20+
--> tests/ui/useless_conversion.rs:22:22
2121
|
2222
LL | let _: i32 = 0i32.into();
2323
| ^^^^^^^^^^^ help: consider removing `.into()`: `0i32`
2424

2525
error: useless conversion to the same type: `std::str::Lines<'_>`
26-
--> tests/ui/useless_conversion.rs:50:22
26+
--> tests/ui/useless_conversion.rs:52:22
2727
|
2828
LL | if Some("ok") == lines.into_iter().next() {}
2929
| ^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `lines`
3030

3131
error: useless conversion to the same type: `std::str::Lines<'_>`
32-
--> tests/ui/useless_conversion.rs:55:21
32+
--> tests/ui/useless_conversion.rs:57:21
3333
|
3434
LL | let mut lines = text.lines().into_iter();
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `text.lines()`
3636

3737
error: useless conversion to the same type: `std::str::Lines<'_>`
38-
--> tests/ui/useless_conversion.rs:61:22
38+
--> tests/ui/useless_conversion.rs:63:22
3939
|
4040
LL | if Some("ok") == text.lines().into_iter().next() {}
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `text.lines()`
4242

4343
error: useless conversion to the same type: `std::ops::Range<i32>`
44-
--> tests/ui/useless_conversion.rs:67:13
44+
--> tests/ui/useless_conversion.rs:69:13
4545
|
4646
LL | let _ = NUMBERS.into_iter().next();
4747
| ^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `NUMBERS`
4848

4949
error: useless conversion to the same type: `std::ops::Range<i32>`
50-
--> tests/ui/useless_conversion.rs:72:17
50+
--> tests/ui/useless_conversion.rs:74:17
5151
|
5252
LL | let mut n = NUMBERS.into_iter();
5353
| ^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `NUMBERS`
5454

5555
error: useless conversion to the same type: `std::string::String`
56-
--> tests/ui/useless_conversion.rs:134:21
56+
--> tests/ui/useless_conversion.rs:136:21
5757
|
5858
LL | let _: String = "foo".to_string().into();
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()`
6060

6161
error: useless conversion to the same type: `std::string::String`
62-
--> tests/ui/useless_conversion.rs:135:21
62+
--> tests/ui/useless_conversion.rs:137:21
6363
|
6464
LL | let _: String = From::from("foo".to_string());
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()`
6666

6767
error: useless conversion to the same type: `std::string::String`
68-
--> tests/ui/useless_conversion.rs:136:13
68+
--> tests/ui/useless_conversion.rs:138:13
6969
|
7070
LL | let _ = String::from("foo".to_string());
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()`
7272

7373
error: useless conversion to the same type: `std::string::String`
74-
--> tests/ui/useless_conversion.rs:137:13
74+
--> tests/ui/useless_conversion.rs:139:13
7575
|
7676
LL | let _ = String::from(format!("A: {:04}", 123));
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `format!("A: {:04}", 123)`
7878

7979
error: useless conversion to the same type: `std::str::Lines<'_>`
80-
--> tests/ui/useless_conversion.rs:138:13
80+
--> tests/ui/useless_conversion.rs:140:13
8181
|
8282
LL | let _ = "".lines().into_iter();
8383
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()`
8484

8585
error: useless conversion to the same type: `std::vec::IntoIter<i32>`
86-
--> tests/ui/useless_conversion.rs:139:13
86+
--> tests/ui/useless_conversion.rs:141:13
8787
|
8888
LL | let _ = vec![1, 2, 3].into_iter().into_iter();
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()`
9090

9191
error: useless conversion to the same type: `std::string::String`
92-
--> tests/ui/useless_conversion.rs:140:21
92+
--> tests/ui/useless_conversion.rs:142:21
9393
|
9494
LL | let _: String = format!("Hello {}", "world").into();
9595
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`
9696

9797
error: useless conversion to the same type: `i32`
98-
--> tests/ui/useless_conversion.rs:145:13
98+
--> tests/ui/useless_conversion.rs:147:13
9999
|
100100
LL | let _ = i32::from(a + b) * 3;
101101
| ^^^^^^^^^^^^^^^^ help: consider removing `i32::from()`: `(a + b)`
102102

103103
error: useless conversion to the same type: `Foo<'a'>`
104-
--> tests/ui/useless_conversion.rs:151:23
104+
--> tests/ui/useless_conversion.rs:153:23
105105
|
106106
LL | let _: Foo<'a'> = s2.into();
107107
| ^^^^^^^^^ help: consider removing `.into()`: `s2`
108108

109109
error: useless conversion to the same type: `Foo<'a'>`
110-
--> tests/ui/useless_conversion.rs:153:13
110+
--> tests/ui/useless_conversion.rs:155:13
111111
|
112112
LL | let _ = Foo::<'a'>::from(s3);
113113
| ^^^^^^^^^^^^^^^^^^^^ help: consider removing `Foo::<'a'>::from()`: `s3`
114114

115115
error: useless conversion to the same type: `std::vec::IntoIter<Foo<'a'>>`
116-
--> tests/ui/useless_conversion.rs:155:13
116+
--> tests/ui/useless_conversion.rs:157:13
117117
|
118118
LL | let _ = vec![s4, s4, s4].into_iter().into_iter();
119119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![s4, s4, s4].into_iter()`
120120

121121
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
122-
--> tests/ui/useless_conversion.rs:187:7
122+
--> tests/ui/useless_conversion.rs:189:7
123123
|
124124
LL | b(vec![1, 2].into_iter());
125125
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]`
126126
|
127127
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
128-
--> tests/ui/useless_conversion.rs:177:13
128+
--> tests/ui/useless_conversion.rs:179:13
129129
|
130130
LL | fn b<T: IntoIterator<Item = i32>>(_: T) {}
131131
| ^^^^^^^^^^^^^^^^^^^^^^^^
132132

133133
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
134-
--> tests/ui/useless_conversion.rs:188:7
134+
--> tests/ui/useless_conversion.rs:190:7
135135
|
136136
LL | c(vec![1, 2].into_iter());
137137
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]`
138138
|
139139
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
140-
--> tests/ui/useless_conversion.rs:178:18
140+
--> tests/ui/useless_conversion.rs:180:18
141141
|
142142
LL | fn c(_: impl IntoIterator<Item = i32>) {}
143143
| ^^^^^^^^^^^^^^^^^^^^^^^^
144144

145145
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
146-
--> tests/ui/useless_conversion.rs:189:7
146+
--> tests/ui/useless_conversion.rs:191:7
147147
|
148148
LL | d(vec![1, 2].into_iter());
149149
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]`
150150
|
151151
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
152-
--> tests/ui/useless_conversion.rs:181:12
152+
--> tests/ui/useless_conversion.rs:183:12
153153
|
154154
LL | T: IntoIterator<Item = i32>,
155155
| ^^^^^^^^^^^^^^^^^^^^^^^^
156156

157157
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
158-
--> tests/ui/useless_conversion.rs:192:7
158+
--> tests/ui/useless_conversion.rs:194:7
159159
|
160160
LL | b(vec![1, 2].into_iter().into_iter());
161161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`s: `vec![1, 2]`
162162
|
163163
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
164-
--> tests/ui/useless_conversion.rs:177:13
164+
--> tests/ui/useless_conversion.rs:179:13
165165
|
166166
LL | fn b<T: IntoIterator<Item = i32>>(_: T) {}
167167
| ^^^^^^^^^^^^^^^^^^^^^^^^
168168

169169
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
170-
--> tests/ui/useless_conversion.rs:193:7
170+
--> tests/ui/useless_conversion.rs:195:7
171171
|
172172
LL | b(vec![1, 2].into_iter().into_iter().into_iter());
173173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`s: `vec![1, 2]`
174174
|
175175
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
176-
--> tests/ui/useless_conversion.rs:177:13
176+
--> tests/ui/useless_conversion.rs:179:13
177177
|
178178
LL | fn b<T: IntoIterator<Item = i32>>(_: T) {}
179179
| ^^^^^^^^^^^^^^^^^^^^^^^^
180180

181181
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
182-
--> tests/ui/useless_conversion.rs:239:24
182+
--> tests/ui/useless_conversion.rs:241:24
183183
|
184184
LL | foo2::<i32, _>([1, 2, 3].into_iter());
185185
| ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]`
186186
|
187187
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
188-
--> tests/ui/useless_conversion.rs:218:12
188+
--> tests/ui/useless_conversion.rs:220:12
189189
|
190190
LL | I: IntoIterator<Item = i32> + Helper<X>,
191191
| ^^^^^^^^^^^^^^^^^^^^^^^^
192192

193193
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
194-
--> tests/ui/useless_conversion.rs:247:14
194+
--> tests/ui/useless_conversion.rs:249:14
195195
|
196196
LL | foo3([1, 2, 3].into_iter());
197197
| ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]`
198198
|
199199
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
200-
--> tests/ui/useless_conversion.rs:227:12
200+
--> tests/ui/useless_conversion.rs:229:12
201201
|
202202
LL | I: IntoIterator<Item = i32>,
203203
| ^^^^^^^^^^^^^^^^^^^^^^^^
204204

205205
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
206-
--> tests/ui/useless_conversion.rs:256:16
206+
--> tests/ui/useless_conversion.rs:258:16
207207
|
208208
LL | S1.foo([1, 2].into_iter());
209209
| ^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2]`
210210
|
211211
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
212-
--> tests/ui/useless_conversion.rs:253:27
212+
--> tests/ui/useless_conversion.rs:255:27
213213
|
214214
LL | pub fn foo<I: IntoIterator>(&self, _: I) {}
215215
| ^^^^^^^^^^^^
216216

217217
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
218-
--> tests/ui/useless_conversion.rs:275:44
218+
--> tests/ui/useless_conversion.rs:277:44
219219
|
220220
LL | v0.into_iter().interleave_shortest(v1.into_iter());
221221
| ^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `v1`
222222
|
223223
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
224-
--> tests/ui/useless_conversion.rs:262:20
224+
--> tests/ui/useless_conversion.rs:264:20
225225
|
226226
LL | J: IntoIterator,
227227
| ^^^^^^^^^^^^
228228

229229
error: useless conversion to the same type: `()`
230-
--> tests/ui/useless_conversion.rs:302:58
230+
--> tests/ui/useless_conversion.rs:304:58
231231
|
232232
LL | let _: Result<(), std::io::Error> = test_issue_3913().map(Into::into);
233233
| ^^^^^^^^^^^^^^^^ help: consider removing
234234

235235
error: useless conversion to the same type: `std::io::Error`
236-
--> tests/ui/useless_conversion.rs:304:58
236+
--> tests/ui/useless_conversion.rs:306:58
237237
|
238238
LL | let _: Result<(), std::io::Error> = test_issue_3913().map_err(Into::into);
239239
| ^^^^^^^^^^^^^^^^^^^^ help: consider removing
240240

241241
error: useless conversion to the same type: `()`
242-
--> tests/ui/useless_conversion.rs:306:58
242+
--> tests/ui/useless_conversion.rs:308:58
243243
|
244244
LL | let _: Result<(), std::io::Error> = test_issue_3913().map(From::from);
245245
| ^^^^^^^^^^^^^^^^ help: consider removing
246246

247247
error: useless conversion to the same type: `std::io::Error`
248-
--> tests/ui/useless_conversion.rs:308:58
248+
--> tests/ui/useless_conversion.rs:310:58
249249
|
250250
LL | let _: Result<(), std::io::Error> = test_issue_3913().map_err(From::from);
251251
| ^^^^^^^^^^^^^^^^^^^^ help: consider removing
252252

253-
error: aborting due to 32 previous errors
253+
error: useless conversion to the same type: `()`
254+
--> tests/ui/useless_conversion.rs:314:31
255+
|
256+
LL | let _: ControlFlow<()> = c.map_break(Into::into);
257+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing
258+
259+
error: useless conversion to the same type: `()`
260+
--> tests/ui/useless_conversion.rs:317:31
261+
|
262+
LL | let _: ControlFlow<()> = c.map_continue(Into::into);
263+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing
264+
265+
error: aborting due to 34 previous errors
254266

0 commit comments

Comments
 (0)