Skip to content

Commit e8b0ab4

Browse files
committed
clean up is_substream logic
1 parent f98808d commit e8b0ab4

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

Diff for: lints/duplicate-mutable-accounts/src/anchor_constraint.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,21 @@ impl Streams {
9090
.any(|token_stream| Self::is_substream(token_stream, other))
9191
}
9292

93-
/// Returns true if `other` is a substream of `stream`. By substream we mean in the
94-
/// sense of a substring.
93+
/// Returns true if `other` is a substream of `stream`. By substream we mean in the sense of a substring.
9594
// NOTE: a possible optimization is when a match is found, to remove the matched
9695
// TokenTrees from the TokenStream, since the constraint has been "checked" so it never
9796
// needs to be validated again. This cuts down the number of comparisons.
9897
fn is_substream(stream: &TokenStream, other: &TokenStream) -> bool {
99-
let other_len = other.len();
10098
for i in 0..stream.len() {
101-
for (j, other_token) in other.trees().enumerate() {
102-
match stream.trees().nth(i + j) {
103-
Some(token_tree) => {
104-
if !token_tree.eq_unspanned(other_token) {
105-
break;
106-
}
107-
// reached last index, so we have a match
108-
if j == other_len - 1 {
109-
return true;
110-
}
111-
}
112-
None => return false, // reached end of stream
113-
}
99+
if other
100+
.trees()
101+
.enumerate()
102+
.all(|(j, other_token)| match stream.trees().nth(i + j) {
103+
Some(token_tree) => token_tree.eq_unspanned(other_token),
104+
None => false,
105+
})
106+
{
107+
return true;
114108
}
115109
}
116110
false

Diff for: lints/duplicate-mutable-accounts/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMutableAccounts {
140140
self.no_alternate_constraints = true; // assume no alternate constraints
141141
for current in 0..exprs.len() - 1 {
142142
for next in current + 1..exprs.len() {
143-
if !values.check_key_constraint(exprs[current], exprs[next]) {
144-
self.spans.push((exprs[current].span, exprs[next].span));
145-
} else {
143+
if values.check_key_constraint(exprs[current], exprs[next]) {
146144
// if there is at least one alt constraint, set flag to false
147145
self.no_alternate_constraints = false;
146+
} else {
147+
self.spans.push((exprs[current].span, exprs[next].span));
148148
}
149149
}
150150
}

0 commit comments

Comments
 (0)