Skip to content

Commit

Permalink
Fix regression introduced in 5be5a60
Browse files Browse the repository at this point in the history
Line matches should belong to a specific rule
  • Loading branch information
AMDmi3 committed Mar 9, 2024
1 parent e124f7b commit e32d1ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/applier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ fn apply_content_rules(
let reader = BufReader::new(file);

let mut local_condition_statuses: Vec<bool> = vec![false; global_condition_statuses.len()];
let mut matched_lines: Vec<usize> = vec![];
let mut matched_lines: Vec<Vec<usize>> = vec![Default::default(); global_rule_statuses.len()];

for (line_number, line) in reader.lines().enumerate() {
let line = line?;

let mut matching_cache = RegexMatchingCache::new(&line, ruleset.regexes_count);

rules_with_conditions.retain(|(_, path_condition)| {
rules_with_conditions.retain(|(rule, path_condition)| {
let mut num_satisfied_content_conditions = 0;
for content_condition in &path_condition.content_conditions {
match content_condition.logic {
Expand All @@ -47,7 +47,7 @@ fn apply_content_rules(
if content_condition.is_reporting_target {
if matching_cache.check_condition_match(content_condition) {
*is_matched = true;
matched_lines.push(line_number);
matched_lines[rule.number].push(line_number);
}
} else if !*is_matched {
*is_matched = matching_cache.check_condition_match(content_condition);
Expand Down Expand Up @@ -75,7 +75,7 @@ fn apply_content_rules(

global_condition_statuses[condition.number] = true;
global_rule_statuses[rule.number].matched_lines.extend(
matched_lines
matched_lines[rule.number]
.iter()
.map(|line_number| (path.clone(), *line_number)),
);
Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod error_exitcode;
mod glob_patterns;
mod glob_scope;
mod ignore_marker;
mod multiple_rules;
mod path_condition_combinations;
mod path_conditions;
mod premature;
Expand Down
17 changes: 17 additions & 0 deletions tests/integration_tests/multiple_rules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: Copyright 2024 Dmitry Marakasov <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::lines;
use crate::utils::TestCase;

#[test]
fn mixing_up_matches_bug() {
// test for actual bug where rules produced false matches from another rules
// which produced vec!["a:1", "a:1", "a:2", "a:2"] matches
TestCase::new_for_json_tests()
.add_file("a", lines!["a", "b"])
.add_rule(lines!["files *", "match /a/"])
.add_rule(lines!["files *", "match /b/"])
.run()
.assert_matches(vec!["a:1", "a:2"]);
}

0 comments on commit e32d1ba

Please sign in to comment.