|
| 1 | +extern crate rustc_typeck as typeck; |
| 2 | + |
| 3 | +use syntax::ptr::P; |
| 4 | +use syntax::ast::*; |
| 5 | +use rustc::lint::{Context, LintPass, LintArray, Lint}; |
| 6 | +use rustc::middle::ty::{self, node_id_to_type, sty, ty_ptr, ty_rptr, mt, MethodTraitItemId}; |
| 7 | +use rustc::middle::def::{DefTy, DefStruct, DefTrait}; |
| 8 | +use syntax::codemap::{Span, Spanned}; |
| 9 | + |
| 10 | +declare_lint!(pub LEN_ZERO, Warn, |
| 11 | + "Warn on usage of double-mut refs, e.g. '&mut &mut ...'"); |
| 12 | + |
| 13 | +declare_lint!(pub LEN_WITHOUT_IS_EMPTY, Warn, |
| 14 | + "Warn on traits and impls that have .len() but not .is_empty()"); |
| 15 | + |
| 16 | +#[derive(Copy,Clone)] |
| 17 | +pub struct LenZero; |
| 18 | + |
| 19 | +impl LintPass for LenZero { |
| 20 | + fn get_lints(&self) -> LintArray { |
| 21 | + lint_array!(LEN_ZERO, LEN_WITHOUT_IS_EMPTY) |
| 22 | + } |
| 23 | + |
| 24 | + fn check_item(&mut self, cx: &Context, item: &Item) { |
| 25 | + match &item.node { |
| 26 | + &ItemTrait(_, _, _, ref trait_items) => |
| 27 | + check_trait_items(cx, item, trait_items), |
| 28 | + &ItemImpl(_, _, _, None, _, ref impl_items) => // only non-trait |
| 29 | + check_impl_items(cx, item, impl_items), |
| 30 | + _ => () |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + fn check_expr(&mut self, cx: &Context, expr: &Expr) { |
| 35 | + if let &ExprBinary(Spanned{node: cmp, ..}, ref left, ref right) = |
| 36 | + &expr.node { |
| 37 | + match cmp { |
| 38 | + BiEq => check_cmp(cx, expr.span, left, right, ""), |
| 39 | + BiGt | BiNe => check_cmp(cx, expr.span, left, right, "!"), |
| 40 | + _ => () |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn check_trait_items(cx: &Context, item: &Item, trait_items: &[P<TraitItem>]) { |
| 47 | + fn is_named_self(item: &TraitItem, name: &str) -> bool { |
| 48 | + item.ident.as_str() == name && item.attrs.len() == 0 |
| 49 | + } |
| 50 | + |
| 51 | + if !trait_items.iter().any(|i| is_named_self(i, "is_empty")) { |
| 52 | + //cx.span_lint(LEN_WITHOUT_IS_EMPTY, item.span, &format!("trait {}", item.ident.as_str())); |
| 53 | + for i in trait_items { |
| 54 | + if is_named_self(i, "len") { |
| 55 | + cx.span_lint(LEN_WITHOUT_IS_EMPTY, i.span, |
| 56 | + &format!("Trait '{}' has a '.len()' method, but no \ |
| 57 | + '.is_empty()' method. Consider adding one.", |
| 58 | + item.ident.as_str())); |
| 59 | + } |
| 60 | + }; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn check_impl_items(cx: &Context, item: &Item, impl_items: &[P<ImplItem>]) { |
| 65 | + fn is_named_self(item: &ImplItem, name: &str) -> bool { |
| 66 | + item.ident.as_str() == name && item.attrs.len() == 0 |
| 67 | + } |
| 68 | + |
| 69 | + if !impl_items.iter().any(|i| is_named_self(i, "is_empty")) { |
| 70 | + for i in impl_items { |
| 71 | + if is_named_self(i, "len") { |
| 72 | + cx.span_lint(LEN_WITHOUT_IS_EMPTY, i.span, |
| 73 | + &format!("Item '{}' has a '.len()' method, but no \ |
| 74 | + '.is_empty()' method. Consider adding one.", |
| 75 | + item.ident.as_str())); |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn check_cmp(cx: &Context, span: Span, left: &Expr, right: &Expr, empty: &str) { |
| 83 | + match (&left.node, &right.node) { |
| 84 | + (&ExprLit(ref lit), &ExprMethodCall(ref method, _, ref args)) => |
| 85 | + check_len_zero(cx, span, method, args, lit, empty), |
| 86 | + (&ExprMethodCall(ref method, _, ref args), &ExprLit(ref lit)) => |
| 87 | + check_len_zero(cx, span, method, args, lit, empty), |
| 88 | + _ => () |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn check_len_zero(cx: &Context, span: Span, method: &SpannedIdent, |
| 93 | + args: &[P<Expr>], lit: &Lit, empty: &str) { |
| 94 | + if let &Spanned{node: LitInt(0, _), ..} = lit { |
| 95 | + if method.node.as_str() == "len" && args.len() == 1 { |
| 96 | + cx.span_lint(LEN_ZERO, span, &format!( |
| 97 | + "Consider replacing the len comparison with '{}_.is_empty()' if available", |
| 98 | + empty)) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments