Skip to content

Commit

Permalink
Use strict sorted and union for NoQA mapping insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Sep 20, 2023
1 parent 97510c8 commit d4bbe49
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
12 changes: 12 additions & 0 deletions crates/ruff/src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,18 @@ y = \
TextRange::new(TextSize::from(77), TextSize::from(83)),
])
);

// https://github.com/astral-sh/ruff/issues/7530
let contents = r"
assert foo, \
'''triple-quoted
string'''
"
.trim();
assert_eq!(
noqa_mappings(contents),
NoqaMapping::from_iter([TextRange::new(TextSize::from(0), TextSize::from(48))])
);
}

#[test]
Expand Down
16 changes: 9 additions & 7 deletions crates/ruff/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,15 +764,17 @@ impl NoqaMapping {
pub(crate) fn push_mapping(&mut self, range: TextRange) {
if let Some(last_range) = self.ranges.last_mut() {
// Strictly sorted insertion
if last_range.end() <= range.start() {
if last_range.end() < range.start() {
// OK
}
// Try merging with the last inserted range
else if let Some(intersected) = last_range.intersect(range) {
*last_range = intersected;
return;
} else {
} else if range.end() < last_range.start() {
// Incoming range is strictly before the last range which violates
// the function's contract.
panic!("Ranges must be inserted in sorted order")
} else {
// Here, it's guaranteed that `last_range` and `range` overlap
// in some way. We want to merge them into a single range.
*last_range = last_range.cover(range);
return;
}
}

Expand Down

0 comments on commit d4bbe49

Please sign in to comment.