|
1 | 1 | //! lint on using `x.get(x.len() - 1)` instead of `x.last()`
|
2 | 2 |
|
3 |
| -use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; |
| 3 | +// use crate::utils::{in_macro, snippet_opt, last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then}; |
| 4 | +// use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; |
| 5 | +use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
4 | 6 | use rustc::{declare_tool_lint, lint_array};
|
5 |
| -use if_chain::if_chain; |
| 7 | +// use if_chain::if_chain; |
6 | 8 | // use syntax::ast::*;
|
7 |
| -use rustc::hir::*; |
8 |
| -use crate::utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then}; |
9 |
| -use crate::utils::{opt_def_id, sugg}; |
| 9 | +use rustc::hir::{PatKind, BindingAnnotation, Expr, ExprKind}; |
| 10 | +// use crate::utils::{opt_def_id, sugg}; |
| 11 | +// use crate::utils::{span_lint, snippet}; |
| 12 | +use crate::utils::span_lint; |
10 | 13 | use if_chain::if_chain;
|
11 |
| -use rustc::ty::{self, Ty}; |
12 |
| -use rustc_errors::Applicability; |
13 |
| -use std::borrow::Cow; |
14 |
| -use syntax::ast; |
| 14 | +// use rustc::ty::{self, Ty}; |
| 15 | +// use rustc_errors::Applicability; |
| 16 | +// use std::borrow::Cow; |
| 17 | +// use syntax::ast; |
15 | 18 |
|
16 | 19 | /// **What it does:** Checks for using `x.get(x.len() - 1)` instead of `x.last()`.
|
17 | 20 | ///
|
@@ -51,23 +54,23 @@ impl LintPass for UseLast {
|
51 | 54 | }
|
52 | 55 |
|
53 | 56 | impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseLast {
|
54 |
| - fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { |
| 57 | + fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { |
55 | 58 | if_chain! {
|
56 |
| - if let StmtKind::Local(ref local) = stmt.node; |
57 |
| - if let Some(ref init) = local.init |
58 |
| - if let ExprKind::MethodCall(ref method_name, ref generics, ref args) = init.node; |
59 |
| - // unimplemented: `ExprKind::MethodCall` is not further destructured at the moment |
60 |
| - if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = local.pat.node; |
61 |
| - if name.node.as_str() == "last_element"; |
| 59 | + if let ExprKind::MethodCall(ref path, _, ref args) = expr.node; |
| 60 | + // check if vector |
| 61 | + // TODO: check if vector |
| 62 | + // check if calling 'get' method |
| 63 | + if path.ident.name.as_str() == "get"; |
| 64 | + // if let ExprKind::MethodCall(ref method_name, ref generics, ref args) = init.node; |
| 65 | + // if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, _, None) = local.pat.node; |
| 66 | + // if name.node.as_str() == "last_element"; |
62 | 67 | then {
|
63 | 68 | span_lint(cx,
|
64 | 69 | USE_LAST,
|
65 |
| - expr.span, |
| 70 | + stmt.span, |
66 | 71 | // Todo: fix this
|
67 |
| - &format!("It is more idiomatic to use {}.iter().enumerate()", |
68 |
| - snippet(cx, iter_args[0].span, "_"))); |
| 72 | + &format!("Use `x.last()` instead of `x.get(x.len() - 1)`")); |
69 | 73 | }
|
70 | 74 | }
|
71 |
| - |
72 | 75 | }
|
73 | 76 | }
|
0 commit comments