Skip to content

Commit 62e3e4a

Browse files
author
Dan Bondarenko
committed
Fix violations against new lint const_size_windows
I replaced const-size usages of `slice::windows` with `slice::array_windows` in the codebase. However, I refrained from replacing usages in the input files for UI tests, instead allowing violations via #![allow(clippy::const_size_windows)].
1 parent bed2582 commit 62e3e4a

13 files changed

Lines changed: 69 additions & 64 deletions

clippy_lints/src/comparison_chain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
8080
return;
8181
}
8282

83-
for cond in conds.windows(2) {
83+
for [left, right] in conds.array_windows() {
8484
if let (&ExprKind::Binary(ref kind1, lhs1, rhs1), &ExprKind::Binary(ref kind2, lhs2, rhs2)) =
85-
(&cond[0].kind, &cond[1].kind)
85+
(&left.kind, &right.kind)
8686
{
8787
if !kind_is_cmp(kind1.node) || !kind_is_cmp(kind2.node) {
8888
return;

clippy_lints/src/formatting.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ declare_lint_pass!(Formatting => [
147147

148148
impl EarlyLintPass for Formatting {
149149
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
150-
for w in block.stmts.windows(2) {
151-
if let (StmtKind::Expr(first), StmtKind::Expr(second) | StmtKind::Semi(second)) = (&w[0].kind, &w[1].kind) {
150+
for [left, right] in block.stmts.array_windows() {
151+
if let (StmtKind::Expr(first), StmtKind::Expr(second) | StmtKind::Semi(second)) = (&left.kind, &right.kind)
152+
{
152153
check_missing_else(cx, first, second);
153154
}
154155
}

clippy_lints/src/inconsistent_struct_constructor.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ fn suggestion<'tcx>(
155155
def_order_map: &FxHashMap<Symbol, usize>,
156156
) -> String {
157157
let ws = fields
158-
.windows(2)
159-
.map(|w| {
160-
let w0_span = field_with_attrs_span(cx.tcx, &w[0]);
161-
let w1_span = field_with_attrs_span(cx.tcx, &w[1]);
162-
let span = w0_span.between(w1_span);
158+
.array_windows()
159+
.map(|[left, right]| {
160+
let left_span = field_with_attrs_span(cx.tcx, left);
161+
let right_span = field_with_attrs_span(cx.tcx, right);
162+
let span = left_span.between(right_span);
163163
snippet(cx, span, " ")
164164
})
165165
.collect::<Vec<_>>();

clippy_lints/src/matches/single_match.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE};
2222
/// span, e.g. a string literal `"//"`, but we know that this isn't the case for empty
2323
/// match arms.
2424
fn empty_arm_has_comment(cx: &LateContext<'_>, span: Span) -> bool {
25-
span.check_text(cx, |text| text.as_bytes().windows(2).any(|w| w == b"//" || w == b"/*"))
25+
span.check_text(cx, |text| {
26+
text.as_bytes().array_windows::<2>().any(|w| w == b"//" || w == b"/*")
27+
})
2628
}
2729

2830
pub(crate) fn check<'tcx>(

clippy_lints/src/redundant_closure_call.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
270270
closure_usage_count.count
271271
}
272272

273-
for w in block.stmts.windows(2) {
274-
if let hir::StmtKind::Let(local) = w[0].kind
273+
for [left, right] in block.stmts.array_windows() {
274+
if let hir::StmtKind::Let(local) = left.kind
275275
&& let Some(t) = local.init
276276
&& let ExprKind::Closure { .. } = t.kind
277277
&& let hir::PatKind::Binding(_, _, ident, _) = local.pat.kind
278-
&& let hir::StmtKind::Semi(second) = w[1].kind
278+
&& let hir::StmtKind::Semi(second) = right.kind
279279
&& let ExprKind::Assign(_, call, _) = second.kind
280280
&& let ExprKind::Call(closure, _) = call.kind
281281
&& let ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind

clippy_lints/src/tabs_in_doc_comments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
114114
return vec![(0, 1)];
115115
}
116116

117-
for entry in char_indices.windows(2) {
117+
for entry in char_indices.array_windows::<2>() {
118118
match entry {
119119
[(_, '\t'), (_, '\t')] => {
120120
// either string starts with double tab, then we have to set it active,

clippy_lints_internal/src/repeated_is_diagnostic_item.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,9 @@ impl<'tcx> LateLintPass<'tcx> for RepeatedIsDiagnosticItem {
125125
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
126126
for [(cond1, stmt1_span), (cond2, stmt2_span)] in block
127127
.stmts
128-
.windows(2)
129-
.filter_map(|pair| {
130-
if let [if1, if2] = pair
131-
&& let StmtKind::Expr(e1) | StmtKind::Semi(e1) = if1.kind
128+
.array_windows()
129+
.filter_map(|[if1, if2]| {
130+
if let StmtKind::Expr(e1) | StmtKind::Semi(e1) = if1.kind
132131
&& let ExprKind::If(cond1, ..) = e1.kind
133132
&& let StmtKind::Expr(e2) | StmtKind::Semi(e2) = if2.kind
134133
&& let ExprKind::If(cond2, ..) = e2.kind

clippy_utils/src/str_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn camel_case_split(s: &str) -> Vec<&str> {
162162
offsets.insert(0, 0);
163163
}
164164

165-
offsets.windows(2).map(|w| &s[w[0]..w[1]]).collect()
165+
offsets.array_windows().map(|[left, right]| &s[*left..*right]).collect()
166166
}
167167

168168
/// Dealing with string comparison can be complicated, this struct ensures that both the

tests/ui/missing_asserts_for_indexing_unfixable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::missing_asserts_for_indexing)]
2+
#![allow(clippy::const_size_windows)]
23

34
fn sum(v: &[u8]) -> u8 {
45
v[0] + v[1] + v[2] + v[3] + v[4]

tests/ui/missing_asserts_for_indexing_unfixable.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: indexing into a slice multiple times without an `assert`
2-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:4:5
2+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:5:5
33
|
44
LL | v[0] + v[1] + v[2] + v[3] + v[4]
55
| ^^^^ ^^^^ ^^^^ ^^^^ ^^^^
@@ -10,7 +10,7 @@ LL | v[0] + v[1] + v[2] + v[3] + v[4]
1010
= help: to override `-D warnings` add `#[allow(clippy::missing_asserts_for_indexing)]`
1111

1212
error: indexing into a slice multiple times without an `assert`
13-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:9:13
13+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:10:13
1414
|
1515
LL | let _ = v[0];
1616
| ^^^^
@@ -21,7 +21,7 @@ LL | let _ = v[1..4];
2121
= help: consider asserting the length before indexing: `assert!(v.len() > 3);`
2222

2323
error: indexing into a slice multiple times without an `assert`
24-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:16:13
24+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:17:13
2525
|
2626
LL | let a = v[0];
2727
| ^^^^
@@ -34,55 +34,55 @@ LL | let c = v[2];
3434
= help: consider asserting the length before indexing: `assert!(v.len() > 2);`
3535

3636
error: indexing into a slice multiple times without an `assert`
37-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:25:13
37+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:26:13
3838
|
3939
LL | let _ = v1[0] + v1[12];
4040
| ^^^^^ ^^^^^^
4141
|
4242
= help: consider asserting the length before indexing: `assert!(v1.len() > 12);`
4343

4444
error: indexing into a slice multiple times without an `assert`
45-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:27:13
45+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:28:13
4646
|
4747
LL | let _ = v2[5] + v2[15];
4848
| ^^^^^ ^^^^^^
4949
|
5050
= help: consider asserting the length before indexing: `assert!(v2.len() > 15);`
5151

5252
error: indexing into a slice multiple times without an `assert`
53-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:34:13
53+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:35:13
5454
|
5555
LL | let _ = v2[5] + v2[15];
5656
| ^^^^^ ^^^^^^
5757
|
5858
= help: consider asserting the length before indexing: `assert!(v2.len() > 15);`
5959

6060
error: indexing into a slice multiple times without an `assert`
61-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:44:13
61+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:45:13
6262
|
6363
LL | let _ = f.v[0] + f.v[1];
6464
| ^^^^^^ ^^^^^^
6565
|
6666
= help: consider asserting the length before indexing: `assert!(f.v.len() > 1);`
6767

6868
error: indexing into a slice multiple times without an `assert`
69-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:58:13
69+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:59:13
7070
|
7171
LL | let _ = x[0] + x[1];
7272
| ^^^^ ^^^^
7373
|
7474
= help: consider asserting the length before indexing: `assert!(x.len() > 1);`
7575

7676
error: indexing into a slice multiple times without an `assert`
77-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:76:13
77+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:77:13
7878
|
7979
LL | let _ = v1[1] + v1[2];
8080
| ^^^^^ ^^^^^
8181
|
8282
= help: consider asserting the length before indexing: `assert!(v1.len() > 2);`
8383

8484
error: indexing into a slice multiple times without an `assert`
85-
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:84:13
85+
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:85:13
8686
|
8787
LL | let _ = v1[0] + v1[1] + v1[2];
8888
| ^^^^^ ^^^^^ ^^^^^

0 commit comments

Comments
 (0)