Fix FP `useless_conversion`

Fix #5833.
This commit is contained in:
Takayuki Nakata 2020-07-25 23:58:22 +09:00
parent 79f948ec0a
commit c81bbd05b9
4 changed files with 34 additions and 9 deletions

View File

@ -1,6 +1,6 @@
use crate::utils::{ use crate::utils::{
is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite, get_parent_expr, is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet,
span_lint_and_help, span_lint_and_sugg, snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg,
}; };
use if_chain::if_chain; use if_chain::if_chain;
use rustc_errors::Applicability; 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 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 a = cx.typeck_results().expr_ty(e);
let b = cx.typeck_results().expr_ty(&args[0]); let b = cx.typeck_results().expr_ty(&args[0]);
if TyS::same_type(a, b) { if TyS::same_type(a, b) {

View File

@ -32,11 +32,20 @@ fn test_issue_3913() -> Result<(), std::io::Error> {
Ok(()) 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() { fn main() {
test_generic(10i32); test_generic(10i32);
test_generic2::<i32, i32>(10i32); test_generic2::<i32, i32>(10i32);
test_questionmark().unwrap(); test_questionmark().unwrap();
test_issue_3913().unwrap(); test_issue_3913().unwrap();
test_issue_5833().unwrap();
let _: String = "foo".into(); let _: String = "foo".into();
let _: String = From::from("foo"); let _: String = From::from("foo");

View File

@ -32,11 +32,20 @@ fn test_issue_3913() -> Result<(), std::io::Error> {
Ok(()) 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() { fn main() {
test_generic(10i32); test_generic(10i32);
test_generic2::<i32, i32>(10i32); test_generic2::<i32, i32>(10i32);
test_questionmark().unwrap(); test_questionmark().unwrap();
test_issue_3913().unwrap(); test_issue_3913().unwrap();
test_issue_5833().unwrap();
let _: String = "foo".into(); let _: String = "foo".into();
let _: String = From::from("foo"); let _: String = From::from("foo");

View File

@ -23,43 +23,43 @@ LL | let _: i32 = 0i32.into();
| ^^^^^^^^^^^ help: consider removing `.into()`: `0i32` | ^^^^^^^^^^^ help: consider removing `.into()`: `0i32`
error: useless conversion to the same type 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(); LL | let _: String = "foo".to_string().into();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()` | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()`
error: useless conversion to the same type 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()); LL | let _: String = From::from("foo".to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()`
error: useless conversion to the same type 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()); LL | let _ = String::from("foo".to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()`
error: useless conversion to the same type 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)); LL | let _ = String::from(format!("A: {:04}", 123));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `format!("A: {:04}", 123)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `format!("A: {:04}", 123)`
error: useless conversion to the same type error: useless conversion to the same type
--> $DIR/useless_conversion.rs:55:13 --> $DIR/useless_conversion.rs:64:13
| |
LL | let _ = "".lines().into_iter(); LL | let _ = "".lines().into_iter();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()`
error: useless conversion to the same type 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(); LL | let _ = vec![1, 2, 3].into_iter().into_iter();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()`
error: useless conversion to the same type 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(); LL | let _: String = format!("Hello {}", "world").into();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`