about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Nakata <f.seasons017@gmail.com>2020-07-25 23:58:22 +0900
committerTakayuki Nakata <f.seasons017@gmail.com>2020-07-25 23:58:22 +0900
commitc81bbd05b95ae03da9af4e7e25e3784edd039465 (patch)
treebdfae2a134cca6a497b903d93c55185f11ed6ad5
parent79f948ec0a507f8dc663508ce013104847fcc9f3 (diff)
downloadrust-c81bbd05b95ae03da9af4e7e25e3784edd039465.tar.gz
rust-c81bbd05b95ae03da9af4e7e25e3784edd039465.zip
Fix FP `useless_conversion`
Fix #5833.
-rw-r--r--clippy_lints/src/useless_conversion.rs11
-rw-r--r--tests/ui/useless_conversion.fixed9
-rw-r--r--tests/ui/useless_conversion.rs9
-rw-r--r--tests/ui/useless_conversion.stderr14
4 files changed, 34 insertions, 9 deletions
diff --git a/clippy_lints/src/useless_conversion.rs b/clippy_lints/src/useless_conversion.rs
index a48ad3185e9..1bf37632e32 100644
--- a/clippy_lints/src/useless_conversion.rs
+++ b/clippy_lints/src/useless_conversion.rs
@@ -1,6 +1,6 @@
 use crate::utils::{
-    is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite,
-    span_lint_and_help, span_lint_and_sugg,
+    get_parent_expr, is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet,
+    snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg,
 };
 use if_chain::if_chain;
 use rustc_errors::Applicability;
@@ -79,6 +79,13 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
                     }
                 }
                 if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
+                    if let Some(parent_expr) = get_parent_expr(cx, e) {
+                        if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind {
+                            if &*parent_name.ident.as_str() != "into_iter" {
+                                return;
+                            }
+                        }
+                    }
                     let a = cx.typeck_results().expr_ty(e);
                     let b = cx.typeck_results().expr_ty(&args[0]);
                     if TyS::same_type(a, b) {
diff --git a/tests/ui/useless_conversion.fixed b/tests/ui/useless_conversion.fixed
index fdd4bc581f3..813cdaecaa9 100644
--- a/tests/ui/useless_conversion.fixed
+++ b/tests/ui/useless_conversion.fixed
@@ -32,11 +32,20 @@ fn test_issue_3913() -> Result<(), std::io::Error> {
     Ok(())
 }
 
+fn test_issue_5833() -> Result<(), ()> {
+    let text = "foo\r\nbar\n\nbaz\n";
+    let lines = text.lines();
+    if Some("ok") == lines.into_iter().next() {}
+
+    Ok(())
+}
+
 fn main() {
     test_generic(10i32);
     test_generic2::<i32, i32>(10i32);
     test_questionmark().unwrap();
     test_issue_3913().unwrap();
+    test_issue_5833().unwrap();
 
     let _: String = "foo".into();
     let _: String = From::from("foo");
diff --git a/tests/ui/useless_conversion.rs b/tests/ui/useless_conversion.rs
index 4cae745e7c0..540fea23b36 100644
--- a/tests/ui/useless_conversion.rs
+++ b/tests/ui/useless_conversion.rs
@@ -32,11 +32,20 @@ fn test_issue_3913() -> Result<(), std::io::Error> {
     Ok(())
 }
 
+fn test_issue_5833() -> Result<(), ()> {
+    let text = "foo\r\nbar\n\nbaz\n";
+    let lines = text.lines();
+    if Some("ok") == lines.into_iter().next() {}
+
+    Ok(())
+}
+
 fn main() {
     test_generic(10i32);
     test_generic2::<i32, i32>(10i32);
     test_questionmark().unwrap();
     test_issue_3913().unwrap();
+    test_issue_5833().unwrap();
 
     let _: String = "foo".into();
     let _: String = From::from("foo");
diff --git a/tests/ui/useless_conversion.stderr b/tests/ui/useless_conversion.stderr
index 84ec5370278..b958b035452 100644
--- a/tests/ui/useless_conversion.stderr
+++ b/tests/ui/useless_conversion.stderr
@@ -23,43 +23,43 @@ LL |         let _: i32 = 0i32.into();
    |                      ^^^^^^^^^^^ help: consider removing `.into()`: `0i32`
 
 error: useless conversion to the same type
-  --> $DIR/useless_conversion.rs:51:21
+  --> $DIR/useless_conversion.rs:60:21
    |
 LL |     let _: String = "foo".to_string().into();
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()`
 
 error: useless conversion to the same type
-  --> $DIR/useless_conversion.rs:52:21
+  --> $DIR/useless_conversion.rs:61:21
    |
 LL |     let _: String = From::from("foo".to_string());
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()`
 
 error: useless conversion to the same type
-  --> $DIR/useless_conversion.rs:53:13
+  --> $DIR/useless_conversion.rs:62:13
    |
 LL |     let _ = String::from("foo".to_string());
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()`
 
 error: useless conversion to the same type
-  --> $DIR/useless_conversion.rs:54:13
+  --> $DIR/useless_conversion.rs:63:13
    |
 LL |     let _ = String::from(format!("A: {:04}", 123));
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `format!("A: {:04}", 123)`
 
 error: useless conversion to the same type
-  --> $DIR/useless_conversion.rs:55:13
+  --> $DIR/useless_conversion.rs:64:13
    |
 LL |     let _ = "".lines().into_iter();
    |             ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()`
 
 error: useless conversion to the same type
-  --> $DIR/useless_conversion.rs:56:13
+  --> $DIR/useless_conversion.rs:65:13
    |
 LL |     let _ = vec![1, 2, 3].into_iter().into_iter();
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()`
 
 error: useless conversion to the same type
-  --> $DIR/useless_conversion.rs:57:21
+  --> $DIR/useless_conversion.rs:66:21
    |
 LL |     let _: String = format!("Hello {}", "world").into();
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`