Skip to content

Commit

Permalink
Add test cases for broader coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed May 26, 2020
1 parent 705bfdc commit 1801841
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
10 changes: 5 additions & 5 deletions clippy_lints/src/useless_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
cx,
USELESS_CONVERSION,
e.span,
"Useless conversion to the same type",
"useless conversion to the same type",
"consider removing `.into()`",
sugg,
Applicability::MachineApplicable, // snippet
Expand All @@ -87,7 +87,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
cx,
USELESS_CONVERSION,
e.span,
"Useless conversion to the same type",
"useless conversion to the same type",
"consider removing `.into_iter()`",
sugg,
Applicability::MachineApplicable, // snippet
Expand All @@ -108,7 +108,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
cx,
USELESS_CONVERSION,
e.span,
"Useless conversion to the same type",
"useless conversion to the same type",
None,
"consider removing `.try_into()`",
);
Expand Down Expand Up @@ -139,7 +139,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
cx,
USELESS_CONVERSION,
e.span,
"Useless conversion to the same type",
"useless conversion to the same type",
None,
&hint,
);
Expand All @@ -158,7 +158,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
cx,
USELESS_CONVERSION,
e.span,
"Useless conversion to the same type",
"useless conversion to the same type",
&sugg_msg,
sugg,
Applicability::MachineApplicable, // snippet
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/useless_conversion.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion.rs:6:13
|
LL | let _ = T::from(val);
Expand All @@ -10,55 +10,55 @@ note: the lint level is defined here
LL | #![deny(clippy::useless_conversion)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion.rs:7:5
|
LL | val.into()
| ^^^^^^^^^^ help: consider removing `.into()`: `val`

error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion.rs:19:22
|
LL | let _: i32 = 0i32.into();
| ^^^^^^^^^^^ 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
|
LL | let _: String = "foo".to_string().into();
| ^^^^^^^^^^^^^^^^^^^^^^^^ 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
|
LL | let _: String = 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
|
LL | let _ = 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
|
LL | let _ = 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
|
LL | let _ = "".lines().into_iter();
| ^^^^^^^^^^^^^^^^^^^^^^ 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
|
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
error: useless conversion to the same type
--> $DIR/useless_conversion.rs:57:21
|
LL | let _: String = format!("Hello {}", "world").into();
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/useless_conversion_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ fn main() {
let _ = String::try_from("foo".to_string()).unwrap();
let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
let _: String = format!("Hello {}", "world").try_into().unwrap();
let _: String = "".to_owned().try_into().unwrap();
let _: String = match String::from("_").try_into() {
Ok(a) => a,
Err(_) => "".into(),
};
// FIXME this is a false negative
#[allow(clippy::cmp_owned)]
if String::from("a") == TryInto::<String>::try_into(String::from("a")).unwrap() {}
}
32 changes: 24 additions & 8 deletions tests/ui/useless_conversion_try.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:6:13
|
LL | let _ = T::try_from(val).unwrap();
Expand All @@ -11,53 +11,69 @@ LL | #![deny(clippy::useless_conversion)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: consider removing `T::try_from()`

error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:7:5
|
LL | val.try_into().unwrap()
| ^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:29:21
|
LL | let _: String = "foo".to_string().try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:30:21
|
LL | let _: String = TryFrom::try_from("foo".to_string()).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `TryFrom::try_from()`

error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:31:13
|
LL | let _ = String::try_from("foo".to_string()).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `String::try_from()`

error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:32:13
|
LL | let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `String::try_from()`

error: Useless conversion to the same type
error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:33:21
|
LL | let _: String = format!("Hello {}", "world").try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: aborting due to 7 previous errors
error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:34:21
|
LL | let _: String = "".to_owned().try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: useless conversion to the same type
--> $DIR/useless_conversion_try.rs:35:27
|
LL | let _: String = match String::from("_").try_into() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: aborting due to 9 previous errors

0 comments on commit 1801841

Please sign in to comment.