Skip to content

Commit

Permalink
Avoid removing comments in RUF005 (#2057)
Browse files Browse the repository at this point in the history
Closes #2054.
  • Loading branch information
charliermarsh committed Jan 21, 2023
1 parent 883e650 commit 38eed29
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
17 changes: 17 additions & 0 deletions resources/test/fixtures/ruff/RUF005.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ def yay(self):
chain = ['a', 'b', 'c'] + eggs + list(('yes', 'no', 'pants') + zoob)

baz = () + zoob

first = [
# The order
1, # here
2, # is
# extremely
3, # critical
# to preserve
]
second = first + [
# please
4,
# don't
5,
# touch
6,
]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustpython_ast::{Expr, ExprContext, ExprKind, Operator};

use crate::ast::helpers::{create_expr, unparse_expr};
use crate::ast::helpers::{create_expr, has_comments_in, unparse_expr};
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::fix::Fix;
Expand Down Expand Up @@ -85,11 +85,13 @@ pub fn unpack_instead_of_concatenating_to_collection_literal(checker: &mut Check
Range::from_located(expr),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
new_expr_string,
expr.location,
expr.end_location.unwrap(),
));
if !has_comments_in(Range::from_located(expr), checker.locator) {
diagnostic.amend(Fix::replacement(
new_expr_string,
expr.location,
expr.end_location.unwrap(),
));
}
}
checker.diagnostics.push(diagnostic);
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,14 @@ expression: diagnostics
row: 22
column: 15
parent: ~
- kind:
UnpackInsteadOfConcatenatingToCollectionLiteral: "[*first, 4, 5, 6]"
location:
row: 32
column: 9
end_location:
row: 39
column: 1
fix: ~
parent: ~

0 comments on commit 38eed29

Please sign in to comment.