Skip to content

Commit 9de8758

Browse files
committed
Implement suggestion in on lint
1 parent 0cf2168 commit 9de8758

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

clippy_lints/src/methods/err_expect.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use super::ERR_EXPECT;
2-
use clippy_utils::{diagnostics::span_lint_and_help, meets_msrv, msrvs, ty::is_type_diagnostic_item};
2+
use clippy_utils::{meets_msrv, msrvs, ty::is_type_diagnostic_item};
3+
use clippy_utils::diagnostics::span_lint_and_sugg;
34
use clippy_utils::ty::implements_trait;
5+
use rustc_errors::Applicability;
46
use rustc_lint::LateContext;
57
use rustc_middle::ty;
68
use rustc_middle::ty::Ty;
79
use rustc_semver::RustcVersion;
8-
use rustc_span::sym;
10+
use rustc_span::{Span, sym};
911

10-
pub(super) fn check(cx: &LateContext<'_>, expr: &rustc_hir::Expr<'_>, recv: &rustc_hir::Expr<'_>, msrv: Option<&RustcVersion>) {
12+
pub(super) fn check(cx: &LateContext<'_>, _expr: &rustc_hir::Expr<'_>, recv: &rustc_hir::Expr<'_>, msrv: Option<&RustcVersion>, span: Span) {
1113
if_chain! {
1214
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
1315
if meets_msrv(msrv, &msrvs::EXPECT_ERR);
@@ -17,16 +19,17 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &rustc_hir::Expr<'_>, recv: &rus
1719
if has_debug_impl(error_type, cx);
1820

1921
then {
20-
span_lint_and_help(
22+
span_lint_and_sugg(
2123
cx,
2224
ERR_EXPECT,
23-
expr.span,
25+
span,
2426
"called `.err().expect()` on a `Result` value",
25-
None,
26-
"`.expect_err()` can be called instead",
27+
"try",
28+
"expect_err".to_string(),
29+
Applicability::MachineApplicable
2730
);
2831
}
29-
}
32+
};
3033
}
3134

3235
/// Given a `Result<T, E>` type, return its error type (`E`).

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2453,7 +2453,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
24532453
},
24542454
("expect", [_]) => match method_call(recv) {
24552455
Some(("ok", [recv], _)) => ok_expect::check(cx, expr, recv),
2456-
Some(("err", [recv], _)) => err_expect::check(cx, expr, recv, msrv),
2456+
Some(("err", [recv], span)) => err_expect::check(cx, expr, recv, msrv, span),
24572457
_ => expect_used::check(cx, expr, recv),
24582458
},
24592459

0 commit comments

Comments
 (0)