Skip to content

Commit

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

baz = () + zoob

# OK (false negative - need to preserve comments)
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 @@ -61,6 +61,11 @@ pub fn unpack_instead_of_concatenating_to_collection_literal(checker: &mut Check
return;
}

// We'll also avoid removing any comments.
if has_comments_in(Range::from_located(expr), checker.locator) {
return;
}

let new_expr = match kind {
Kind::List => create_expr(ExprKind::List {
elts: make_splat_elts(splat_element, other_elements, splat_at_left),
Expand Down

0 comments on commit 0dd04a3

Please sign in to comment.