2019-05-21 00:01:21 +00:00
|
|
|
//! lint on using `x.get(x.len() - 1)` instead of `x.last()`
|
|
|
|
|
2019-09-09 15:22:41 +00:00
|
|
|
use crate::utils::{is_type_diagnostic_item, snippet_with_applicability, span_lint_and_sugg, SpanlessEq};
|
2019-05-21 00:01:21 +00:00
|
|
|
use if_chain::if_chain;
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast::LitKind;
|
2019-05-21 00:01:21 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::Spanned;
|
2019-05-21 00:01:21 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for using `x.get(x.len() - 1)` instead of
|
|
|
|
/// `x.last()`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Using `x.last()` is easier to read and has the same
|
|
|
|
/// result.
|
|
|
|
///
|
|
|
|
/// Note that using `x[x.len() - 1]` is semantically different from
|
|
|
|
/// `x.last()`. Indexing into the array will panic on out-of-bounds
|
|
|
|
/// accesses, while `x.get()` and `x.last()` will return `None`.
|
|
|
|
///
|
|
|
|
/// There is another lint (get_unwrap) that covers the case of using
|
|
|
|
/// `x.get(index).unwrap()` instead of `x[index]`.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// // Bad
|
|
|
|
/// let x = vec![2, 3, 5];
|
|
|
|
/// let last_element = x.get(x.len() - 1);
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let x = vec![2, 3, 5];
|
|
|
|
/// let last_element = x.last();
|
|
|
|
/// ```
|
|
|
|
pub GET_LAST_WITH_LEN,
|
|
|
|
complexity,
|
|
|
|
"Using `x.get(x.len() - 1)` when `x.last()` is correct and simpler"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(GetLastWithLen => [GET_LAST_WITH_LEN]);
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for GetLastWithLen {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2019-05-21 00:01:21 +00:00
|
|
|
if_chain! {
|
|
|
|
// Is a method call
|
2020-06-09 21:44:04 +00:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind;
|
2019-05-21 00:01:21 +00:00
|
|
|
|
|
|
|
// Method name is "get"
|
2020-04-13 06:57:34 +00:00
|
|
|
if path.ident.name == sym!(get);
|
2019-05-21 00:01:21 +00:00
|
|
|
|
|
|
|
// Argument 0 (the struct we're calling the method on) is a vector
|
|
|
|
if let Some(struct_calling_on) = args.get(0);
|
2020-07-17 08:47:04 +00:00
|
|
|
let struct_ty = cx.typeck_results().expr_ty(struct_calling_on);
|
2020-04-13 06:57:34 +00:00
|
|
|
if is_type_diagnostic_item(cx, struct_ty, sym!(vec_type));
|
2019-05-21 00:01:21 +00:00
|
|
|
|
|
|
|
// Argument to "get" is a subtraction
|
|
|
|
if let Some(get_index_arg) = args.get(1);
|
2019-05-21 07:33:59 +00:00
|
|
|
if let ExprKind::Binary(
|
|
|
|
Spanned {
|
|
|
|
node: BinOpKind::Sub,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
lhs,
|
|
|
|
rhs,
|
2019-09-27 15:16:06 +00:00
|
|
|
) = &get_index_arg.kind;
|
2019-05-21 00:01:21 +00:00
|
|
|
|
|
|
|
// LHS of subtraction is "x.len()"
|
2020-06-09 21:44:04 +00:00
|
|
|
if let ExprKind::MethodCall(arg_lhs_path, _, lhs_args, _) = &lhs.kind;
|
2020-04-13 06:57:34 +00:00
|
|
|
if arg_lhs_path.ident.name == sym!(len);
|
2019-05-21 00:01:21 +00:00
|
|
|
if let Some(arg_lhs_struct) = lhs_args.get(0);
|
|
|
|
|
|
|
|
// The two vectors referenced (x in x.get(...) and in x.len())
|
|
|
|
if SpanlessEq::new(cx).eq_expr(struct_calling_on, arg_lhs_struct);
|
|
|
|
|
|
|
|
// RHS of subtraction is 1
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Lit(rhs_lit) = &rhs.kind;
|
2019-09-29 16:40:38 +00:00
|
|
|
if let LitKind::Int(1, ..) = rhs_lit.node;
|
2019-05-21 00:01:21 +00:00
|
|
|
|
|
|
|
then {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let vec_name = snippet_with_applicability(
|
|
|
|
cx,
|
|
|
|
struct_calling_on.span, "vec",
|
|
|
|
&mut applicability,
|
|
|
|
);
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
GET_LAST_WITH_LEN,
|
|
|
|
expr.span,
|
|
|
|
&format!("accessing last element with `{0}.get({0}.len() - 1)`", vec_name),
|
|
|
|
"try",
|
|
|
|
format!("{}.last()", vec_name),
|
2019-05-21 07:33:59 +00:00
|
|
|
applicability,
|
|
|
|
);
|
2019-05-21 00:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|