|
| 1 | +use crate::utils::{snippet_with_applicability, span_lint_and_sugg}; |
| 2 | +use rustc_ast::ast::LitKind; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::def_id::DefId; |
| 5 | +use rustc_hir::{BinOpKind, Expr, ExprKind, ItemKind, TraitItemRef}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_middle::ty; |
| 8 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 9 | +use rustc_span::source_map::{Span, Spanned}; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// **What it does:** |
| 13 | + /// |
| 14 | + /// **Why is this bad?** |
| 15 | + /// |
| 16 | + /// **Known problems:** None. |
| 17 | + /// |
| 18 | + /// **Example:** |
| 19 | + /// |
| 20 | + /// ```rust |
| 21 | + /// // example code where clippy issues a warning |
| 22 | + /// ``` |
| 23 | + /// Use instead: |
| 24 | + /// ```rust |
| 25 | + /// // example code which does not raise clippy warning |
| 26 | + /// ``` |
| 27 | + pub COMPARISON_TO_EMPTY, |
| 28 | + style, |
| 29 | + "default lint description" |
| 30 | +} |
| 31 | + |
| 32 | +declare_lint_pass!(ComparisonToEmpty => [COMPARISON_TO_EMPTY]); |
| 33 | + |
| 34 | +impl LateLintPass<'_> for ComparisonToEmpty { |
| 35 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 36 | + if expr.span.from_expansion() { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if let ExprKind::Binary(Spanned { node: cmp, .. }, ref left, ref right) = expr.kind { |
| 41 | + match cmp { |
| 42 | + BinOpKind::Eq => { |
| 43 | + check_cmp(cx, expr.span, left, right, "", 0); // len == 0 |
| 44 | + check_cmp(cx, expr.span, right, left, "", 0); // 0 == len |
| 45 | + }, |
| 46 | + BinOpKind::Ne => { |
| 47 | + check_cmp(cx, expr.span, left, right, "!", 0); // len != 0 |
| 48 | + check_cmp(cx, expr.span, right, left, "!", 0); // 0 != len |
| 49 | + }, |
| 50 | + BinOpKind::Gt => { |
| 51 | + check_cmp(cx, expr.span, left, right, "!", 0); // len > 0 |
| 52 | + check_cmp(cx, expr.span, right, left, "", 1); // 1 > len |
| 53 | + }, |
| 54 | + BinOpKind::Lt => { |
| 55 | + check_cmp(cx, expr.span, left, right, "", 1); // len < 1 |
| 56 | + check_cmp(cx, expr.span, right, left, "!", 0); // 0 < len |
| 57 | + }, |
| 58 | + BinOpKind::Ge => check_cmp(cx, expr.span, left, right, "!", 1), // len >= 1 |
| 59 | + BinOpKind::Le => check_cmp(cx, expr.span, right, left, "!", 1), // 1 <= len |
| 60 | + _ => (), |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +fn check_cmp(cx: &LateContext<'_>, span: Span, lit1: &Expr<'_>, lit2: &Expr<'_>, op: &str, compare_to: u32) { |
| 69 | + check_empty_expr(cx, span, lit1, lit2, op) |
| 70 | +} |
| 71 | + |
| 72 | +fn check_empty_expr( |
| 73 | + cx: &LateContext<'_>, |
| 74 | + span: Span, |
| 75 | + lit1: &Expr<'_>, |
| 76 | + lit2: &Expr<'_>, |
| 77 | + op: &str |
| 78 | +) { |
| 79 | + if (is_empty_array(lit2) || is_empty_string(lit2)) && has_is_empty(cx, lit1) { |
| 80 | + let mut applicability = Applicability::MachineApplicable; |
| 81 | + span_lint_and_sugg( |
| 82 | + cx, |
| 83 | + COMPARISON_TO_EMPTY, |
| 84 | + span, |
| 85 | + &format!("comparison to empty slice"), |
| 86 | + &format!("using `{}is_empty` is clearer and more explicit", op), |
| 87 | + format!( |
| 88 | + "{}{}.is_empty()", |
| 89 | + op, |
| 90 | + snippet_with_applicability(cx, lit1.span, "_", &mut applicability) |
| 91 | + ), |
| 92 | + applicability, |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn is_empty_string(expr: &Expr<'_>) -> bool { |
| 98 | + if let ExprKind::Lit(ref lit) = expr.kind { |
| 99 | + if let LitKind::Str(lit, _) = lit.node { |
| 100 | + let lit = lit.as_str(); |
| 101 | + return lit == ""; |
| 102 | + } |
| 103 | + } |
| 104 | + false |
| 105 | +} |
| 106 | + |
| 107 | +fn is_empty_array(expr: &Expr<'_>) -> bool { |
| 108 | + if let ExprKind::Array(ref arr) = expr.kind { |
| 109 | + return arr.is_empty(); |
| 110 | + } |
| 111 | + false |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +/// Checks if this type has an `is_empty` method. |
| 116 | +fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 117 | + /// Gets an `AssocItem` and return true if it matches `is_empty(self)`. |
| 118 | + fn is_is_empty(cx: &LateContext<'_>, item: &ty::AssocItem) -> bool { |
| 119 | + if let ty::AssocKind::Fn = item.kind { |
| 120 | + if item.ident.name.as_str() == "is_empty" { |
| 121 | + let sig = cx.tcx.fn_sig(item.def_id); |
| 122 | + let ty = sig.skip_binder(); |
| 123 | + ty.inputs().len() == 1 |
| 124 | + } else { |
| 125 | + false |
| 126 | + } |
| 127 | + } else { |
| 128 | + false |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// Checks the inherent impl's items for an `is_empty(self)` method. |
| 133 | + fn has_is_empty_impl(cx: &LateContext<'_>, id: DefId) -> bool { |
| 134 | + cx.tcx.inherent_impls(id).iter().any(|imp| { |
| 135 | + cx.tcx |
| 136 | + .associated_items(*imp) |
| 137 | + .in_definition_order() |
| 138 | + .any(|item| is_is_empty(cx, &item)) |
| 139 | + }) |
| 140 | + } |
| 141 | + |
| 142 | + let ty = &cx.typeck_results().expr_ty(expr).peel_refs(); |
| 143 | + match ty.kind() { |
| 144 | + ty::Dynamic(ref tt, ..) => tt.principal().map_or(false, |principal| { |
| 145 | + cx.tcx |
| 146 | + .associated_items(principal.def_id()) |
| 147 | + .in_definition_order() |
| 148 | + .any(|item| is_is_empty(cx, &item)) |
| 149 | + }), |
| 150 | + ty::Projection(ref proj) => has_is_empty_impl(cx, proj.item_def_id), |
| 151 | + ty::Adt(id, _) => has_is_empty_impl(cx, id.did), |
| 152 | + ty::Array(..) | ty::Slice(..) | ty::Str => true, |
| 153 | + _ => false, |
| 154 | + } |
| 155 | +} |
0 commit comments