Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strict sorted and union for NoQA mapping insertion #7531

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/ruff_linter/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_linter/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