Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend iter_on lints to catch more cases #11038

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use rustc_trait_selection::infer::InferCtxtExt as _;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
use rustc_trait_selection::traits::{Obligation, ObligationCause};
use std::collections::VecDeque;
use std::iter::once;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -455,7 +456,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
&& cx.tcx.infer_ctxt().build()
.type_implements_trait(
trait_id,
[impl_ty.into()].into_iter().chain(args.iter().copied()),
once(impl_ty.into()).chain(args.iter().copied()),
cx.param_env,
)
.must_apply_modulo_regions()
Expand Down
8 changes: 5 additions & 3 deletions clippy_lints/src/manual_strip.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::iter::once;

use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::msrvs::{self, Msrv};
Expand Down Expand Up @@ -112,11 +114,11 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
multispan_sugg(
diag,
&format!("try using the `strip_{kind_word}` method"),
vec![(test_span,
once((test_span,
format!("if let Some(<stripped>) = {}.strip_{kind_word}({}) ",
snippet(cx, target_arg.span, ".."),
snippet(cx, pattern.span, "..")))]
.into_iter().chain(strippings.into_iter().map(|span| (span, "<stripped>".into()))),
snippet(cx, pattern.span, ".."))))
.chain(strippings.into_iter().map(|span| (span, "<stripped>".into()))),
);
});
}
Expand Down
92 changes: 0 additions & 92 deletions clippy_lints/src/methods/iter_on_single_or_empty_collections.rs

This file was deleted.

35 changes: 13 additions & 22 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ mod iter_kv_map;
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_skip_zero;
Expand Down Expand Up @@ -85,6 +84,7 @@ mod single_char_add_str;
mod single_char_insert_string;
mod single_char_pattern;
mod single_char_push_string;
mod single_or_empty_collections_iter;
mod skip_while_next;
mod stable_sort_primitive;
mod str_splitn;
Expand Down Expand Up @@ -2432,15 +2432,15 @@ declare_clippy_lint! {

declare_clippy_lint! {
/// ### What it does
///
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on collections containing a single item
/// 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// It is simpler to use the once function from the standard library.
/// It is simpler to use the [`once`](https://doc.rust-lang.org/std/iter/fn.once.html) function from the standard library.

///
/// It is simpler to use the once function from the standard library:
/// ### Known problems
/// The type of the resulting iterator might become incompatible with its usage.
Comment on lines +2440 to +2441
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably mention the reason, that it is a core::iter::sources::once::Once<T> instead of a core::slice::iter::Iter<T>, but it should work fine as Once also implements Iterator

///
/// ### Example
///
/// ```rust
/// let a = [123].iter();
/// let b = Some(123).into_iter();
Expand All @@ -2451,27 +2451,23 @@ declare_clippy_lint! {
/// 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.65.0"]
pub ITER_ON_SINGLE_ITEMS,
nursery,
pedantic,
"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
/// 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.
///
/// It is simpler to use the empty function from the standard library:
/// ### Known problems
/// The type of the resulting iterator might become incompatible with its usage.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Same as comment above 🐮 )

///
/// ### Example
///
/// ```rust
/// use std::{slice, option};
/// let a: slice::Iter<i32> = [].iter();
Expand All @@ -2483,13 +2479,9 @@ declare_clippy_lint! {
/// 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.65.0"]
pub ITER_ON_EMPTY_COLLECTIONS,
nursery,
pedantic,
"Iterator for empty array"
}

Expand Down Expand Up @@ -3695,6 +3687,8 @@ fn method_call<'tcx>(

impl<'tcx> LateLintPass<'tcx> for Methods {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
single_or_empty_collections_iter::check(cx, expr, &self.msrv);
Centri3 marked this conversation as resolved.
Show resolved Hide resolved

if expr.span.from_expansion() {
return;
}
Expand Down Expand Up @@ -3991,9 +3985,6 @@ 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);
Expand Down
Loading