|
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, |
4 |
| - snippet_with_applicability, SpanlessEq}; |
5 |
| -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
6 |
| -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; |
7 | 5 | use rustc::hir::{Expr, ExprKind};
|
| 6 | +use rustc::lint::{LateContext, LateLintPass, LintPass, LintArray}; |
| 7 | +use rustc::{declare_tool_lint, declare_lint_pass}; |
8 | 8 | use rustc_errors::Applicability;
|
9 | 9 | use syntax::ast::{LitKind};
|
10 |
| -use if_chain::if_chain; |
11 |
| - |
12 |
| -/// **What it does:** Checks for using `x.get(x.len() - 1)` instead of `x.last()`. |
13 |
| -/// |
14 |
| -/// **Why is this bad?** Using `x.last()` is easier to read and has the same result. |
15 |
| -/// |
16 |
| -/// Note that using `x[x.len() - 1]` is semantically different from `x.last()`. |
17 |
| -/// Indexing into the array will panic on out-of-bounds accesses, while |
18 |
| -/// `x.get()` and `x.last()` will return `None`. |
19 |
| -/// |
20 |
| -/// There is another lint (get_unwrap) that covers the case of using |
21 |
| -/// `x.get(index).unwrap()` instead of `x[index]`. |
22 |
| -/// |
23 |
| -/// **Known problems:** None. |
24 |
| -/// |
25 |
| -/// **Example:** |
26 |
| -/// |
27 |
| -/// ```rust |
28 |
| -/// // Bad |
29 |
| -/// let x = vec![2, 3, 5]; |
30 |
| -/// let last_element = x.get(x.len() - 1); |
31 |
| -/// |
32 |
| -/// // Good |
33 |
| -/// let x = vec![2, 3, 5]; |
34 |
| -/// let last_element = x.last(); |
35 |
| -/// ``` |
36 | 10 |
|
37 | 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 | + /// ``` |
38 | 38 | pub USE_LAST,
|
39 | 39 | complexity,
|
40 | 40 | "using `x.get(x.len() - 1)` instead of `x.last()`"
|
41 | 41 | }
|
42 | 42 |
|
43 |
| -#[derive(Copy, Clone, Debug)] |
44 |
| -pub struct UseLast; |
45 |
| - |
46 |
| -impl LintPass for UseLast { |
47 |
| - fn get_lints(&self) -> LintArray { |
48 |
| - lint_array!(USE_LAST) |
49 |
| - } |
50 |
| - |
51 |
| - fn name(&self) -> &'static str { |
52 |
| - "UseLast" |
53 |
| - } |
54 |
| -} |
| 43 | +declare_lint_pass!(UseLast => [USE_LAST]); |
55 | 44 |
|
56 | 45 | impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseLast {
|
57 | 46 | fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
|
0 commit comments