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 bbbb0a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
9 changes: 6 additions & 3 deletions lints/duplicate-mutable-accounts/src/alternate_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ impl<'cx, 'tcx> Values<'cx, 'tcx> {
if_chain! {
if let ExprKind::MethodCall(path_seg_left, exprs_left, _span) = left.kind;
if let ExprKind::MethodCall(path_seg_right, exprs_right, _span) = right.kind;
if path_seg_left.ident.name.as_str() == "key" && path_seg_right.ident.name.as_str() == "key";
if path_seg_left.ident.name.as_str() == "key"
&& path_seg_right.ident.name.as_str() == "key";
if !exprs_left.is_empty() && !exprs_right.is_empty();
let mut spanless_eq = SpanlessEq::new(self.cx);
if (spanless_eq.eq_expr(&exprs_left[0], first_account) && spanless_eq.eq_expr(&exprs_right[0], second_account))
|| (spanless_eq.eq_expr(&exprs_left[0], second_account) && spanless_eq.eq_expr(&exprs_right[0], first_account));
if (spanless_eq.eq_expr(&exprs_left[0], first_account)
&& spanless_eq.eq_expr(&exprs_right[0], second_account))
|| (spanless_eq.eq_expr(&exprs_left[0], second_account)
&& spanless_eq.eq_expr(&exprs_right[0], first_account));
then {
return true;
}
Expand Down
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
9 changes: 5 additions & 4 deletions lints/duplicate-mutable-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMutableAccounts {
if let Some(v) = self.anchor_accounts.get_mut(&account_id) {
v.push((field.ident.name, field.span));
} else {
self.anchor_accounts.insert(account_id, vec![(field.ident.name, field.span)]);
self.anchor_accounts
.insert(account_id, vec![(field.ident.name, field.span)]);
}
}
}
Expand Down Expand Up @@ -140,11 +141,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 bbbb0a9

Please sign in to comment.