Auto merge of #9187 - sgued:iter-once, r=flip1995
Add lint recommending using `std::iter::once` and `std::iter::empty` ``` changelog: [`iter_once`]: add new lint changelog: [`iter_empty`]: add new lint ``` fixes #9186 - \[ ] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints The lint doesn't really follow the naming conventions. I don't have any better idea so I'm open to suggestions.
This commit is contained in:
commit
679fa9f2bf
16 changed files with 524 additions and 5 deletions
|
|
@ -33,6 +33,7 @@ mod iter_count;
|
|||
mod iter_next_slice;
|
||||
mod iter_nth;
|
||||
mod iter_nth_zero;
|
||||
mod iter_on_single_or_empty_collections;
|
||||
mod iter_overeager_cloned;
|
||||
mod iter_skip_next;
|
||||
mod iter_with_drain;
|
||||
|
|
@ -2304,6 +2305,69 @@ declare_clippy_lint! {
|
|||
more clearly with `if .. else ..`"
|
||||
}
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
///
|
||||
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on collections containing a single item
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
///
|
||||
/// It is simpler to use the once function from the standard library:
|
||||
///
|
||||
/// ### Example
|
||||
///
|
||||
/// ```rust
|
||||
/// let a = [123].iter();
|
||||
/// let b = Some(123).into_iter();
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// use std::iter;
|
||||
/// let a = iter::once(&123);
|
||||
/// let b = iter::once(123);
|
||||
/// ```
|
||||
///
|
||||
/// ### Known problems
|
||||
///
|
||||
/// The type of the resulting iterator might become incompatible with its usage
|
||||
#[clippy::version = "1.64.0"]
|
||||
pub ITER_ON_SINGLE_ITEMS,
|
||||
nursery,
|
||||
"Iterator for array of length 1"
|
||||
}
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
///
|
||||
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on empty collections
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
///
|
||||
/// It is simpler to use the empty function from the standard library:
|
||||
///
|
||||
/// ### Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::{slice, option};
|
||||
/// let a: slice::Iter<i32> = [].iter();
|
||||
/// let f: option::IntoIter<i32> = None.into_iter();
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// use std::iter;
|
||||
/// let a: iter::Empty<i32> = iter::empty();
|
||||
/// let b: iter::Empty<i32> = iter::empty();
|
||||
/// ```
|
||||
///
|
||||
/// ### Known problems
|
||||
///
|
||||
/// The type of the resulting iterator might become incompatible with its usage
|
||||
#[clippy::version = "1.64.0"]
|
||||
pub ITER_ON_EMPTY_COLLECTIONS,
|
||||
nursery,
|
||||
"Iterator for empty array"
|
||||
}
|
||||
|
||||
pub struct Methods {
|
||||
avoid_breaking_exported_api: bool,
|
||||
msrv: Option<RustcVersion>,
|
||||
|
|
@ -2406,6 +2470,8 @@ impl_lint_pass!(Methods => [
|
|||
NEEDLESS_OPTION_TAKE,
|
||||
NO_EFFECT_REPLACE,
|
||||
OBFUSCATED_IF_ELSE,
|
||||
ITER_ON_SINGLE_ITEMS,
|
||||
ITER_ON_EMPTY_COLLECTIONS
|
||||
]);
|
||||
|
||||
/// Extracts a method call name, args, and `Span` of the method name.
|
||||
|
|
@ -2708,6 +2774,9 @@ impl Methods {
|
|||
("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, self.msrv),
|
||||
("is_none", []) => check_is_some_is_none(cx, expr, recv, false),
|
||||
("is_some", []) => check_is_some_is_none(cx, expr, recv, true),
|
||||
("iter" | "iter_mut" | "into_iter", []) => {
|
||||
iter_on_single_or_empty_collections::check(cx, expr, name, recv);
|
||||
},
|
||||
("join", [join_arg]) => {
|
||||
if let Some(("collect", _, span)) = method_call(recv) {
|
||||
unnecessary_join::check(cx, expr, recv, join_arg, span);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue