-
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.
Extend
useless_conversion
lint with TryFrom
- Loading branch information
Showing
5 changed files
with
112 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![deny(clippy::useless_conversion)] | ||
|
||
use std::convert::TryFrom; | ||
|
||
fn test_generic<T: Copy>(val: T) -> T { | ||
T::try_from(val).unwrap() | ||
} | ||
|
||
fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) { | ||
let _ = U::try_from(val).unwrap(); | ||
} | ||
|
||
fn main() { | ||
test_generic(10i32); | ||
test_generic2::<i32, i32>(10i32); | ||
|
||
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 = TryFrom::try_from("foo".to_string()).unwrap(); | ||
let _ = String::try_from("foo".to_string()).unwrap(); | ||
let _ = String::try_from(format!("A: {:04}", 123)).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,39 @@ | ||
error: Useless conversion to the same type | ||
--> $DIR/useless_conversion_try.rs:6:5 | ||
| | ||
LL | 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:22: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:23: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:24:13 | ||
| | ||
LL | let _ = String::try_from(format!("A: {:04}", 123)).unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider removing `String::try_from()` | ||
|
||
error: aborting due to 4 previous errors | ||
|