Add iter_once and iter_empty lints

This commit is contained in:
Sosthène Guédon 2022-07-16 10:22:43 +02:00
parent 8f390610a5
commit 73cd95465e
13 changed files with 383 additions and 0 deletions

View file

@ -3651,11 +3651,13 @@ Released 2018-09-13
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
[`iter_cloned_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_cloned_collect
[`iter_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_count
[`iter_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_empty
[`iter_next_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop
[`iter_next_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_slice
[`iter_not_returning_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_not_returning_iterator
[`iter_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth
[`iter_nth_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth_zero
[`iter_once`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_once
[`iter_overeager_cloned`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_overeager_cloned
[`iter_skip_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next
[`iter_with_drain`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_with_drain

View file

@ -0,0 +1,164 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use rustc_ast::ast::{Expr, ExprKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
/// ### What it does
///
/// Checks for usage of:
///
/// - `[foo].iter()`
/// - `[foo].iter_mut()`
/// - `[foo].into_iter()`
/// - `Some(foo).iter()`
/// - `Some(foo).iter_mut()`
/// - `Some(foo).into_iter()`
///
/// ### 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_ONCE,
nursery,
"Iterator for array of length 1"
}
declare_clippy_lint! {
/// ### What it does
///
/// Checks for usage of:
///
/// - `[].iter()`
/// - `[].iter_mut()`
/// - `[].into_iter()`
/// - `None.iter()`
/// - `None.iter_mut()`
/// - `None.into_iter()`
///
/// ### 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_EMPTY,
nursery,
"Iterator for empty array"
}
declare_lint_pass!(IterOnceEmpty => [ITER_ONCE, ITER_EMPTY]);
impl EarlyLintPass for IterOnceEmpty {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if expr.span.from_expansion() {
// Don't lint match expressions present in
// macro_rules! block
return;
}
let (method_name, args) = if let ExprKind::MethodCall(seg, args, _) = &expr.kind {
(seg.ident.as_str(), args)
} else {
return;
};
let arg = if args.len() == 1 {
&args[0]
} else {
return;
};
let item = match &arg.kind {
ExprKind::Array(v) if v.len() <= 1 => v.first(),
ExprKind::Path(None, p) => {
if p.segments.len() == 1 && p.segments[0].ident.name == rustc_span::sym::None {
None
} else {
return;
}
},
ExprKind::Call(f, some_args) if some_args.len() == 1 => {
if let ExprKind::Path(None, p) = &f.kind {
if p.segments.len() == 1 && p.segments[0].ident.name == rustc_span::sym::Some {
Some(&some_args[0])
} else {
return;
}
} else {
return;
}
},
_ => return,
};
if let Some(i) = item {
let (sugg, msg) = match method_name {
"iter" => (
format!("std::iter::once(&{})", snippet(cx, i.span, "...")),
"this `iter` call can be replaced with std::iter::once",
),
"iter_mut" => (
format!("std::iter::once(&mut {})", snippet(cx, i.span, "...")),
"this `iter_mut` call can be replaced with std::iter::once",
),
"into_iter" => (
format!("std::iter::once({})", snippet(cx, i.span, "...")),
"this `into_iter` call can be replaced with std::iter::once",
),
_ => return,
};
span_lint_and_sugg(cx, ITER_ONCE, expr.span, msg, "try", sugg, Applicability::Unspecified);
} else {
let msg = match method_name {
"iter" => "this `iter call` can be replaced with std::iter::empty",
"iter_mut" => "this `iter_mut` call can be replaced with std::iter::empty",
"into_iter" => "this `into_iter` call can be replaced with std::iter::empty",
_ => return,
};
span_lint_and_sugg(
cx,
ITER_EMPTY,
expr.span,
msg,
"try",
"std::iter::empty()".to_string(),
Applicability::Unspecified,
);
}
}
}

View file

@ -200,6 +200,8 @@ store.register_lints(&[
invalid_utf8_in_unchecked::INVALID_UTF8_IN_UNCHECKED,
items_after_statements::ITEMS_AFTER_STATEMENTS,
iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR,
iter_once_empty::ITER_EMPTY,
iter_once_empty::ITER_ONCE,
large_const_arrays::LARGE_CONST_ARRAYS,
large_enum_variant::LARGE_ENUM_VARIANT,
large_include_file::LARGE_INCLUDE_FILE,

View file

@ -14,6 +14,8 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
LintId::of(index_refutable_slice::INDEX_REFUTABLE_SLICE),
LintId::of(let_if_seq::USELESS_LET_IF_SEQ),
LintId::of(matches::SIGNIFICANT_DROP_IN_SCRUTINEE),
LintId::of(methods::ITER_EMPTY),
LintId::of(methods::ITER_ONCE),
LintId::of(methods::ITER_WITH_DRAIN),
LintId::of(missing_const_for_fn::MISSING_CONST_FOR_FN),
LintId::of(mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),

View file

@ -41,6 +41,8 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
LintId::of(invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS),
LintId::of(items_after_statements::ITEMS_AFTER_STATEMENTS),
LintId::of(iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR),
LintId::of(iter_once_empty::ITER_EMPTY),
LintId::of(iter_once_empty::ITER_ONCE),
LintId::of(large_stack_arrays::LARGE_STACK_ARRAYS),
LintId::of(let_underscore::LET_UNDERSCORE_DROP),
LintId::of(literal_representation::LARGE_DIGIT_GROUPS),

View file

@ -258,6 +258,7 @@ mod invalid_upcast_comparisons;
mod invalid_utf8_in_unchecked;
mod items_after_statements;
mod iter_not_returning_iterator;
mod iter_once_empty;
mod large_const_arrays;
mod large_enum_variant;
mod large_include_file;
@ -931,6 +932,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| Box::new(invalid_utf8_in_unchecked::InvalidUtf8InUnchecked));
store.register_late_pass(|| Box::new(std_instead_of_core::StdReexports::default()));
store.register_late_pass(|| Box::new(manual_instant_elapsed::ManualInstantElapsed));
store.register_early_pass(|| Box::new(iter_once_empty::IterOnceEmpty));
// add lints here, do not remove this comment, it's used in `new_lint`
}

View file

@ -484,6 +484,7 @@ pub fn def_path_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
}
fn find_primitive<'tcx>(tcx: TyCtxt<'tcx>, name: &str) -> impl Iterator<Item = DefId> + 'tcx {
let single = |ty| tcx.incoherent_impls(ty).iter().copied();
#[allow(clippy::iter_empty)]
let empty = || [].iter().copied();
match name {
"bool" => single(BoolSimplifiedType),

32
tests/ui/iter_empty.fixed Normal file
View file

@ -0,0 +1,32 @@
// run-rustfix
#![warn(clippy::iter_empty)]
#![allow(clippy::iter_next_slice, clippy::redundant_clone)]
fn array() {
assert_eq!(std::iter::empty().next(), Option::<i32>::None);
assert_eq!(std::iter::empty().next(), Option::<&mut i32>::None);
assert_eq!(std::iter::empty().next(), Option::<&i32>::None);
assert_eq!(std::iter::empty().next(), Option::<i32>::None);
assert_eq!(std::iter::empty().next(), Option::<&mut i32>::None);
assert_eq!(std::iter::empty().next(), Option::<&i32>::None);
// Don't trigger on non-iter methods
let _: Option<String> = None.clone();
let _: [String; 0] = [].clone();
}
macro_rules! in_macros {
() => {
assert_eq!([].into_iter().next(), Option::<i32>::None);
assert_eq!([].iter_mut().next(), Option::<&mut i32>::None);
assert_eq!([].iter().next(), Option::<&i32>::None);
assert_eq!(None.into_iter().next(), Option::<i32>::None);
assert_eq!(None.iter_mut().next(), Option::<&mut i32>::None);
assert_eq!(None.iter().next(), Option::<&i32>::None);
};
}
fn main() {
array();
in_macros!();
}

32
tests/ui/iter_empty.rs Normal file
View file

@ -0,0 +1,32 @@
// run-rustfix
#![warn(clippy::iter_empty)]
#![allow(clippy::iter_next_slice, clippy::redundant_clone)]
fn array() {
assert_eq!([].into_iter().next(), Option::<i32>::None);
assert_eq!([].iter_mut().next(), Option::<&mut i32>::None);
assert_eq!([].iter().next(), Option::<&i32>::None);
assert_eq!(None.into_iter().next(), Option::<i32>::None);
assert_eq!(None.iter_mut().next(), Option::<&mut i32>::None);
assert_eq!(None.iter().next(), Option::<&i32>::None);
// Don't trigger on non-iter methods
let _: Option<String> = None.clone();
let _: [String; 0] = [].clone();
}
macro_rules! in_macros {
() => {
assert_eq!([].into_iter().next(), Option::<i32>::None);
assert_eq!([].iter_mut().next(), Option::<&mut i32>::None);
assert_eq!([].iter().next(), Option::<&i32>::None);
assert_eq!(None.into_iter().next(), Option::<i32>::None);
assert_eq!(None.iter_mut().next(), Option::<&mut i32>::None);
assert_eq!(None.iter().next(), Option::<&i32>::None);
};
}
fn main() {
array();
in_macros!();
}

View file

@ -0,0 +1,40 @@
error: this `into_iter` call can be replaced with std::iter::empty
--> $DIR/iter_empty.rs:6:16
|
LL | assert_eq!([].into_iter().next(), Option::<i32>::None);
| ^^^^^^^^^^^^^^ help: try: `std::iter::empty()`
|
= note: `-D clippy::iter-empty` implied by `-D warnings`
error: this `iter_mut` call can be replaced with std::iter::empty
--> $DIR/iter_empty.rs:7:16
|
LL | assert_eq!([].iter_mut().next(), Option::<&mut i32>::None);
| ^^^^^^^^^^^^^ help: try: `std::iter::empty()`
error: this `iter call` can be replaced with std::iter::empty
--> $DIR/iter_empty.rs:8:16
|
LL | assert_eq!([].iter().next(), Option::<&i32>::None);
| ^^^^^^^^^ help: try: `std::iter::empty()`
error: this `into_iter` call can be replaced with std::iter::empty
--> $DIR/iter_empty.rs:9:16
|
LL | assert_eq!(None.into_iter().next(), Option::<i32>::None);
| ^^^^^^^^^^^^^^^^ help: try: `std::iter::empty()`
error: this `iter_mut` call can be replaced with std::iter::empty
--> $DIR/iter_empty.rs:10:16
|
LL | assert_eq!(None.iter_mut().next(), Option::<&mut i32>::None);
| ^^^^^^^^^^^^^^^ help: try: `std::iter::empty()`
error: this `iter call` can be replaced with std::iter::empty
--> $DIR/iter_empty.rs:11:16
|
LL | assert_eq!(None.iter().next(), Option::<&i32>::None);
| ^^^^^^^^^^^ help: try: `std::iter::empty()`
error: aborting due to 6 previous errors

32
tests/ui/iter_once.fixed Normal file
View file

@ -0,0 +1,32 @@
// run-rustfix
#![warn(clippy::iter_once)]
#![allow(clippy::iter_next_slice, clippy::redundant_clone)]
fn array() {
assert_eq!(std::iter::once(123).next(), Some(123));
assert_eq!(std::iter::once(&mut 123).next(), Some(&mut 123));
assert_eq!(std::iter::once(&123).next(), Some(&123));
assert_eq!(std::iter::once(123).next(), Some(123));
assert_eq!(std::iter::once(&mut 123).next(), Some(&mut 123));
assert_eq!(std::iter::once(&123).next(), Some(&123));
// Don't trigger on non-iter methods
let _: Option<String> = Some("test".to_string()).clone();
let _: [String; 1] = ["test".to_string()].clone();
}
macro_rules! in_macros {
() => {
assert_eq!([123].into_iter().next(), Some(123));
assert_eq!([123].iter_mut().next(), Some(&mut 123));
assert_eq!([123].iter().next(), Some(&123));
assert_eq!(Some(123).into_iter().next(), Some(123));
assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
assert_eq!(Some(123).iter().next(), Some(&123));
};
}
fn main() {
array();
in_macros!();
}

32
tests/ui/iter_once.rs Normal file
View file

@ -0,0 +1,32 @@
// run-rustfix
#![warn(clippy::iter_once)]
#![allow(clippy::iter_next_slice, clippy::redundant_clone)]
fn array() {
assert_eq!([123].into_iter().next(), Some(123));
assert_eq!([123].iter_mut().next(), Some(&mut 123));
assert_eq!([123].iter().next(), Some(&123));
assert_eq!(Some(123).into_iter().next(), Some(123));
assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
assert_eq!(Some(123).iter().next(), Some(&123));
// Don't trigger on non-iter methods
let _: Option<String> = Some("test".to_string()).clone();
let _: [String; 1] = ["test".to_string()].clone();
}
macro_rules! in_macros {
() => {
assert_eq!([123].into_iter().next(), Some(123));
assert_eq!([123].iter_mut().next(), Some(&mut 123));
assert_eq!([123].iter().next(), Some(&123));
assert_eq!(Some(123).into_iter().next(), Some(123));
assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
assert_eq!(Some(123).iter().next(), Some(&123));
};
}
fn main() {
array();
in_macros!();
}

40
tests/ui/iter_once.stderr Normal file
View file

@ -0,0 +1,40 @@
error: this `into_iter` call can be replaced with std::iter::once
--> $DIR/iter_once.rs:6:16
|
LL | assert_eq!([123].into_iter().next(), Some(123));
| ^^^^^^^^^^^^^^^^^ help: try: `std::iter::once(123)`
|
= note: `-D clippy::iter-once` implied by `-D warnings`
error: this `iter_mut` call can be replaced with std::iter::once
--> $DIR/iter_once.rs:7:16
|
LL | assert_eq!([123].iter_mut().next(), Some(&mut 123));
| ^^^^^^^^^^^^^^^^ help: try: `std::iter::once(&mut 123)`
error: this `iter` call can be replaced with std::iter::once
--> $DIR/iter_once.rs:8:16
|
LL | assert_eq!([123].iter().next(), Some(&123));
| ^^^^^^^^^^^^ help: try: `std::iter::once(&123)`
error: this `into_iter` call can be replaced with std::iter::once
--> $DIR/iter_once.rs:9:16
|
LL | assert_eq!(Some(123).into_iter().next(), Some(123));
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `std::iter::once(123)`
error: this `iter_mut` call can be replaced with std::iter::once
--> $DIR/iter_once.rs:10:16
|
LL | assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
| ^^^^^^^^^^^^^^^^^^^^ help: try: `std::iter::once(&mut 123)`
error: this `iter` call can be replaced with std::iter::once
--> $DIR/iter_once.rs:11:16
|
LL | assert_eq!(Some(123).iter().next(), Some(&123));
| ^^^^^^^^^^^^^^^^ help: try: `std::iter::once(&123)`
error: aborting due to 6 previous errors