Skip to content

Commit 3d7e3fd

Browse files
committed
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
2 parents 2ceb8c6 + e967710 commit 3d7e3fd

File tree

5 files changed

+155
-31
lines changed

5 files changed

+155
-31
lines changed

clippy_lints/src/try_err.rs

+88-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
use crate::utils::{match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg};
1+
use crate::utils::{
2+
is_type_diagnostic_item, match_def_path, match_qpath, paths, snippet, snippet_with_macro_callsite,
3+
span_lint_and_sugg,
4+
};
25
use if_chain::if_chain;
36
use rustc_errors::Applicability;
4-
use rustc_hir::{Arm, Expr, ExprKind, MatchSource};
7+
use rustc_hir::{Expr, ExprKind, MatchSource};
58
use rustc_lint::{LateContext, LateLintPass};
69
use rustc_middle::lint::in_external_macro;
7-
use rustc_middle::ty::Ty;
10+
use rustc_middle::ty::{self, Ty};
811
use rustc_session::{declare_lint_pass, declare_tool_lint};
912

1013
declare_clippy_lint! {
@@ -65,19 +68,39 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
6568
if let Some(ref err_arg) = err_args.get(0);
6669
if let ExprKind::Path(ref err_fun_path) = err_fun.kind;
6770
if match_qpath(err_fun_path, &paths::RESULT_ERR);
68-
if let Some(return_type) = find_err_return_type(cx, &expr.kind);
69-
71+
if let Some(return_ty) = find_return_type(cx, &expr.kind);
7072
then {
71-
let err_type = cx.typeck_results().expr_ty(err_arg);
73+
let prefix;
74+
let suffix;
75+
let err_ty;
76+
77+
if let Some(ty) = result_error_type(cx, return_ty) {
78+
prefix = "Err(";
79+
suffix = ")";
80+
err_ty = ty;
81+
} else if let Some(ty) = poll_result_error_type(cx, return_ty) {
82+
prefix = "Poll::Ready(Err(";
83+
suffix = "))";
84+
err_ty = ty;
85+
} else if let Some(ty) = poll_option_result_error_type(cx, return_ty) {
86+
prefix = "Poll::Ready(Some(Err(";
87+
suffix = ")))";
88+
err_ty = ty;
89+
} else {
90+
return;
91+
};
92+
93+
let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
94+
7295
let origin_snippet = if err_arg.span.from_expansion() {
7396
snippet_with_macro_callsite(cx, err_arg.span, "_")
7497
} else {
7598
snippet(cx, err_arg.span, "_")
7699
};
77-
let suggestion = if err_type == return_type {
78-
format!("return Err({})", origin_snippet)
100+
let suggestion = if err_ty == expr_err_ty {
101+
format!("return {}{}{}", prefix, origin_snippet, suffix)
79102
} else {
80-
format!("return Err({}.into())", origin_snippet)
103+
format!("return {}{}.into(){}", prefix, origin_snippet, suffix)
81104
};
82105

83106
span_lint_and_sugg(
@@ -94,27 +117,68 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
94117
}
95118
}
96119

97-
// In order to determine whether to suggest `.into()` or not, we need to find the error type the
98-
// function returns. To do that, we look for the From::from call (see tree above), and capture
99-
// its output type.
100-
fn find_err_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
120+
/// Finds function return type by examining return expressions in match arms.
121+
fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
101122
if let ExprKind::Match(_, ref arms, MatchSource::TryDesugar) = expr {
102-
arms.iter().find_map(|ty| find_err_return_type_arm(cx, ty))
103-
} else {
104-
None
123+
for arm in arms.iter() {
124+
if let ExprKind::Ret(Some(ref ret)) = arm.body.kind {
125+
return Some(cx.typeck_results().expr_ty(ret));
126+
}
127+
}
105128
}
129+
None
106130
}
107131

108-
// Check for From::from in one of the match arms.
109-
fn find_err_return_type_arm<'tcx>(cx: &LateContext<'tcx>, arm: &'tcx Arm<'_>) -> Option<Ty<'tcx>> {
132+
/// Extracts the error type from Result<T, E>.
133+
fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
110134
if_chain! {
111-
if let ExprKind::Ret(Some(ref err_ret)) = arm.body.kind;
112-
if let ExprKind::Call(ref from_error_path, ref from_error_args) = err_ret.kind;
113-
if let ExprKind::Path(ref from_error_fn) = from_error_path.kind;
114-
if match_qpath(from_error_fn, &paths::TRY_FROM_ERROR);
115-
if let Some(from_error_arg) = from_error_args.get(0);
135+
if let ty::Adt(_, subst) = ty.kind;
136+
if is_type_diagnostic_item(cx, ty, sym!(result_type));
137+
let err_ty = subst.type_at(1);
138+
then {
139+
Some(err_ty)
140+
} else {
141+
None
142+
}
143+
}
144+
}
145+
146+
/// Extracts the error type from Poll<Result<T, E>>.
147+
fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
148+
if_chain! {
149+
if let ty::Adt(def, subst) = ty.kind;
150+
if match_def_path(cx, def.did, &paths::POLL);
151+
let ready_ty = subst.type_at(0);
152+
153+
if let ty::Adt(ready_def, ready_subst) = ready_ty.kind;
154+
if cx.tcx.is_diagnostic_item(sym!(result_type), ready_def.did);
155+
let err_ty = ready_subst.type_at(1);
156+
157+
then {
158+
Some(err_ty)
159+
} else {
160+
None
161+
}
162+
}
163+
}
164+
165+
/// Extracts the error type from Poll<Option<Result<T, E>>>.
166+
fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
167+
if_chain! {
168+
if let ty::Adt(def, subst) = ty.kind;
169+
if match_def_path(cx, def.did, &paths::POLL);
170+
let ready_ty = subst.type_at(0);
171+
172+
if let ty::Adt(ready_def, ready_subst) = ready_ty.kind;
173+
if cx.tcx.is_diagnostic_item(sym!(option_type), ready_def.did);
174+
let some_ty = ready_subst.type_at(0);
175+
176+
if let ty::Adt(some_def, some_subst) = some_ty.kind;
177+
if cx.tcx.is_diagnostic_item(sym!(result_type), some_def.did);
178+
let err_ty = some_subst.type_at(1);
179+
116180
then {
117-
Some(cx.typeck_results().expr_ty(from_error_arg))
181+
Some(err_ty)
118182
} else {
119183
None
120184
}

clippy_lints/src/utils/paths.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub const PATH: [&str; 3] = ["std", "path", "Path"];
8080
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
8181
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
8282
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
83+
pub const POLL: [&str; 4] = ["core", "task", "poll", "Poll"];
8384
pub const PTR_EQ: [&str; 3] = ["core", "ptr", "eq"];
8485
pub const PTR_NULL: [&str; 2] = ["ptr", "null"];
8586
pub const PTR_NULL_MUT: [&str; 2] = ["ptr", "null_mut"];
@@ -129,7 +130,6 @@ pub const TO_STRING: [&str; 3] = ["alloc", "string", "ToString"];
129130
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
130131
pub const TRANSMUTE: [&str; 4] = ["core", "intrinsics", "", "transmute"];
131132
pub const TRY_FROM: [&str; 4] = ["core", "convert", "TryFrom", "try_from"];
132-
pub const TRY_FROM_ERROR: [&str; 4] = ["std", "ops", "Try", "from_error"];
133133
pub const TRY_INTO_RESULT: [&str; 4] = ["std", "ops", "Try", "into_result"];
134134
pub const TRY_INTO_TRAIT: [&str; 3] = ["core", "convert", "TryInto"];
135135
pub const VEC: [&str; 3] = ["alloc", "vec", "Vec"];

tests/ui/try_err.fixed

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#[macro_use]
77
extern crate macro_rules;
88

9+
use std::io;
10+
use std::task::Poll;
11+
912
// Tests that a simple case works
1013
// Should flag `Err(err)?`
1114
pub fn basic_test() -> Result<i32, i32> {
@@ -104,3 +107,21 @@ pub fn macro_inside(fail: bool) -> Result<i32, String> {
104107
}
105108
Ok(0)
106109
}
110+
111+
pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
112+
if n == 0 {
113+
return Poll::Ready(Err(io::ErrorKind::WriteZero.into()))
114+
} else if n == 1 {
115+
return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, "error")))
116+
};
117+
118+
Poll::Ready(Ok(n))
119+
}
120+
121+
pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
122+
if !ready {
123+
return Poll::Ready(Some(Err(io::ErrorKind::NotFound.into())))
124+
}
125+
126+
Poll::Ready(None)
127+
}

