Skip to content

Commit bbbb0a9

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

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

lints/duplicate-mutable-accounts/src/alternate_constraint.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ impl<'cx, 'tcx> Values<'cx, 'tcx> {
4747
if_chain! {
4848
if let ExprKind::MethodCall(path_seg_left, exprs_left, _span) = left.kind;
4949
if let ExprKind::MethodCall(path_seg_right, exprs_right, _span) = right.kind;
50-
if path_seg_left.ident.name.as_str() == "key" && path_seg_right.ident.name.as_str() == "key";
50+
if path_seg_left.ident.name.as_str() == "key"
51+
&& path_seg_right.ident.name.as_str() == "key";
5152
if !exprs_left.is_empty() && !exprs_right.is_empty();
5253
let mut spanless_eq = SpanlessEq::new(self.cx);
53-
if (spanless_eq.eq_expr(&exprs_left[0], first_account) && spanless_eq.eq_expr(&exprs_right[0], second_account))
54-
|| (spanless_eq.eq_expr(&exprs_left[0], second_account) && spanless_eq.eq_expr(&exprs_right[0], first_account));
54+
if (spanless_eq.eq_expr(&exprs_left[0], first_account)
55+
&& spanless_eq.eq_expr(&exprs_right[0], second_account))
56+
|| (spanless_eq.eq_expr(&exprs_left[0], second_account)
57+
&& spanless_eq.eq_expr(&exprs_right[0], first_account));
5558
then {
5659
return true;
5760
}

lints/duplicate-mutable-accounts/src/anchor_constraint.rs

Lines changed: 10 additions & 16 deletions
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

lints/duplicate-mutable-accounts/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMutableAccounts {
102102
if let Some(v) = self.anchor_accounts.get_mut(&account_id) {
103103
v.push((field.ident.name, field.span));
104104
} else {
105-
self.anchor_accounts.insert(account_id, vec![(field.ident.name, field.span)]);
105+
self.anchor_accounts
106+
.insert(account_id, vec![(field.ident.name, field.span)]);
106107
}
107108
}
108109
}
@@ -140,11 +141,11 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMutableAccounts {
140141
self.no_alternate_constraints = true; // assume no alternate constraints
141142
for current in 0..exprs.len() - 1 {
142143
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 {
144+
if values.check_key_constraint(exprs[current], exprs[next]) {
146145
// if there is at least one alt constraint, set flag to false
147146
self.no_alternate_constraints = false;
147+
} else {
148+
self.spans.push((exprs[current].span, exprs[next].span));
148149
}
149150
}
150151
}

0 commit comments

Comments
 (0)