-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5631 - ThibsG:ExtendUselessConversion, r=matthiaskrgr
Extend useless conversion This PR extends `useless_conversion` lint with `TryFrom` and `TryInto` fixes: #5344 changelog: Extend `useless_conversion` with `TryFrom` and `TryInto`
- Loading branch information
Showing
6 changed files
with
199 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#![deny(clippy::useless_conversion)] | ||
|
||
use std::convert::{TryFrom, TryInto}; | ||
|
||
fn test_generic<T: Copy>(val: T) -> T { | ||
let _ = T::try_from(val).unwrap(); | ||
val.try_into().unwrap() | ||
} | ||
|
||
fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) { | ||
// ok | ||
let _: i32 = val.try_into().unwrap(); | ||
let _: U = val.try_into().unwrap(); | ||
let _ = U::try_from(val).unwrap(); | ||
} | ||
|
||
fn main() { | ||
test_generic(10i32); | ||
test_generic2::<i32, i32>(10i32); | ||
|
||
let _: String = "foo".try_into().unwrap(); | ||
let _: String = TryFrom::try_from("foo").unwrap(); | ||
let _ = String::try_from("foo").unwrap(); | ||
#[allow(clippy::useless_conversion)] | ||
{ | ||
let _ = String::try_from("foo").unwrap(); | ||
let _: String = "foo".try_into().unwrap(); | ||
} | ||
let _: String = "foo".to_string().try_into().unwrap(); | ||
let _: String = TryFrom::try_from("foo".to_string()).unwrap(); | ||
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
error: useless conversion to the same type | ||
--> $DIR/useless_conversion_try.rs:6:13 | ||
| | ||
LL | let _ = T::try_from(val).unwrap(); | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/useless_conversion_try.rs:1:9 | ||
| | ||
LL | #![deny(clippy::useless_conversion)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= help: consider removing `T::try_from()` | ||
|
||
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 | ||
--> $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 | ||
--> $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 | ||
--> $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 | ||
--> $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 | ||
--> $DIR/useless_conversion_try.rs:33:21 | ||
| | ||
LL | let _: String = format!("Hello {}", "world").try_into().unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider removing `.try_into()` | ||
|
||
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 | ||
|