{"record":{"id":"879fcb31541e15eb","repo":"rust-lang/rust-clippy","slug":"called-err-expect-on-a-result-value","errorCode":null,"errorMessage":"called `.err().expect()` on a `Result` value","messagePattern":"called `\\.err\\(\\)\\.expect\\(\\)` on a `Result` value","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clippy_lints/src/methods/err_expect.rs","lineNumber":30,"sourceCode":"    cx: &LateContext<'_>,\n    _expr: &rustc_hir::Expr<'_>,\n    recv: &rustc_hir::Expr<'_>,\n    expect_span: Span,\n    err_span: Span,\n    msrv: Msrv,\n) {\n    let result_ty = cx.typeck_results().expr_ty(recv);\n    // Grabs the `Result<T, E>` type\n    if let Some(data_type) = get_data_type(cx, result_ty)\n        // Tests if the T type in a `Result<T, E>` implements Debug\n        && has_debug_impl(cx, data_type)\n        && msrv.meets(cx, msrvs::EXPECT_ERR)\n    {\n        span_lint_and_sugg(\n            cx,\n            ERR_EXPECT,\n            err_span.to(expect_span),\n            \"called `.err().expect()` on a `Result` value\",\n            \"try\",\n            \"expect_err\".to_string(),\n            Applicability::MachineApplicable,\n        );\n    }\n}\n\n/// Given a `Result<T, E>` type, return its data (`T`).\nfn get_data_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {\n    match ty.kind() {\n        ty::Adt(adt, args) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => args.types().next(),\n        _ => None,\n    }\n}\n","sourceCodeStart":12,"sourceCodeEnd":45,"githubUrl":"https://github.com/rust-lang/rust-clippy/blob/13aece11388703700981f29d9480ffd6409a3192/clippy_lints/src/methods/err_expect.rs#L12-L45","documentation":"Clippy lint ERR_EXPECT: calling .err() on a Result<T, E> converts it into an Option<E>, discarding the Ok(T) payload; .expect(msg) on that Option then panics with the caller-supplied message whenever the Result was Ok. The panic therefore fires on success and reports a misleading generic message instead of the actual value. The lint only fires when T: Debug (so expect_err is expressible) and the MSRV supports EXPECT_ERR.","triggerScenarios":"Thrown at clippy_lints/src/methods/err_expect.rs:30 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Replace .err().expect(\"...\") with .expect_err(\"...\") so the panic message includes the debug-printed Ok value","Use .unwrap_err() when no custom message is needed","Handle both cases explicitly with a match or if let! to avoid panicking entirely"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"13aece11388703700981f29d9480ffd6409a3192","analyzedAt":"2026-09-07T23:08:18.134Z","contentChangedAt":"2026-09-07T23:08:18.134Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}