// run-rustfix
#![warn(clippy::while_let_on_iterator)]
-#![allow(clippy::never_loop, unreachable_code, unused_mut, dead_code)]
+#![allow(
+ clippy::never_loop,
+ unreachable_code,
+ unused_mut,
+ dead_code,
+ clippy::equatable_if_let,
+ clippy::manual_find,
+ clippy::redundant_closure_call
+)]
fn base() {
let mut iter = 1..20;
// Used in outer loop, needs &mut
let mut it = 1..40;
while let Some(n) = it.next() {
- for m in &mut it {
+ for m in it.by_ref() {
if m % 10 == 0 {
break;
}
// Used after the loop, needs &mut.
let mut it = 1..40;
- for m in &mut it {
+ for m in it.by_ref() {
if m % 10 == 0 {
break;
}
let mut it = 1..40;
let mut opt = Some(0);
while let Some(n) = opt.take().or_else(|| it.next()) {
- for m in &mut it {
+ for m in it.by_ref() {
if n % 10 == 0 {
break;
}
impl<T: Iterator<Item = u32>> S<T> {
fn f(&mut self) -> Option<u32> {
// Used as a field.
- for i in &mut self.0 {
- if !(3..=7).contains(&i) {
+ for i in self.0.by_ref() {
+ if !(3..8).contains(&i) {
return Some(i);
}
}
}
}
// This one is fine, a different field is borrowed
- for i in &mut self.0.0.0 {
+ for i in self.0.0.0.by_ref() {
if i == 1 {
return self.0.1.take();
} else {
// Needs &mut, field of the iterator is accessed after the loop
let mut it = S2(1..40, 0);
- for n in &mut it {
+ for n in it.by_ref() {
if n == 0 {
break;
}
let mut it = 0..10;
let mut x = || {
// Needs &mut, the closure can be called multiple times
- for x in &mut it {
+ for x in it.by_ref() {
if x % 2 == 0 {
break;
}
x();
}
+fn issue7510() {
+ let mut it = 0..10;
+ let it = &mut it;
+ // Needs to reborrow `it` as the binding isn't mutable
+ for x in it.by_ref() {
+ if x % 2 == 0 {
+ break;
+ }
+ }
+ println!("{}", it.next().unwrap());
+
+ struct S<T>(T);
+ let mut it = 0..10;
+ let it = S(&mut it);
+ // Needs to reborrow `it.0` as the binding isn't mutable
+ for x in it.0.by_ref() {
+ if x % 2 == 0 {
+ break;
+ }
+ }
+ println!("{}", it.0.next().unwrap());
+}
+
+fn exact_match_with_single_field() {
+ struct S<T>(T);
+ let mut s = S(0..10);
+ // Don't lint. `s.0` is used inside the loop.
+ while let Some(_) = s.0.next() {
+ let _ = &mut s.0;
+ }
+}
+
+fn custom_deref() {
+ struct S1<T> {
+ x: T,
+ }
+ struct S2<T>(S1<T>);
+ impl<T> core::ops::Deref for S2<T> {
+ type Target = S1<T>;
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+ }
+ impl<T> core::ops::DerefMut for S2<T> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
+ }
+ }
+
+ let mut s = S2(S1 { x: 0..10 });
+ for x in s.x.by_ref() {
+ println!("{}", x);
+ }
+}
+
+fn issue_8113() {
+ let mut x = [0..10];
+ for x in x[0].by_ref() {
+ println!("{}", x);
+ }
+}
+
+fn fn_once_closure() {
+ let mut it = 0..10;
+ (|| {
+ for x in it {
+ if x % 2 == 0 {
+ break;
+ }
+ }
+ })();
+
+ fn f(_: impl FnOnce()) {}
+ let mut it = 0..10;
+ f(|| {
+ for x in it {
+ if x % 2 == 0 {
+ break;
+ }
+ }
+ });
+
+ fn f2(_: impl FnMut()) {}
+ let mut it = 0..10;
+ f2(|| {
+ for x in it.by_ref() {
+ if x % 2 == 0 {
+ break;
+ }
+ }
+ });
+
+ fn f3(_: fn()) {}
+ f3(|| {
+ let mut it = 0..10;
+ for x in it {
+ if x % 2 == 0 {
+ break;
+ }
+ }
+ })
+}
+
fn main() {
let mut it = 0..20;
for _ in it {