From 97912966340f5e1b04dcde6da92646fe14d3c790 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 2 Jun 2025 05:30:26 +0300 Subject: [PATCH] diagnostics: reduce complexity of redundant rule checks --- src/diagnostic.rs | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/diagnostic.rs b/src/diagnostic.rs index dec4a83..abcc06b 100644 --- a/src/diagnostic.rs +++ b/src/diagnostic.rs @@ -1000,7 +1000,7 @@ impl SemanticAnalyzer { fn check_for_redundant_rules(&self, source: &str) -> Vec { let mut diagnostics = Vec::new(); - let mut rules = Vec::new(); + let mut seen_rules = HashSet::new(); for (line_idx, line) in source.lines().enumerate() { let line_num = line_idx as u32; @@ -1011,26 +1011,23 @@ impl SemanticAnalyzer { || trimmed.contains(" drop") || trimmed.contains(" reject") { - for (existing_idx, existing_rule) in rules.iter().enumerate() { - if existing_rule == &trimmed { - let range = Range::new( - Position::new(line_num, 0), - Position::new(line_num, line.len() as u32), - ); - let diagnostic = Diagnostic::new( - range, - DiagnosticSeverity::Warning, - DiagnosticCode::RedundantRule, - format!( - "Duplicate rule found (first occurrence at line {})", - existing_idx + 1 - ), - ); - diagnostics.push(diagnostic); - break; - } + // Check if rule already exists in the HashSet + if seen_rules.contains(trimmed) { + let range = Range::new( + Position::new(line_num, 0), + Position::new(line_num, line.len() as u32), + ); + let diagnostic = Diagnostic::new( + range, + DiagnosticSeverity::Warning, + DiagnosticCode::RedundantRule, + "Duplicate rule found".to_string(), + ); + diagnostics.push(diagnostic); + } else { + // Add the rule to the HashSet if it's not a duplicate + seen_rules.insert(trimmed.to_string()); } - rules.push(trimmed.to_string()); } }