mod flat_map_identity;
mod flat_map_option;
mod from_iter_instead_of_collect;
+mod get_last_with_len;
mod get_unwrap;
mod implicit_clone;
mod inefficient_to_string;
"replace `.drain(..)` with `.into_iter()`"
}
+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]`.
+ ///
+ /// ### 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();
+ /// ```
+ #[clippy::version = "1.37.0"]
+ pub GET_LAST_WITH_LEN,
+ complexity,
+ "Using `x.get(x.len() - 1)` when `x.last()` is correct and simpler"
+}
+
declare_clippy_lint! {
/// ### What it does
/// Checks for use of `.get().unwrap()` (or
#[clippy::version = "1.39.0"]
pub MANUAL_SATURATING_ARITHMETIC,
style,
- "`.chcked_add/sub(x).unwrap_or(MAX/MIN)`"
+ "`.checked_add/sub(x).unwrap_or(MAX/MIN)`"
}
declare_clippy_lint! {
///
/// ### Example
/// ```rust
- /// use std::iter::FromIterator;
- ///
/// let five_fives = std::iter::repeat(5).take(5);
///
/// let v = Vec::from_iter(five_fives);
BYTES_NTH,
ITER_SKIP_NEXT,
GET_UNWRAP,
+ GET_LAST_WITH_LEN,
STRING_EXTEND_CHARS,
ITER_CLONED_COLLECT,
ITER_WITH_DRAIN,
inspect_for_each::check(cx, expr, span2);
}
},
+ ("get", [arg]) => get_last_with_len::check(cx, expr, recv, arg),
("get_or_insert_with", [arg]) => unnecessary_lazy_eval::check(cx, expr, recv, arg, "get_or_insert"),
("is_file", []) => filetype_is_file::check(cx, expr, recv),
("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, self.msrv),