-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove parent fn call check, make
.try_into().unwrap()
- Loading branch information
Showing
6 changed files
with
131 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#![warn(clippy::unnecessary_fallible_conversions)] | ||
|
||
fn main() { | ||
let _: i64 = 0i32.into(); | ||
let _: i64 = 0i32.into(); | ||
} |
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 |
---|---|---|
@@ -1,43 +1,6 @@ | ||
//@aux-build:proc_macros.rs | ||
//@no-rustfix | ||
#![warn(clippy::unnecessary_fallible_conversions)] | ||
|
||
extern crate proc_macros; | ||
|
||
struct Foo; | ||
impl TryFrom<i32> for Foo { | ||
type Error = (); | ||
fn try_from(_: i32) -> Result<Self, Self::Error> { | ||
Ok(Foo) | ||
} | ||
} | ||
impl From<i64> for Foo { | ||
fn from(_: i64) -> Self { | ||
Foo | ||
} | ||
} | ||
|
||
fn main() { | ||
// `Foo` only implements `TryFrom<i32>` and not `From<i32>`, so don't lint | ||
let _: Result<Foo, _> = 0i32.try_into(); | ||
let _: Result<Foo, _> = i32::try_into(0i32); | ||
let _: Result<Foo, _> = Foo::try_from(0i32); | ||
|
||
// ... it does impl From<i64> however | ||
let _: Result<Foo, _> = 0i64.try_into(); | ||
//~^ ERROR: calling `TryInto::try_into` when `Into` could be used | ||
let _: Result<Foo, _> = i64::try_into(0i64); | ||
//~^ ERROR: calling `TryInto::try_into` when `Into` could be used | ||
let _: Result<Foo, _> = Foo::try_from(0i64); | ||
//~^ ERROR: calling `TryFrom::try_from` when `From` could be used | ||
|
||
let _: Result<i64, _> = 0i32.try_into(); | ||
//~^ ERROR: calling `TryInto::try_into` when `Into` could be used | ||
let _: Result<i64, _> = i32::try_into(0i32); | ||
//~^ ERROR: calling `TryInto::try_into` when `Into` could be used | ||
let _: Result<i64, _> = <_>::try_from(0i32); | ||
//~^ ERROR: calling `TryFrom::try_from` when `From` could be used | ||
|
||
// From a macro | ||
let _: Result<i64, _> = proc_macros::external!(0i32).try_into(); | ||
let _: i64 = 0i32.try_into().unwrap(); | ||
let _: i64 = 0i32.try_into().expect("can't happen"); | ||
} |
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 |
---|---|---|
@@ -1,41 +1,17 @@ | ||
error: calling `TryInto::try_into` when `Into` could be used | ||
--> $DIR/unnecessary_fallible_conversions.rs:27:34 | ||
--> $DIR/unnecessary_fallible_conversions.rs:4:23 | ||
| | ||
LL | let _: Result<Foo, _> = 0i64.try_into(); | ||
| ^^^^^^^^ help: use: `into` | ||
LL | let _: i64 = 0i32.try_into().unwrap(); | ||
| ^^^^^^^^^^^^^^^^^^^ help: use: `into()` | ||
| | ||
= note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]` | ||
|
||
error: calling `TryInto::try_into` when `Into` could be used | ||
--> $DIR/unnecessary_fallible_conversions.rs:29:29 | ||
--> $DIR/unnecessary_fallible_conversions.rs:5:23 | ||
| | ||
LL | let _: Result<Foo, _> = i64::try_into(0i64); | ||
| ^^^^^^^^^^^^^ help: use: `Into::into` | ||
LL | let _: i64 = 0i32.try_into().expect("can't happen"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `into()` | ||
|
||
error: calling `TryFrom::try_from` when `From` could be used | ||
--> $DIR/unnecessary_fallible_conversions.rs:31:29 | ||
| | ||
LL | let _: Result<Foo, _> = Foo::try_from(0i64); | ||
| ^^^^^^^^^^^^^ help: use: `From::from` | ||
|
||
error: calling `TryInto::try_into` when `Into` could be used | ||
--> $DIR/unnecessary_fallible_conversions.rs:34:34 | ||
| | ||
LL | let _: Result<i64, _> = 0i32.try_into(); | ||
| ^^^^^^^^ help: use: `into` | ||
|
||
error: calling `TryInto::try_into` when `Into` could be used | ||
--> $DIR/unnecessary_fallible_conversions.rs:36:29 | ||
| | ||
LL | let _: Result<i64, _> = i32::try_into(0i32); | ||
| ^^^^^^^^^^^^^ help: use: `Into::into` | ||
|
||
error: calling `TryFrom::try_from` when `From` could be used | ||
--> $DIR/unnecessary_fallible_conversions.rs:38:29 | ||
| | ||
LL | let _: Result<i64, _> = <_>::try_from(0i32); | ||
| ^^^^^^^^^^^^^ help: use: `From::from` | ||
|
||
error: aborting due to 6 previous errors | ||
error: aborting due to 2 previous errors | ||
|
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,43 @@ | ||
//@aux-build:proc_macros.rs | ||
//@no-rustfix | ||
#![warn(clippy::unnecessary_fallible_conversions)] | ||
|
||
extern crate proc_macros; | ||
|
||
struct Foo; | ||
impl TryFrom<i32> for Foo { | ||
type Error = (); | ||
fn try_from(_: i32) -> Result<Self, Self::Error> { | ||
Ok(Foo) | ||
} | ||
} | ||
impl From<i64> for Foo { | ||
fn from(_: i64) -> Self { | ||
Foo | ||
} | ||
} | ||
|
||
fn main() { | ||
// `Foo` only implements `TryFrom<i32>` and not `From<i32>`, so don't lint | ||
let _: Result<Foo, _> = 0i32.try_into(); | ||
let _: Result<Foo, _> = i32::try_into(0i32); | ||
let _: Result<Foo, _> = Foo::try_from(0i32); | ||
|
||
// ... it does impl From<i64> however | ||
let _: Result<Foo, _> = 0i64.try_into(); | ||
//~^ ERROR: calling `TryInto::try_into` when `Into` could be used | ||
let _: Result<Foo, _> = i64::try_into(0i64); | ||
//~^ ERROR: calling `TryInto::try_into` when `Into` could be used | ||
let _: Result<Foo, _> = Foo::try_from(0i64); | ||
//~^ ERROR: calling `TryFrom::try_from` when `From` could be used | ||
|
||
let _: Result<i64, _> = 0i32.try_into(); | ||
//~^ ERROR: calling `TryInto::try_into` when `Into` could be used | ||
let _: Result<i64, _> = i32::try_into(0i32); | ||
//~^ ERROR: calling `TryInto::try_into` when `Into` could be used | ||
let _: Result<i64, _> = <_>::try_from(0i32); | ||
//~^ ERROR: calling `TryFrom::try_from` when `From` could be used | ||
|
||
// From a macro | ||
let _: Result<i64, _> = proc_macros::external!(0i32).try_into(); | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/ui/unnecessary_fallible_conversions_unfixable.stderr
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,41 @@ | ||
error: calling `TryInto::try_into` when `Into` could be used | ||
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:27:34 | ||
| | ||
LL | let _: Result<Foo, _> = 0i64.try_into(); | ||
| ^^^^^^^^ help: use: `into` | ||
| | ||
= note: `-D clippy::unnecessary-fallible-conversions` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_fallible_conversions)]` | ||
|
||
error: calling `TryInto::try_into` when `Into` could be used | ||
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:29:29 | ||
| | ||
LL | let _: Result<Foo, _> = i64::try_into(0i64); | ||
| ^^^^^^^^^^^^^ help: use: `Into::into` | ||
|
||
error: calling `TryFrom::try_from` when `From` could be used | ||
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:31:29 | ||
| | ||
LL | let _: Result<Foo, _> = Foo::try_from(0i64); | ||
| ^^^^^^^^^^^^^ help: use: `From::from` | ||
|
||
error: calling `TryInto::try_into` when `Into` could be used | ||
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:34:34 | ||
| | ||
LL | let _: Result<i64, _> = 0i32.try_into(); | ||
| ^^^^^^^^ help: use: `into` | ||
|
||
error: calling `TryInto::try_into` when `Into` could be used | ||
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:36:29 | ||
| | ||
LL | let _: Result<i64, _> = i32::try_into(0i32); | ||
| ^^^^^^^^^^^^^ help: use: `Into::into` | ||
|
||
error: calling `TryFrom::try_from` when `From` could be used | ||
--> $DIR/unnecessary_fallible_conversions_unfixable.rs:38:29 | ||
| | ||
LL | let _: Result<i64, _> = <_>::try_from(0i32); | ||
| ^^^^^^^^^^^^^ help: use: `From::from` | ||
|
||
error: aborting due to 6 previous errors | ||
|