Skip to content

Commit 1e6c697

Browse files
committed
PR comments
1 parent 60a8084 commit 1e6c697

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

clippy_lints/src/try_err.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{match_qpath, paths, snippet, span_lint_and_then};
1+
use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc::hir::*;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -17,17 +17,17 @@ declare_clippy_lint! {
1717
/// **Known problems:** None.
1818
///
1919
/// **Example:**
20-
///
21-
/// ```rust,ignore
22-
/// // Bad
20+
/// ```rust
2321
/// fn foo(fail: bool) -> Result<i32, String> {
2422
/// if fail {
2523
/// Err("failed")?;
2624
/// }
2725
/// Ok(0)
2826
/// }
27+
/// ```
28+
/// Could be written:
2929
///
30-
/// // Good
30+
/// ```rust
3131
/// fn foo(fail: bool) -> Result<i32, String> {
3232
/// if fail {
3333
/// return Err("failed".into());
@@ -57,7 +57,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
5757
if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.node;
5858
if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.node;
5959
if let ExprKind::Path(ref match_fun_path) = match_fun.node;
60-
if match_qpath(match_fun_path, &["std", "ops", "Try", "into_result"]);
60+
if match_qpath(match_fun_path, &paths::TRY_INTO_RESULT);
6161
if let Some(ref try_arg) = try_args.get(0);
6262
if let ExprKind::Call(ref err_fun, ref err_args) = try_arg.node;
6363
if let Some(ref err_arg) = err_args.get(0);
@@ -73,19 +73,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
7373
format!("return Err({}.into())", snippet(cx, err_arg.span, "_"))
7474
};
7575

76-
span_lint_and_then(
76+
span_lint_and_sugg(
7777
cx,
7878
TRY_ERR,
7979
expr.span,
80-
&format!("confusing error return, consider using `{}`", suggestion),
81-
|db| {
82-
db.span_suggestion(
83-
expr.span,
84-
"try this",
85-
suggestion,
86-
Applicability::MaybeIncorrect
87-
);
88-
},
80+
"returning an `Err(_)` with the `?` operator",
81+
"try this",
82+
suggestion,
83+
Applicability::MaybeIncorrect
8984
);
9085
}
9186
}
@@ -97,7 +92,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
9792
// its output type.
9893
fn find_err_return_type<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx ExprKind) -> Option<Ty<'tcx>> {
9994
if let ExprKind::Match(_, ref arms, MatchSource::TryDesugar) = expr {
100-
arms.iter().filter_map(|ty| find_err_return_type_arm(cx, ty)).nth(0)
95+
arms.iter().find_map(|ty| find_err_return_type_arm(cx, ty))
10196
} else {
10297
None
10398
}
@@ -109,7 +104,7 @@ fn find_err_return_type_arm<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arm: &'tcx Arm
109104
if let ExprKind::Ret(Some(ref err_ret)) = arm.body.node;
110105
if let ExprKind::Call(ref from_error_path, ref from_error_args) = err_ret.node;
111106
if let ExprKind::Path(ref from_error_fn) = from_error_path.node;
112-
if match_qpath(from_error_fn, &["std", "ops", "Try", "from_error"]);
107+
if match_qpath(from_error_fn, &paths::TRY_FROM_ERROR);
113108
if let Some(from_error_arg) = from_error_args.get(0);
114109
then {
115110
Some(cx.tables.expr_ty(from_error_arg))

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"
107107
pub const TO_STRING: [&str; 3] = ["alloc", "string", "ToString"];
108108
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
109109
pub const TRANSMUTE: [&str; 4] = ["core", "intrinsics", "", "transmute"];
110+
pub const TRY_FROM_ERROR: [&str; 4] = ["std", "ops", "Try", "from_error"];
110111
pub const TRY_INTO_RESULT: [&str; 4] = ["std", "ops", "Try", "into_result"];
111112
pub const UNINIT: [&str; 4] = ["core", "intrinsics", "", "uninit"];
112113
pub const VEC: [&str; 3] = ["alloc", "vec", "Vec"];

src/lintlist/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ pub const ALL_LINTS: [Lint; 306] = [
18231823
Lint {
18241824
name: "try_err",
18251825
group: "style",
1826-
desc: "TODO",
1826+
desc: "return errors explicitly rather than hiding them behind a `?`",
18271827
deprecation: None,
18281828
module: "try_err",
18291829
},

tests/ui/try_err.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: confusing error return, consider using `return Err(err)`
1+
error: returning an `Err(_)` with the `?` operator
22
--> $DIR/try_err.rs:7:5
33
|
44
LL | Err(err)?;
@@ -10,19 +10,19 @@ note: lint level defined here
1010
LL | #![deny(clippy::try_err)]
1111
| ^^^^^^^^^^^^^^^
1212

13-
error: confusing error return, consider using `return Err(err.into())`
13+
error: returning an `Err(_)` with the `?` operator
1414
--> $DIR/try_err.rs:14:5
1515
|
1616
LL | Err(err)?;
1717
| ^^^^^^^^^ help: try this: `return Err(err.into())`
1818

19-
error: confusing error return, consider using `return Err(err)`
19+
error: returning an `Err(_)` with the `?` operator
2020
--> $DIR/try_err.rs:31:13
2121
|
2222
LL | Err(err)?;
2323
| ^^^^^^^^^ help: try this: `return Err(err)`
2424

25-
error: confusing error return, consider using `return Err(err.into())`
25+
error: returning an `Err(_)` with the `?` operator
2626
--> $DIR/try_err.rs:46:13
2727
|
2828
LL | Err(err)?;

0 commit comments

Comments
 (0)