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

Avoid removing comments in RUF005 #2057

Merged
merged 1 commit into from
Jan 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ~