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

Consider same-site fixes to be overlapping #3638

Merged
merged 1 commit into from
Mar 21, 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
20 changes: 10 additions & 10 deletions crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn apply_fixes<'a>(
locator: &'a Locator<'a>,
) -> (String, FixTable) {
let mut output = String::with_capacity(locator.len());
let mut last_pos: Location = Location::new(1, 0);
let mut last_pos: Option<Location> = None;
let mut applied: BTreeSet<&Fix> = BTreeSet::default();
let mut fixed = FxHashMap::default();

Expand All @@ -50,25 +50,25 @@ fn apply_fixes<'a>(

// Best-effort approach: if this fix overlaps with a fix we've already applied,
// skip it.
if last_pos > fix.location {
if last_pos.map_or(false, |last_pos| last_pos >= fix.location) {
continue;
}

// Add all contents from `last_pos` to `fix.location`.
let slice = locator.slice(Range::new(last_pos, fix.location));
let slice = locator.slice(Range::new(last_pos.unwrap_or_default(), fix.location));
output.push_str(slice);

// Add the patch itself.
output.push_str(&fix.content);

// Track that the fix was applied.
last_pos = fix.end_location;
last_pos = Some(fix.end_location);
applied.insert(fix);
*fixed.entry(rule).or_default() += 1;
}

// Add the remaining content.
let slice = locator.skip(last_pos);
let slice = locator.skip(last_pos.unwrap_or_default());
output.push_str(slice);

(output, fixed)
Expand Down Expand Up @@ -194,29 +194,29 @@ class A:
fn apply_two_removals() {
let locator = Locator::new(
r#"
class A(object, object):
class A(object, object, object):
...
"#
.trim(),
);
let diagnostics = create_diagnostics([
Fix {
content: String::new(),
location: Location::new(1, 7),
location: Location::new(1, 8),
end_location: Location::new(1, 16),
},
Fix {
content: String::new(),
location: Location::new(1, 16),
end_location: Location::new(1, 23),
location: Location::new(1, 22),
end_location: Location::new(1, 30),
},
]);
let (contents, fixed) = apply_fixes(diagnostics.iter(), &locator);

assert_eq!(
contents,
r#"
class A:
class A(object):
...
"#
.trim()
Expand Down
2 changes: 0 additions & 2 deletions crates/ruff/src/rules/flake8_return/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,6 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) {
content.push_str(checker.stylist.line_ending().as_str());
content.push_str(indent);
content.push_str("return None");
// This is the last statement in the function. So it has to be followed by
// a newline, or comments, or nothing.
diagnostic.amend(Fix::insertion(
content,
end_of_statement(stmt, checker.locator),
Expand Down