Auto merge of #5857 - tmiasko:try-err-poll, r=matthiaskrgr

try_err: Consider Try impl for Poll when generating suggestions

There are two different implementation of `Try` trait for `Poll` type:
`Poll<Result<T, E>>` and `Poll<Option<Result<T, E>>>`. Take them into
account when generating suggestions.

For example, for `Err(e)?` suggest either `return Poll::Ready(Err(e))` or
`return Poll::Ready(Some(Err(e)))` as appropriate.

Fixes #5855

changelog: try_err: Consider Try impl for Poll when generating suggestions
This commit is contained in:
bors 2020-08-05 08:43:37 +00:00
commit 3d7e3fdffd
5 changed files with 155 additions and 31 deletions

View file

@ -6,6 +6,9 @@
#[macro_use]
extern crate macro_rules;
use std::io;
use std::task::Poll;
// Tests that a simple case works
// Should flag `Err(err)?`
pub fn basic_test() -> Result<i32, i32> {
@ -104,3 +107,21 @@ pub fn macro_inside(fail: bool) -> Result<i32, String> {
}
Ok(0)
}
pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
if n == 0 {
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()))
} else if n == 1 {
return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, "error")))
};
Poll::Ready(Ok(n))
}
pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
if !ready {
return Poll::Ready(Some(Err(io::ErrorKind::NotFound.into())))
}
Poll::Ready(None)
}

View file

@ -6,6 +6,9 @@
#[macro_use]
extern crate macro_rules;
use std::io;
use std::task::Poll;
// Tests that a simple case works
// Should flag `Err(err)?`
pub fn basic_test() -> Result<i32, i32> {
@ -104,3 +107,21 @@ pub fn macro_inside(fail: bool) -> Result<i32, String> {
}
Ok(0)
}
pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
if n == 0 {
Err(io::ErrorKind::WriteZero)?
} else if n == 1 {
Err(io::Error::new(io::ErrorKind::InvalidInput, "error"))?
};
Poll::Ready(Ok(n))
}
pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
if !ready {
Err(io::ErrorKind::NotFound)?
}
Poll::Ready(None)
}

View file

@ -1,5 +1,5 @@
error: returning an `Err(_)` with the `?` operator
--> $DIR/try_err.rs:15:9
--> $DIR/try_err.rs:18:9
|
LL | Err(err)?;
| ^^^^^^^^^ help: try this: `return Err(err)`
@ -11,28 +11,46 @@ LL | #![deny(clippy::try_err)]
| ^^^^^^^^^^^^^^^
error: returning an `Err(_)` with the `?` operator
--> $DIR/try_err.rs:25:9
--> $DIR/try_err.rs:28:9
|
LL | Err(err)?;
| ^^^^^^^^^ help: try this: `return Err(err.into())`
error: returning an `Err(_)` with the `?` operator
--> $DIR/try_err.rs:45:17
--> $DIR/try_err.rs:48:17
|
LL | Err(err)?;
| ^^^^^^^^^ help: try this: `return Err(err)`
error: returning an `Err(_)` with the `?` operator
--> $DIR/try_err.rs:64:17
--> $DIR/try_err.rs:67:17
|
LL | Err(err)?;
| ^^^^^^^^^ help: try this: `return Err(err.into())`
error: returning an `Err(_)` with the `?` operator
--> $DIR/try_err.rs:103:9
--> $DIR/try_err.rs:106:9
|
LL | Err(foo!())?;
| ^^^^^^^^^^^^ help: try this: `return Err(foo!())`
error: aborting due to 5 previous errors
error: returning an `Err(_)` with the `?` operator
--> $DIR/try_err.rs:113:9
|
LL | Err(io::ErrorKind::WriteZero)?
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Err(io::ErrorKind::WriteZero.into()))`
error: returning an `Err(_)` with the `?` operator
--> $DIR/try_err.rs:115:9
|
LL | Err(io::Error::new(io::ErrorKind::InvalidInput, "error"))?
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, "error")))`
error: returning an `Err(_)` with the `?` operator
--> $DIR/try_err.rs:123:9
|
LL | Err(io::ErrorKind::NotFound)?
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Some(Err(io::ErrorKind::NotFound.into())))`
error: aborting due to 8 previous errors