tests/ui/try_err.rs

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#[macro_use]
77
extern crate macro_rules;
88

9+
use std::io;
10+
use std::task::Poll;
11+
912
// Tests that a simple case works
1013
// Should flag `Err(err)?`
1114
pub fn basic_test() -> Result<i32, i32> {
@@ -104,3 +107,21 @@ pub fn macro_inside(fail: bool) -> Result<i32, String> {
104107
}
105108
Ok(0)
106109
}
110+
111+
pub fn poll_write(n: usize) -> Poll<io::Result<usize>> {
112+
if n == 0 {
113+
Err(io::ErrorKind::WriteZero)?
114+
} else if n == 1 {
115+
Err(io::Error::new(io::ErrorKind::InvalidInput, "error"))?
116+
};
117+
118+
Poll::Ready(Ok(n))
119+
}
120+
121+
pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
122+
if !ready {
123+
Err(io::ErrorKind::NotFound)?
124+
}
125+
126+
Poll::Ready(None)
127+
}

tests/ui/try_err.stderr

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: returning an `Err(_)` with the `?` operator
2-
--> $DIR/try_err.rs:15:9
2+
--> $DIR/try_err.rs:18:9
33
|
44
LL | Err(err)?;
55
| ^^^^^^^^^ help: try this: `return Err(err)`
@@ -11,28 +11,46 @@ LL | #![deny(clippy::try_err)]
1111
| ^^^^^^^^^^^^^^^
1212

