Skip to content

Commit bc391ab

Browse files
committed
Use declare_lint_pass! macro
1 parent b15da8d commit bc391ab

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

clippy_lints/src/use_last.rs

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,46 @@
11
//! lint on using `x.get(x.len() - 1)` instead of `x.last()`
22
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;
75
use rustc::hir::{Expr, ExprKind};
6+
use rustc::lint::{LateContext, LateLintPass, LintPass, LintArray};
7+
use rustc::{declare_tool_lint, declare_lint_pass};
88
use rustc_errors::Applicability;
99
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-
/// ```
3610

3711
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+
/// ```
3838
pub USE_LAST,
3939
complexity,
4040
"using `x.get(x.len() - 1)` instead of `x.last()`"
4141
}
4242

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]);
5544

5645
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseLast {
5746
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {

0 commit comments

Comments
 (0)