Skip to content

Commit d77a4ac

Browse files
committed
Extend iter_on lints to catch more cases
1 parent a4e64ff commit d77a4ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+759
-338
lines changed

clippy_lints/src/dereference.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use rustc_trait_selection::infer::InferCtxtExt as _;
3737
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
3838
use rustc_trait_selection::traits::{Obligation, ObligationCause};
3939
use std::collections::VecDeque;
40+
use std::iter::once;
4041

4142
declare_clippy_lint! {
4243
/// ### What it does
@@ -455,7 +456,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
455456
&& cx.tcx.infer_ctxt().build()
456457
.type_implements_trait(
457458
trait_id,
458-
[impl_ty.into()].into_iter().chain(subs.iter().copied()),
459+
once(impl_ty.into()).chain(subs.iter().copied()),
459460
cx.param_env,
460461
)
461462
.must_apply_modulo_regions()

clippy_lints/src/manual_strip.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::iter::once;
2+
13
use clippy_utils::consts::{constant, Constant};
24
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
35
use clippy_utils::msrvs::{self, Msrv};
@@ -112,11 +114,11 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
112114
multispan_sugg(
113115
diag,
114116
&format!("try using the `strip_{kind_word}` method"),
115-
vec![(test_span,
117+
once((test_span,
116118
format!("if let Some(<stripped>) = {}.strip_{kind_word}({}) ",
117119
snippet(cx, target_arg.span, ".."),
118-
snippet(cx, pattern.span, "..")))]
119-
.into_iter().chain(strippings.into_iter().map(|span| (span, "<stripped>".into()))),
120+
snippet(cx, pattern.span, ".."))))
121+
.chain(strippings.into_iter().map(|span| (span, "<stripped>".into()))),
120122
);
121123
});
122124
}

clippy_lints/src/methods/iter_on_single_or_empty_collections.rs

Lines changed: 0 additions & 92 deletions
This file was deleted.

clippy_lints/src/methods/mod.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ mod iter_kv_map;
4242
mod iter_next_slice;
4343
mod iter_nth;
4444
mod iter_nth_zero;
45-
mod iter_on_single_or_empty_collections;
4645
mod iter_overeager_cloned;
4746
mod iter_skip_next;
4847
mod iter_skip_zero;
@@ -83,6 +82,7 @@ mod single_char_add_str;
8382
mod single_char_insert_string;
8483
mod single_char_pattern;
8584
mod single_char_push_string;
85+
mod single_or_empty_collections_iter;
8686
mod skip_while_next;
8787
mod stable_sort_primitive;
8888
mod str_splitn;
@@ -2420,15 +2420,15 @@ declare_clippy_lint! {
24202420

24212421
declare_clippy_lint! {
24222422
/// ### What it does
2423-
///
2424-
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on collections containing a single item
2423+
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on collections containing a single item.
24252424
///
24262425
/// ### Why is this bad?
2426+
/// It is simpler to use the once function from the standard library.
24272427
///
2428-
/// It is simpler to use the once function from the standard library:
2428+
/// ### Known problems
2429+
/// The type of the resulting iterator might become incompatible with its usage.
24292430
///
24302431
/// ### Example
2431-
///
24322432
/// ```rust
24332433
/// let a = [123].iter();
24342434
/// let b = Some(123).into_iter();
@@ -2439,27 +2439,23 @@ declare_clippy_lint! {
24392439
/// let a = iter::once(&123);
24402440
/// let b = iter::once(123);
24412441
/// ```
2442-
///
2443-
/// ### Known problems
2444-
///
2445-
/// The type of the resulting iterator might become incompatible with its usage
24462442
#[clippy::version = "1.65.0"]
24472443
pub ITER_ON_SINGLE_ITEMS,
2448-
nursery,
2444+
pedantic,
24492445
"Iterator for array of length 1"
24502446
}
24512447

24522448
declare_clippy_lint! {
24532449
/// ### What it does
2454-
///
2455-
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on empty collections
2450+
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on empty collections.
24562451
///
24572452
/// ### Why is this bad?
2453+
/// It is simpler to use the empty function from the standard library.
24582454
///
2459-
/// It is simpler to use the empty function from the standard library:
2455+
/// ### Known problems
2456+
/// The type of the resulting iterator might become incompatible with its usage.
24602457
///
24612458
/// ### Example
2462-
///
24632459
/// ```rust
24642460
/// use std::{slice, option};
24652461
/// let a: slice::Iter<i32> = [].iter();
@@ -2471,13 +2467,9 @@ declare_clippy_lint! {
24712467
/// let a: iter::Empty<i32> = iter::empty();
24722468
/// let b: iter::Empty<i32> = iter::empty();
24732469
/// ```
2474-
///
2475-
/// ### Known problems
2476-
///
2477-
/// The type of the resulting iterator might become incompatible with its usage
24782470
#[clippy::version = "1.65.0"]
24792471
pub ITER_ON_EMPTY_COLLECTIONS,
2480-
nursery,
2472+
pedantic,
24812473
"Iterator for empty array"
24822474
}
24832475

@@ -3619,6 +3611,8 @@ fn method_call<'tcx>(
36193611

36203612
impl<'tcx> LateLintPass<'tcx> for Methods {
36213613
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
3614+
single_or_empty_collections_iter::check(cx, expr, &self.msrv);
3615+
36223616
if expr.span.from_expansion() {
36233617
return;
36243618
}
@@ -3916,9 +3910,6 @@ impl Methods {
39163910
("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, &self.msrv),
39173911
("is_none", []) => check_is_some_is_none(cx, expr, recv, false),
39183912
("is_some", []) => check_is_some_is_none(cx, expr, recv, true),
3919-
("iter" | "iter_mut" | "into_iter", []) => {
3920-
iter_on_single_or_empty_collections::check(cx, expr, name, recv);
3921-
},
39223913
("join", [join_arg]) => {
39233914
if let Some(("collect", _, _, span, _)) = method_call(recv) {
39243915
unnecessary_join::check(cx, expr, recv, join_arg, span);

0 commit comments

Comments
 (0)