Skip to content

Commit

Permalink
Fix FP useless_conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
giraffate committed Jul 25, 2020
1 parent 79f948e commit c81bbd0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
11 changes: 9 additions & 2 deletions clippy_lints/src/useless_conversion.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/useless_conversion.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/useless_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/useless_conversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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")`
Expand Down

0 comments on commit c81bbd0

Please sign in to comment.