1313
error: returning an `Err(_)` with the `?` operator
14-
--> $DIR/try_err.rs:25:9
14+
--> $DIR/try_err.rs:28:9
1515
|
1616
LL | Err(err)?;
1717
| ^^^^^^^^^ help: try this: `return Err(err.into())`
1818

1919
error: returning an `Err(_)` with the `?` operator
20-
--> $DIR/try_err.rs:45:17
20+
--> $DIR/try_err.rs:48:17
2121
|
2222
LL | Err(err)?;
2323
| ^^^^^^^^^ help: try this: `return Err(err)`
2424

2525
error: returning an `Err(_)` with the `?` operator
26-
--> $DIR/try_err.rs:64:17
26+
--> $DIR/try_err.rs:67:17
2727
|
2828
LL | Err(err)?;
2929
| ^^^^^^^^^ help: try this: `return Err(err.into())`
3030

3131
error: returning an `Err(_)` with the `?` operator
32-
--> $DIR/try_err.rs:103:9
32+
--> $DIR/try_err.rs:106:9
3333
|
3434
LL | Err(foo!())?;
3535
| ^^^^^^^^^^^^ help: try this: `return Err(foo!())`
3636

37-
error: aborting due to 5 previous errors
37+
error: returning an `Err(_)` with the `?` operator
38+
--> $DIR/try_err.rs:113:9
39+
|
40+
LL | Err(io::ErrorKind::WriteZero)?
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Err(io::ErrorKind::WriteZero.into()))`
42+
43+
error: returning an `Err(_)` with the `?` operator
44+
--> $DIR/try_err.rs:115:9
45+
|
46+
LL | Err(io::Error::new(io::ErrorKind::InvalidInput, "error"))?
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, "error")))`
48+
49+
error: returning an `Err(_)` with the `?` operator
50+
--> $DIR/try_err.rs:123:9
51+
|
52+
LL | Err(io::ErrorKind::NotFound)?
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Some(Err(io::ErrorKind::NotFound.into())))`
54+
55+
error: aborting due to 8 previous errors
3856

0 commit comments

Comments
 (0)