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 FURB113 autofix if comments are present #8494

Merged
merged 1 commit into from
Nov 9, 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
9 changes: 9 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB113.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ def yes_six(x: list):
x.append(2)


if True:
# FURB113
nums.append(1)
# comment
nums.append(2)
# comment
nums.append(3)


# Non-errors.

nums.append(1)
Expand Down
11 changes: 10 additions & 1 deletion crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,16 @@ pub(crate) fn repeated_append(checker: &mut Checker, stmt: &Stmt) {

// We only suggest a fix when all appends in a group are clumped together. If they're
// non-consecutive, fixing them is much more difficult.
if group.is_consecutive {
//
// Avoid fixing if there are comments in between the appends:
//
// ```python
// a.append(1)
// # comment
// a.append(2)
// ```
if group.is_consecutive && !checker.indexer().comment_ranges().intersects(group.range())
{
diagnostic.set_fix(Fix::unsafe_edit(Edit::replacement(
replacement,
group.start(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,20 @@ FURB113.py:122:5: FURB113 [*] Use `x.extend((1, 2))` instead of repeatedly calli
122 |+ x.extend((1, 2))
124 123 |
125 124 |
126 125 | # Non-errors.
126 125 | if True:

FURB113.py:128:5: FURB113 Use `nums.extend((1, 2, 3))` instead of repeatedly calling `nums.append()`
|
126 | if True:
127 | # FURB113
128 | nums.append(1)
| _____^
129 | | # comment
130 | | nums.append(2)
131 | | # comment
132 | | nums.append(3)
| |__________________^ FURB113
|
= help: Replace with `nums.extend((1, 2, 3))`


Loading