|
1 | 1 | //! lint on using `x.get(x.len() - 1)` instead of `x.last()`
|
2 | 2 |
|
3 |
| -use crate::utils::{match_type, paths, span_lint_and_sugg, snippet_with_applicability}; |
4 |
| -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
5 |
| -use rustc::{declare_tool_lint, lint_array}; |
| 3 | +use crate::utils::{match_type, paths, snippet_with_applicability, span_lint_and_sugg, SpanlessEq}; |
| 4 | +use if_chain::if_chain; |
6 | 5 | use rustc::hir::{Expr, ExprKind};
|
| 6 | +use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
| 7 | +use rustc::{declare_lint_pass, declare_tool_lint}; |
7 | 8 | use rustc_errors::Applicability;
|
8 |
| -use syntax::ast::{LitKind}; |
9 |
| -use if_chain::if_chain; |
10 |
| - |
11 |
| -/// **What it does:** Checks for using `x.get(x.len() - 1)` instead of `x.last()`. |
12 |
| -/// |
13 |
| -/// **Why is this bad?** Using `x.last()` is easier to read and has the same result. |
14 |
| -/// |
15 |
| -/// Note that using `x[x.len() - 1]` is semantically different from `x.last()`. |
16 |
| -/// Indexing into the array will panic on out-of-bounds accesses, while |
17 |
| -/// `x.get()` and `x.last()` will return `None`. |
18 |
| -/// |
19 |
| -/// There is another lint (get_unwrap) that covers the case of using |
20 |
| -/// `x.get(index).unwrap()` instead of `x[index]`. |
21 |
| -/// |
22 |
| -/// **Known problems:** None. |
23 |
| -/// |
24 |
| -/// **Example:** |
25 |
| -/// |
26 |
| -/// ```rust |
27 |
| -/// // Bad |
28 |
| -/// let x = vec![2, 3, 5]; |
29 |
| -/// let last_element = x.get(x.len() - 1); |
30 |
| -/// |
31 |
| -/// // Good |
32 |
| -/// let x = vec![2, 3, 5]; |
33 |
| -/// let last_element = x.last(); |
34 |
| -/// ``` |
| 9 | +use syntax::ast::LitKind; |
35 | 10 |
|
36 | 11 | declare_clippy_lint! {
|
| 12 | + /// **What it does:** Checks for using `x.get(x.len() - 1)` instead of |
| 13 | + /// `x.last()`. |
| 14 | + /// |
| 15 | + /// **Why is this bad?** Using `x.last()` is easier to read and has the same |
| 16 | + /// result. |
| 17 | + /// |
| 18 | + /// Note that using `x[x.len() - 1]` is semantically different from |
| 19 | + /// `x.last()`. Indexing into the array will panic on out-of-bounds |
| 20 | + /// accesses, while `x.get()` and `x.last()` will return `None`. |
| 21 | + /// |
| 22 | + /// There is another lint (get_unwrap) that covers the case of using |
| 23 | + /// `x.get(index).unwrap()` instead of `x[index]`. |
| 24 | + /// |
| 25 | + /// **Known problems:** None. |
| 26 | + /// |
| 27 | + /// **Example:** |
| 28 | + /// |
| 29 | + /// ```rust |
| 30 | + /// // Bad |
| 31 | + /// let x = vec![2, 3, 5]; |
| 32 | + /// let last_element = x.get(x.len() - 1); |
| 33 | + /// |
| 34 | + /// // Good |
| 35 | + /// let x = vec![2, 3, 5]; |
| 36 | + /// let last_element = x.last(); |
| 37 | + /// ``` |
37 | 38 | pub USE_LAST,
|
38 | 39 | complexity,
|
39 |
| - "using `x.get(x.len() - 1)` instead of `x.last()`" |
| 40 | + "Using `x.get(x.len() - 1)` when `x.last()` is correct and simpler" |
40 | 41 | }
|
41 | 42 |
|
42 |
| -#[derive(Copy, Clone, Debug)] |
43 |
| -pub struct UseLast; |
44 |
| - |
45 |
| -impl LintPass for UseLast { |
46 |
| - fn get_lints(&self) -> LintArray { |
47 |
| - lint_array!(USE_LAST) |
48 |
| - } |
49 |
| - |
50 |
| - fn name(&self) -> &'static str { |
51 |
| - "UseLast" |
52 |
| - } |
53 |
| -} |
| 43 | +declare_lint_pass!(UseLast => [USE_LAST]); |
54 | 44 |
|
55 | 45 | impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseLast {
|
56 | 46 | fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
@@ -78,10 +68,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseLast {
|
78 | 68 | if arg_lhs_path.ident.name == "len";
|
79 | 69 | if let Some(arg_lhs_struct) = lhs_args.get(0);
|
80 | 70 |
|
81 |
| - // TODO: Is this a valid way to check if they reference the same vector? |
82 |
| - if let ExprKind::Path(arg_lhs_struct_path) = arg_lhs_struct.node; |
83 |
| - if let ExprKind::Path(struct_calling_on_path) = struct_calling_on.nod |
84 |
| - if arg_lhs_struct_path == struct_calling_on_path; |
| 71 | + if SpanlessEq::new(cx).eq_expr(struct_calling_on, arg_lhs_struct); |
85 | 72 |
|
86 | 73 | // RHS of subtraction is 1
|
87 | 74 | if let ExprKind::Lit(ref rhs_lit) = rhs.node;
|
|
0 commit comments