|
| 1 | +use if_chain::if_chain; |
| 2 | +use rustc_hir::*; |
| 3 | +use rustc_lint::{LateContext, LateLintPass}; |
| 4 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 5 | +use rustc_span::{Span, sym}; |
| 6 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 7 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 8 | + |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for `.or(…).unwrap()` calls to Options and Results. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// You should use `.unwrap_or(…)` instead for clarity. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```rust |
| 19 | + /// // Result |
| 20 | + /// let port = result.or::<Error>(Ok(fallback)).unwrap(); |
| 21 | + /// |
| 22 | + /// // Option |
| 23 | + /// let value = option.or(Some(fallback)).unwrap(); |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```rust |
| 27 | + /// // Result |
| 28 | + /// let port = result.unwrap_or(fallback); |
| 29 | + /// |
| 30 | + /// // Option |
| 31 | + /// let value = option.unwrap_or(fallback); |
| 32 | + /// ``` |
| 33 | + #[clippy::version = "1.61.0"] |
| 34 | + pub USE_UNWRAP_OR, |
| 35 | + complexity, |
| 36 | + "checks for `.or(…).unwrap()` calls to Options and Results." |
| 37 | +} |
| 38 | +declare_lint_pass!(UseUnwrapOr => [USE_UNWRAP_OR]); |
| 39 | + |
| 40 | +impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr { |
| 41 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 42 | + |
| 43 | + // look for x.or().unwrap() |
| 44 | + if_chain! { |
| 45 | + if let ExprKind::MethodCall(path, args, unwrap_span) = expr.kind; |
| 46 | + if path.ident.name.as_str() == "unwrap"; |
| 47 | + if let Some(caller) = args.first(); |
| 48 | + if let ExprKind::MethodCall(caller_path, caller_args, or_span) = caller.kind; |
| 49 | + if caller_path.ident.name.as_str() == "or"; |
| 50 | + then { |
| 51 | + let ty = cx.typeck_results().expr_ty(&caller_args[0]); // get type of x (we later check if it's Option or Result) |
| 52 | + let title; |
| 53 | + let arg = &caller_args[1]; // the argument or(xyz) is called with |
| 54 | + |
| 55 | + if is_type_diagnostic_item(&cx, ty, sym::Option) { |
| 56 | + title = ".or(Some(…)).unwrap() found"; |
| 57 | + if !is(arg, "Some") { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + } else if is_type_diagnostic_item(&cx, ty, sym::Result) { |
| 62 | + title = ".or(Ok(…)).unwrap() found"; |
| 63 | + if !is(arg, "Ok") { |
| 64 | + return; |
| 65 | + } |
| 66 | + } else { |
| 67 | + // Someone has implemented a struct with .or(...).unwrap() chaining, |
| 68 | + // but it's not an Option or a Result, so bail |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // span = or_span + unwrap_span |
| 73 | + let span = Span::new(or_span.lo(), unwrap_span.hi(), or_span.ctxt(), or_span.parent()); |
| 74 | + |
| 75 | + span_lint_and_help( |
| 76 | + cx, |
| 77 | + USE_UNWRAP_OR, |
| 78 | + span, |
| 79 | + title, |
| 80 | + None, |
| 81 | + "use `unwrap_or()` instead" |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// is expr a Call to name? |
| 89 | +/// name might be "Some", "Ok", "Err", etc. |
| 90 | +fn is<'a>(expr: &Expr<'a>, name: &str) -> bool { |
| 91 | + if_chain! { |
| 92 | + if let ExprKind::Call(some_expr, _some_args) = expr.kind; |
| 93 | + if let ExprKind::Path(path) = &some_expr.kind; |
| 94 | + if let QPath::Resolved(_, path) = path; |
| 95 | + if let Some(path_segment) = path.segments.first(); |
| 96 | + if path_segment.ident.name.as_str() == name; |
| 97 | + then { |
| 98 | + return true; |
| 99 | + } |
| 100 | + else { |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
0 commit comments