Skip to content

Commit

Permalink
perf: RuleTable::any_enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Apr 16, 2024
1 parent effd518 commit bac6d21
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/registry/rule_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl RuleSet {
/// assert!(set.contains(Rule::AmbiguousFunctionName));
/// assert!(!set.contains(Rule::BreakOutsideLoop));
/// ```
#[inline]
pub const fn contains(&self, rule: Rule) -> bool {
let rule = rule as u16;
let index = rule as usize / Self::SLICE_BITS as usize;
Expand All @@ -243,6 +244,20 @@ impl RuleSet {
self.0[index] & mask != 0
}

/// Returns `true` if any of the rules in `rules` are in this set.
#[inline]
pub const fn any(&self, rules: &[Rule]) -> bool {
let mut any = false;
let mut i = 0;

while i < rules.len() {
any |= self.contains(rules[i]);
i += 1;
}

any
}

/// Returns an iterator over the rules in this set.
///
/// ## Examples
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/settings/rule_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl RuleTable {
/// Returns whether any of the given rules should be checked.
#[inline]
pub const fn any_enabled(&self, rules: &[Rule]) -> bool {
self.enabled.intersects(&RuleSet::from_rules(rules))
self.enabled.any(rules)
}

/// Returns whether violations of the given rule should be fixed.
Expand Down

0 comments on commit bac6d21

Please sign in to comment.