Skip to content

Commit 561352f

Browse files
committed
Temporary commit
1 parent aef5fe2 commit 561352f

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

clippy_lints/src/use_last.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
//! lint on using `x.get(x.len() - 1)` instead of `x.last()`
22
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};
46
use rustc::{declare_tool_lint, lint_array};
5-
use if_chain::if_chain;
7+
// use if_chain::if_chain;
68
// 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;
1013
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;
1518

1619
/// **What it does:** Checks for using `x.get(x.len() - 1)` instead of `x.last()`.
1720
///
@@ -51,23 +54,23 @@ impl LintPass for UseLast {
5154
}
5255

5356
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) {
5558
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";
6267
then {
6368
span_lint(cx,
6469
USE_LAST,
65-
expr.span,
70+
stmt.span,
6671
// 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)`"));
6973
}
7074
}
71-
7275
}
7376
}

tests/ui/use_last.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::all)]
2-
#![warn(clippy::use_last)]
2+
// #![warn(clippy::use_last)]
33

44
fn dont_use_last() -> Option<i32> {
55
let x = vec![2, 3, 5];

0 commit comments

Comments
 (0)