suggest alternatives to iterate an array of ranges

Co-authored-by: ThinkerDreamer <74881094+ThinkerDreamer@users.noreply.github.com>
This commit is contained in:
Christoph Beberweil 2023-11-23 23:07:36 +01:00
parent 840e227bb6
commit 447edf92b4
3 changed files with 43 additions and 74 deletions

View File

@ -1,6 +1,6 @@
use super::SINGLE_ELEMENT_LOOP;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::{indent_of, snippet_with_applicability};
use clippy_utils::source::{indent_of, snippet, snippet_with_applicability};
use clippy_utils::visitors::contains_break_or_continue;
use rustc_ast::util::parser::PREC_PREFIX;
use rustc_ast::Mutability;
@ -87,6 +87,18 @@ pub(super) fn check<'tcx>(
arg_snip = format!("({arg_snip})").into();
}
if clippy_utils::higher::Range::hir(arg_expression).is_some() {
let sugg = snippet(cx, arg_expression.span, "..");
span_lint_and_sugg(
cx,
SINGLE_ELEMENT_LOOP,
arg.span,
"for loop over a single range inside an array, rather than iterating over the elements in the range directly",
"did you mean to iterate over the range instead?",
sugg.to_string(),
applicability,
);
} else {
span_lint_and_sugg(
cx,
SINGLE_ELEMENT_LOOP,
@ -98,3 +110,4 @@ pub(super) fn check<'tcx>(
);
}
}
}

View File

@ -15,23 +15,19 @@ fn main() {
dbg!(item);
}
{
let item = &(0..5);
for item in 0..5 {
dbg!(item);
}
{
let item = &mut (0..5);
for item in 0..5 {
dbg!(item);
}
{
let item = 0..5;
for item in 0..5 {
dbg!(item);
}
{
let item = 0..5;
for item in 0..5 {
dbg!(item);
}

View File

@ -32,69 +32,29 @@ LL + dbg!(item);
LL + }
|
error: for loop over a single element
--> $DIR/single_element_loop.rs:16:5
|
LL | / for item in &[0..5] {
LL | | dbg!(item);
LL | | }
| |_____^
|
help: try
|
LL ~ {
LL + let item = &(0..5);
LL + dbg!(item);
LL + }
error: for loop over a single range inside an array, rather than iterating over the elements in the range directly
--> $DIR/single_element_loop.rs:16:17
|
LL | for item in &[0..5] {
| ^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
error: for loop over a single element
--> $DIR/single_element_loop.rs:20:5
|
LL | / for item in [0..5].iter_mut() {
LL | | dbg!(item);
LL | | }
| |_____^
|
help: try
|
LL ~ {
LL + let item = &mut (0..5);
LL + dbg!(item);
LL + }
error: for loop over a single range inside an array, rather than iterating over the elements in the range directly
--> $DIR/single_element_loop.rs:20:17
|
LL | for item in [0..5].iter_mut() {
| ^^^^^^^^^^^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
error: for loop over a single element
--> $DIR/single_element_loop.rs:24:5
|
LL | / for item in [0..5] {
LL | | dbg!(item);
LL | | }
| |_____^
|
help: try
|
LL ~ {
LL + let item = 0..5;
LL + dbg!(item);
LL + }
error: for loop over a single range inside an array, rather than iterating over the elements in the range directly
--> $DIR/single_element_loop.rs:24:17
|
LL | for item in [0..5] {
| ^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
error: for loop over a single element
--> $DIR/single_element_loop.rs:28:5
|
LL | / for item in [0..5].into_iter() {
LL | | dbg!(item);
LL | | }
| |_____^
|
help: try
|
LL ~ {
LL + let item = 0..5;
LL + dbg!(item);
LL + }
error: for loop over a single range inside an array, rather than iterating over the elements in the range directly
--> $DIR/single_element_loop.rs:28:17
|
LL | for item in [0..5].into_iter() {
| ^^^^^^^^^^^^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
error: for loop over a single element
--> $DIR/single_element_loop.rs:47:5