Skip to content

Commit d92da0f

Browse files
authored
manual_slice_fill: do not initialize from the iterator (#14191)
```rust let mut tmp = vec![1, 2, 3]; for b in &mut tmp { *b = !*b; } ``` must not suggest the invalid `tmp.fill(!*b)`. In addition, there is another commit which cleans up two function calls with no effect. Fix #14189 changelog: [`manual_slice_fill`]: ensure that the initialization expression doesn't reference the iterator
2 parents d5488b3 + 44aa75f commit d92da0f

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

clippy_lints/src/loops/manual_slice_fill.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
3-
use clippy_utils::macros::span_is_local;
43
use clippy_utils::msrvs::{self, Msrv};
54
use clippy_utils::source::{HasSession, snippet_with_applicability};
65
use clippy_utils::ty::implements_trait;
@@ -55,7 +54,6 @@ pub(super) fn check<'tcx>(
5554
&& !assignval.span.from_expansion()
5655
// It is generally not equivalent to use the `fill` method if `assignval` can have side effects
5756
&& switch_to_eager_eval(cx, assignval)
58-
&& span_is_local(assignval.span)
5957
// The `fill` method requires that the slice's element type implements the `Clone` trait.
6058
&& let Some(clone_trait) = cx.tcx.lang_items().clone_trait()
6159
&& implements_trait(cx, cx.typeck_results().expr_ty(slice), clone_trait, &[])
@@ -78,7 +76,8 @@ pub(super) fn check<'tcx>(
7876
&& local == pat.hir_id
7977
&& !assignval.span.from_expansion()
8078
&& switch_to_eager_eval(cx, assignval)
81-
&& span_is_local(assignval.span)
79+
// `assignval` must not reference the iterator
80+
&& !is_local_used(cx, assignval, local)
8281
// The `fill` method cannot be used if the slice's element type does not implement the `Clone` trait.
8382
&& let Some(clone_trait) = cx.tcx.lang_items().clone_trait()
8483
&& implements_trait(cx, cx.typeck_results().expr_ty(recv), clone_trait, &[])

tests/ui/manual_slice_fill.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,11 @@ fn issue_14192() {
115115
tmp[0] = i;
116116
}
117117
}
118+
119+
fn issue14189() {
120+
// Should not lint because `!*b` is not constant
121+
let mut tmp = vec![1, 2, 3];
122+
for b in &mut tmp {
123+
*b = !*b;
124+
}
125+
}

tests/ui/manual_slice_fill.rs

+8
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,11 @@ fn issue_14192() {
128128
tmp[0] = i;
129129
}
130130
}
131+
132+
fn issue14189() {
133+
// Should not lint because `!*b` is not constant
134+
let mut tmp = vec![1, 2, 3];
135+
for b in &mut tmp {
136+
*b = !*b;
137+
}
138+
}

0 commit comments

Comments
 (0)