Skip to content

Commit 8b8bea6

Browse files
committed
Extend iter_on lints to catch more cases
1 parent 4b355d8 commit 8b8bea6

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

+779
-359
lines changed

clippy_lints/src/dereference.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_trait_selection::infer::InferCtxtExt as _;
3535
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
3636
use rustc_trait_selection::traits::{Obligation, ObligationCause};
3737
use std::collections::VecDeque;
38+
use std::iter::once;
3839

3940
declare_clippy_lint! {
4041
/// ### What it does
@@ -901,7 +902,7 @@ fn walk_parents<'tcx>(
901902
&& infcx
902903
.type_implements_trait(
903904
trait_id,
904-
[impl_ty.into()].into_iter().chain(subs.iter().copied()),
905+
once(impl_ty.into()).chain(subs.iter().copied()),
905906
cx.param_env,
906907
)
907908
.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: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ mod iter_kv_map;
4141
mod iter_next_slice;
4242
mod iter_nth;
4343
mod iter_nth_zero;
44-
mod iter_on_single_or_empty_collections;
4544
mod iter_overeager_cloned;
4645
mod iter_skip_next;
4746
mod iter_with_drain;
@@ -81,6 +80,7 @@ mod single_char_add_str;
8180
mod single_char_insert_string;
8281
mod single_char_pattern;
8382
mod single_char_push_string;
83+
mod single_or_empty_collections_iter;
8484
mod skip_while_next;
8585
mod stable_sort_primitive;
8686
mod str_splitn;
@@ -2417,15 +2417,15 @@ declare_clippy_lint! {
24172417

24182418
declare_clippy_lint! {
24192419
/// ### What it does
2420-
///
2421-
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on collections containing a single item
2420+
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on collections containing a single item.
24222421
///
24232422
/// ### Why is this bad?
2423+
/// It is simpler to use the once function from the standard library.
24242424
///
2425-
/// It is simpler to use the once function from the standard library:
2425+
/// ### Known problems
2426+
/// The type of the resulting iterator might become incompatible with its usage.
24262427
///
24272428
/// ### Example
2428-
///
24292429
/// ```rust
24302430
/// let a = [123].iter();
24312431
/// let b = Some(123).into_iter();
@@ -2436,27 +2436,23 @@ declare_clippy_lint! {
24362436
/// let a = iter::once(&123);
24372437
/// let b = iter::once(123);
24382438
/// ```
2439-
///
2440-
/// ### Known problems
2441-
///
2442-
/// The type of the resulting iterator might become incompatible with its usage
24432439
#[clippy::version = "1.65.0"]
24442440
pub ITER_ON_SINGLE_ITEMS,
2445-
nursery,
2441+
pedantic,
24462442
"Iterator for array of length 1"
24472443
}
24482444

24492445
declare_clippy_lint! {
24502446
/// ### What it does
2451-
///
2452-
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on empty collections
2447+
/// Checks for calls to `iter`, `iter_mut` or `into_iter` on empty collections.
24532448
///
24542449
/// ### Why is this bad?
2450+
/// It is simpler to use the empty function from the standard library.
24552451
///
2456-
/// It is simpler to use the empty function from the standard library:
2452+
/// ### Known problems
2453+
/// The type of the resulting iterator might become incompatible with its usage.
24572454
///
24582455
/// ### Example
2459-
///
24602456
/// ```rust
24612457
/// use std::{slice, option};
24622458
/// let a: slice::Iter<i32> = [].iter();
@@ -2468,13 +2464,9 @@ declare_clippy_lint! {
24682464
/// let a: iter::Empty<i32> = iter::empty();
24692465
/// let b: iter::Empty<i32> = iter::empty();
24702466
/// ```
2471-
///
2472-
/// ### Known problems
2473-
///
2474-
/// The type of the resulting iterator might become incompatible with its usage
24752467
#[clippy::version = "1.65.0"]
24762468
pub ITER_ON_EMPTY_COLLECTIONS,
2477-
nursery,
2469+
pedantic,
24782470
"Iterator for empty array"
24792471
}
24802472

@@ -3529,6 +3521,8 @@ fn method_call<'tcx>(
35293521

35303522
impl<'tcx> LateLintPass<'tcx> for Methods {
35313523
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
3524+
single_or_empty_collections_iter::check(cx, expr, &self.msrv);
3525+
35323526
if expr.span.from_expansion() {
35333527
return;
35343528
}
@@ -3825,9 +3819,6 @@ impl Methods {
38253819
("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, &self.msrv),
38263820
("is_none", []) => check_is_some_is_none(cx, expr, recv, false),
38273821
("is_some", []) => check_is_some_is_none(cx, expr, recv, true),
3828-
("iter" | "iter_mut" | "into_iter", []) => {
3829-
iter_on_single_or_empty_collections::check(cx, expr, name, recv);
3830-
},
38313822
("join", [join_arg]) => {
38323823
if let Some(("collect", _, _, span, _)) = method_call(recv) {
38333824
unnecessary_join::check(cx, expr, recv, join_arg, span);
@@ -4204,7 +4195,7 @@ impl SelfKind {
42044195
};
42054196

42064197
let Some(trait_def_id) = cx.tcx.get_diagnostic_item(trait_sym) else {
4207-
return false
4198+
return false;
42084199
};
42094200
implements_trait(cx, ty, trait_def_id, &[parent_ty.into()])
42104201
}

0 commit comments

Comments
 (0)