diff options
| author | bors <bors@rust-lang.org> | 2021-04-26 14:28:31 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-04-26 14:28:31 +0000 |
| commit | 1bb3b12dfb6f7f100acec3a318bf7e5011d400a4 (patch) | |
| tree | a58d17dd559f076cb228b34e3c959831c8160b4b | |
| parent | c4e2d3691bc8374d8749fc4b8e2fd5774df950f6 (diff) | |
| parent | efc4c6c957ea2b0d870f4728fa934042213da5e8 (diff) | |
| download | rust-1bb3b12dfb6f7f100acec3a318bf7e5011d400a4.tar.gz rust-1bb3b12dfb6f7f100acec3a318bf7e5011d400a4.zip | |
Auto merge of #7132 - rust-lang:single_element_loop_iter, r=Manishearth
extend `single_element_loop` to match `.iter()` This extends `single_element_loop` to also match `[..].iter()` in the loop argument. Related to #7125, but not completely fixing it due to the lint only firing if the array expression contains a local variable. --- changelog: none
| -rw-r--r-- | clippy_lints/src/loops/single_element_loop.rs | 8 | ||||
| -rw-r--r-- | tests/ui/single_element_loop.fixed | 5 | ||||
| -rw-r--r-- | tests/ui/single_element_loop.rs | 4 | ||||
| -rw-r--r-- | tests/ui/single_element_loop.stderr | 18 |
4 files changed, 33 insertions, 2 deletions
diff --git a/clippy_lints/src/loops/single_element_loop.rs b/clippy_lints/src/loops/single_element_loop.rs index fc067e81bca..0fd09ff7197 100644 --- a/clippy_lints/src/loops/single_element_loop.rs +++ b/clippy_lints/src/loops/single_element_loop.rs @@ -14,8 +14,14 @@ pub(super) fn check<'tcx>( body: &'tcx Expr<'_>, expr: &'tcx Expr<'_>, ) { + let arg_expr = match arg.kind { + ExprKind::AddrOf(BorrowKind::Ref, _, ref_arg) => ref_arg, + ExprKind::MethodCall(method, _, args, _) if args.len() == 1 && method.ident.name == rustc_span::sym::iter => { + &args[0] + }, + _ => return, + }; if_chain! { - if let ExprKind::AddrOf(BorrowKind::Ref, _, arg_expr) = arg.kind; if let PatKind::Binding(.., target, _) = pat.kind; if let ExprKind::Array([arg_expression]) = arg_expr.kind; if let ExprKind::Path(ref list_item) = arg_expression.kind; diff --git a/tests/ui/single_element_loop.fixed b/tests/ui/single_element_loop.fixed index 8ca068293a6..c307afffcb8 100644 --- a/tests/ui/single_element_loop.fixed +++ b/tests/ui/single_element_loop.fixed @@ -8,4 +8,9 @@ fn main() { let item = &item1; println!("{}", item); } + + { + let item = &item1; + println!("{:?}", item); + } } diff --git a/tests/ui/single_element_loop.rs b/tests/ui/single_element_loop.rs index 57e9336a31f..2c0c03b7211 100644 --- a/tests/ui/single_element_loop.rs +++ b/tests/ui/single_element_loop.rs @@ -7,4 +7,8 @@ fn main() { for item in &[item1] { println!("{}", item); } + + for item in [item1].iter() { + println!("{:?}", item); + } } diff --git a/tests/ui/single_element_loop.stderr b/tests/ui/single_element_loop.stderr index 90be1dc3283..0e35a33ded5 100644 --- a/tests/ui/single_element_loop.stderr +++ b/tests/ui/single_element_loop.stderr @@ -15,5 +15,21 @@ LL | println!("{}", item); LL | } | -error: aborting due to previous error +error: for loop over a single element + --> $DIR/single_element_loop.rs:11:5 + | +LL | / for item in [item1].iter() { +LL | | println!("{:?}", item); +LL | | } + | |_____^ + | +help: try + | +LL | { +LL | let item = &item1; +LL | println!("{:?}", item); +LL | } + | + +error: aborting due to 2 previous errors |
