Skip to content

Commit

Permalink
clean up is_substream logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oslfmt committed Jul 22, 2022
1 parent f98808d commit e8b0ab4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
26 changes: 10 additions & 16 deletions lints/duplicate-mutable-accounts/src/anchor_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,21 @@ impl Streams {
.any(|token_stream| Self::is_substream(token_stream, other))
}

/// Returns true if `other` is a substream of `stream`. By substream we mean in the
/// sense of a substring.
/// Returns true if `other` is a substream of `stream`. By substream we mean in the sense of a substring.
// NOTE: a possible optimization is when a match is found, to remove the matched
// TokenTrees from the TokenStream, since the constraint has been "checked" so it never
// needs to be validated again. This cuts down the number of comparisons.
fn is_substream(stream: &TokenStream, other: &TokenStream) -> bool {
let other_len = other.len();
for i in 0..stream.len() {
for (j, other_token) in other.trees().enumerate() {
match stream.trees().nth(i + j) {
Some(token_tree) => {
if !token_tree.eq_unspanned(other_token) {
break;
}
// reached last index, so we have a match
if j == other_len - 1 {
return true;
}
}
None => return false, // reached end of stream
}
if other
.trees()
.enumerate()
.all(|(j, other_token)| match stream.trees().nth(i + j) {
Some(token_tree) => token_tree.eq_unspanned(other_token),
None => false,
})
{
return true;
}
}
false
Expand Down
6 changes: 3 additions & 3 deletions lints/duplicate-mutable-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMutableAccounts {
self.no_alternate_constraints = true; // assume no alternate constraints
for current in 0..exprs.len() - 1 {
for next in current + 1..exprs.len() {
if !values.check_key_constraint(exprs[current], exprs[next]) {
self.spans.push((exprs[current].span, exprs[next].span));
} else {
if values.check_key_constraint(exprs[current], exprs[next]) {
// if there is at least one alt constraint, set flag to false
self.no_alternate_constraints = false;
} else {
self.spans.push((exprs[current].span, exprs[next].span));
}
}
}
Expand Down

0 comments on commit e8b0ab4

Please sign in to comment.