|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 3 | +use clippy_utils::res::MaybeDef as _; |
| 4 | +use clippy_utils::{higher, is_in_test}; |
| 5 | +use rustc_hir::{Expr, LetStmt, Pat, PatKind, QPath}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::impl_lint_pass; |
| 8 | +use rustc_span::sym; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for `if let Ok(x) = expr`, `while let Ok(x) = expr`, and |
| 13 | + /// `let Ok(x) = expr else { ... }` where the `Err` variant is discarded |
| 14 | + /// without binding. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// The error value contains context about what went wrong. Discarding it |
| 18 | + /// prevents detailed logging and makes error recovery impossible. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```rust,ignore |
| 22 | + /// if let Ok(res) = some_call() { |
| 23 | + /// use_res(res); |
| 24 | + /// } else { |
| 25 | + /// error!("Something went wrong"); |
| 26 | + /// } |
| 27 | + /// |
| 28 | + /// while let Ok(line) = reader.read_line() { |
| 29 | + /// process(line); |
| 30 | + /// } |
| 31 | + /// |
| 32 | + /// let Ok(val) = some_call() else { return; }; |
| 33 | + /// ``` |
| 34 | + /// Use instead: |
| 35 | + /// ```rust,ignore |
| 36 | + /// match some_call() { |
| 37 | + /// Ok(res) => use_res(res), |
| 38 | + /// Err(e) => error!("Something went wrong: {}", e), |
| 39 | + /// } |
| 40 | + /// |
| 41 | + /// loop { |
| 42 | + /// match reader.read_line() { |
| 43 | + /// Ok(line) => process(line), |
| 44 | + /// Err(e) => { |
| 45 | + /// error!("Read failed: {}", e); |
| 46 | + /// break; |
| 47 | + /// } |
| 48 | + /// } |
| 49 | + /// } |
| 50 | + /// |
| 51 | + /// match some_call() { |
| 52 | + /// Ok(val) => { /* use val */ }, |
| 53 | + /// Err(e) => { |
| 54 | + /// error!("Failed: {}", e); |
| 55 | + /// return; |
| 56 | + /// } |
| 57 | + /// } |
| 58 | + /// ``` |
| 59 | + /// |
| 60 | + /// ### Configuration |
| 61 | + /// - `allow-ignored-result-err-in-tests`: set to `true` to disable this lint in test code |
| 62 | + #[clippy::version = "1.98.0"] |
| 63 | + pub IGNORED_RESULT_ERR, |
| 64 | + restriction, |
| 65 | + "`if let Ok(x) = ...`, `while let Ok(x) = ...`, or `let Ok(x) = ... else` discards the error variant without binding it" |
| 66 | +} |
| 67 | + |
| 68 | +impl_lint_pass!(IgnoredResultErr => [IGNORED_RESULT_ERR]); |
| 69 | + |
| 70 | +pub struct IgnoredResultErr { |
| 71 | + allow_in_tests: bool, |
| 72 | +} |
| 73 | + |
| 74 | +impl IgnoredResultErr { |
| 75 | + pub fn new(conf: &'static Conf) -> Self { |
| 76 | + Self { |
| 77 | + allow_in_tests: conf.allow_ignored_result_err_in_tests, |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn is_ok_pattern_on_result(cx: &LateContext<'_>, pat: &Pat<'_>, scrutinee: &Expr<'_>) -> bool { |
| 83 | + cx.typeck_results().expr_ty(scrutinee).is_diag_item(cx, sym::Result) |
| 84 | + && matches!( |
| 85 | + pat.kind, |
| 86 | + PatKind::TupleStruct(QPath::Resolved(_, path), _, _) |
| 87 | + if path.segments.last().is_some_and(|s| s.ident.name == sym::Ok) |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +impl<'tcx> LateLintPass<'tcx> for IgnoredResultErr { |
| 92 | + fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx LetStmt<'_>) { |
| 93 | + if self.allow_in_tests && is_in_test(cx.tcx, local.hir_id) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + if local.els.is_some() |
| 98 | + && let Some(init) = local.init |
| 99 | + && is_ok_pattern_on_result(cx, local.pat, init) |
| 100 | + { |
| 101 | + span_lint_and_help( |
| 102 | + cx, |
| 103 | + IGNORED_RESULT_ERR, |
| 104 | + local.span, |
| 105 | + "this `let Ok(...) = ... else` discards the `Err` variant", |
| 106 | + None, |
| 107 | + "consider using `match` and binding the `Err` value for logging or recovery", |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 113 | + if self.allow_in_tests && is_in_test(cx.tcx, expr.hir_id) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + if let Some(if_let) = higher::IfLet::hir(cx, expr) |
| 118 | + && is_ok_pattern_on_result(cx, if_let.let_pat, if_let.let_expr) |
| 119 | + { |
| 120 | + span_lint_and_help( |
| 121 | + cx, |
| 122 | + IGNORED_RESULT_ERR, |
| 123 | + expr.span, |
| 124 | + "this `if let Ok(...)` discards the `Err` variant", |
| 125 | + None, |
| 126 | + "consider using `match` and binding the `Err` value for logging or recovery", |
| 127 | + ); |
| 128 | + } else if let Some(while_let) = higher::WhileLet::hir(expr) |
| 129 | + && is_ok_pattern_on_result(cx, while_let.let_pat, while_let.let_expr) |
| 130 | + { |
| 131 | + span_lint_and_help( |
| 132 | + cx, |
| 133 | + IGNORED_RESULT_ERR, |
| 134 | + expr.span, |
| 135 | + "this `while let Ok(...)` discards the `Err` variant", |
| 136 | + None, |
| 137 | + "consider using `loop` + `match` and binding the `Err` value for logging or recovery", |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments