Skip to content

Commit da5a6fb

Browse files
committed
Auto merge of #5845 - giraffate:fix_fp_useless_conversion, r=yaahc
Fix FP `useless_conversion` Fix #5833. changelog: none
2 parents 79f948e + c81bbd0 commit da5a6fb

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

clippy_lints/src/useless_conversion.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
2-
is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite,
3-
span_lint_and_help, span_lint_and_sugg,
2+
get_parent_expr, is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet,
3+
snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg,
44
};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
@@ -79,6 +79,13 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
7979
}
8080
}
8181
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
82+
if let Some(parent_expr) = get_parent_expr(cx, e) {
83+
if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind {
84+
if &*parent_name.ident.as_str() != "into_iter" {
85+
return;
86+
}
87+
}
88+
}
8289
let a = cx.typeck_results().expr_ty(e);
8390
let b = cx.typeck_results().expr_ty(&args[0]);
8491
if TyS::same_type(a, b) {

tests/ui/useless_conversion.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ fn test_issue_3913() -> Result<(), std::io::Error> {
3232
Ok(())
3333
}
3434

35+
fn test_issue_5833() -> Result<(), ()> {
36+
let text = "foo\r\nbar\n\nbaz\n";
37+
let lines = text.lines();
38+
if Some("ok") == lines.into_iter().next() {}
39+
40+
Ok(())
41+
}
42+
3543
fn main() {
3644
test_generic(10i32);
3745
test_generic2::<i32, i32>(10i32);
3846
test_questionmark().unwrap();
3947
test_issue_3913().unwrap();
48+
test_issue_5833().unwrap();
4049

4150
let _: String = "foo".into();
4251
let _: String = From::from("foo");

tests/ui/useless_conversion.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ fn test_issue_3913() -> Result<(), std::io::Error> {
3232
Ok(())
3333
}
3434

35+
fn test_issue_5833() -> Result<(), ()> {
36+
let text = "foo\r\nbar\n\nbaz\n";
37+
let lines = text.lines();
38+
if Some("ok") == lines.into_iter().next() {}
39+
40+
Ok(())
41+
}
42+
3543
fn main() {
3644
test_generic(10i32);
3745
test_generic2::<i32, i32>(10i32);
3846
test_questionmark().unwrap();
3947
test_issue_3913().unwrap();
48+
test_issue_5833().unwrap();
4049

4150
let _: String = "foo".into();
4251
let _: String = From::from("foo");

tests/ui/useless_conversion.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,43 @@ LL | let _: i32 = 0i32.into();
2323
| ^^^^^^^^^^^ help: consider removing `.into()`: `0i32`
2424

2525
error: useless conversion to the same type
26-
--> $DIR/useless_conversion.rs:51:21
26+
--> $DIR/useless_conversion.rs:60:21
2727
|
2828
LL | let _: String = "foo".to_string().into();
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()`
3030

3131
error: useless conversion to the same type
32-
--> $DIR/useless_conversion.rs:52:21
32+
--> $DIR/useless_conversion.rs:61:21
3333
|
3434
LL | let _: String = From::from("foo".to_string());
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()`
3636

3737
error: useless conversion to the same type
38-
--> $DIR/useless_conversion.rs:53:13
38+
--> $DIR/useless_conversion.rs:62:13
3939
|
4040
LL | let _ = String::from("foo".to_string());
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()`
4242

4343
error: useless conversion to the same type
44-
--> $DIR/useless_conversion.rs:54:13
44+
--> $DIR/useless_conversion.rs:63:13
4545
|
4646
LL | let _ = String::from(format!("A: {:04}", 123));
4747
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `format!("A: {:04}", 123)`
4848

4949
error: useless conversion to the same type
50-
--> $DIR/useless_conversion.rs:55:13
50+
--> $DIR/useless_conversion.rs:64:13
5151
|
5252
LL | let _ = "".lines().into_iter();
5353
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()`
5454

5555
error: useless conversion to the same type
56-
--> $DIR/useless_conversion.rs:56:13
56+
--> $DIR/useless_conversion.rs:65:13
5757
|
5858
LL | let _ = vec![1, 2, 3].into_iter().into_iter();
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()`
6060

6161
error: useless conversion to the same type
62-
--> $DIR/useless_conversion.rs:57:21
62+
--> $DIR/useless_conversion.rs:66:21
6363
|
6464
LL | let _: String = format!("Hello {}", "world").into();
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`

0 commit comments

Comments
 (0)