|
| 1 | +//! lint on using `x.get(x.len() - 1)` instead of `x.last()` |
| 2 | +
|
| 3 | +use crate::utils::{match_type, paths, span_lint}; |
| 4 | +use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
| 5 | +use rustc::{declare_tool_lint, lint_array}; |
| 6 | +use rustc::hir::{Expr, ExprKind}; |
| 7 | +use syntax::ast::{LitKind}; |
| 8 | +use if_chain::if_chain; |
| 9 | + |
| 10 | +/// **What it does:** Checks for using `x.get(x.len() - 1)` instead of `x.last()`. |
| 11 | +/// |
| 12 | +/// **Why is this bad?** Using `x.last()` is easier to read and has the same result. |
| 13 | +/// |
| 14 | +/// Note that using x[x.len() - 1] is semantically different from x.last(), |
| 15 | +/// since indexing into the array will panic on out-of-bounds accesses, while |
| 16 | +/// x.get() and x.last() will return None. |
| 17 | +/// |
| 18 | +/// **Known problems:** None. |
| 19 | +/// |
| 20 | +/// **Example:** |
| 21 | +/// |
| 22 | +/// ```rust |
| 23 | +/// // Bad |
| 24 | +/// let x = vec![2, 3, 5]; |
| 25 | +/// let last_element = x.get(x.len() - 1); |
| 26 | +/// |
| 27 | +/// // Good |
| 28 | +/// let x = vec![2, 3, 5]; |
| 29 | +/// let last_element = x.last(); |
| 30 | +/// ``` |
| 31 | +
|
| 32 | +declare_clippy_lint! { |
| 33 | + pub USE_LAST, |
| 34 | + complexity, |
| 35 | + "using `x.get(x.len() - 1)` instead of `x.last()`" |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Copy, Clone, Debug)] |
| 39 | +pub struct UseLast; |
| 40 | + |
| 41 | +impl LintPass for UseLast { |
| 42 | + fn get_lints(&self) -> LintArray { |
| 43 | + lint_array!(USE_LAST) |
| 44 | + } |
| 45 | + |
| 46 | + fn name(&self) -> &'static str { |
| 47 | + "UseLast" |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseLast { |
| 52 | + fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { |
| 53 | + if_chain! { |
| 54 | + // Is a method call |
| 55 | + if let ExprKind::MethodCall(ref path, _, ref args) = expr.node; |
| 56 | + |
| 57 | + // Method name is "get" |
| 58 | + if path.ident.name == "get"; |
| 59 | + |
| 60 | + // Argument 0 (the struct we're calling the method on) is a vector |
| 61 | + if let Some(struct_calling_on) = args.get(0); |
| 62 | + let struct_ty = cx.tables.expr_ty(struct_calling_on); |
| 63 | + if match_type(cx, struct_ty, &paths::VEC); |
| 64 | + |
| 65 | + // Argument to "get" is a binary operation |
| 66 | + if let Some(get_index_arg) = args.get(1); |
| 67 | + if let rustc::hir::ExprKind::Binary(ref op, ref lhs, ref rhs) = get_index_arg.node; |
| 68 | + |
| 69 | + // Binary operation is a subtraction |
| 70 | + if op.node == rustc::hir::BinOpKind::Sub; |
| 71 | + |
| 72 | + // LHS of subtraction is "x.len()" |
| 73 | + if let ExprKind::MethodCall(ref arg_lhs_path, _, ref lhs_args) = lhs.node; |
| 74 | + if arg_lhs_path.ident.name == "len"; |
| 75 | + if let Some(arg_lhs_struct) = lhs_args.get(0); |
| 76 | + // TODO: Is this a valid way to check if they reference the same vector? |
| 77 | + if arg_lhs_struct.hir_id == struct_calling_on.hir_id; |
| 78 | + |
| 79 | + // RHS of subtraction is 1 |
| 80 | + if let ExprKind::Lit(ref rhs_lit) = rhs.node; |
| 81 | + if let LitKind::Int(rhs_value, ..) = rhs_lit.node; |
| 82 | + if rhs_value == 1; |
| 83 | + |
| 84 | + // TODO: Figure out how to get name of variable for lint message |
| 85 | + let vec_name = "x"; |
| 86 | + |
| 87 | + then { |
| 88 | + span_lint(cx, |
| 89 | + USE_LAST, |
| 90 | + expr.span, |
| 91 | + &format!("Use `{}.last()` instead of `{}.get({}.len() - 1)`", |
| 92 | + vec_name, vec_name, vec_name)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments