From af26fb28660c1c2535698a8d5bfee94e81393bf2 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 21 Jan 2023 07:29:50 -0500 Subject: [PATCH] Avoid removing comments in RUF005 --- resources/test/fixtures/ruff/RUF005.py | 17 +++++++++++++++++ ...ad_of_concatenating_to_collection_literal.rs | 14 ++++++++------ ...f__rules__ruff__tests__RUF005_RUF005.py.snap | 10 ++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/resources/test/fixtures/ruff/RUF005.py b/resources/test/fixtures/ruff/RUF005.py index 3df2f4b217ace..b8a4311caee3e 100644 --- a/resources/test/fixtures/ruff/RUF005.py +++ b/resources/test/fixtures/ruff/RUF005.py @@ -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, +] diff --git a/src/rules/ruff/rules/unpack_instead_of_concatenating_to_collection_literal.rs b/src/rules/ruff/rules/unpack_instead_of_concatenating_to_collection_literal.rs index b5cec73bfe327..522bdad6fce63 100644 --- a/src/rules/ruff/rules/unpack_instead_of_concatenating_to_collection_literal.rs +++ b/src/rules/ruff/rules/unpack_instead_of_concatenating_to_collection_literal.rs @@ -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; @@ -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); } diff --git a/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF005_RUF005.py.snap b/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF005_RUF005.py.snap index b2ef003638d41..610ac357e8b76 100644 --- a/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF005_RUF005.py.snap +++ b/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF005_RUF005.py.snap @@ -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: ~