Skip to content

Commit

Permalink
Avoid FURB113 autofix if comments are present (#8494)
Browse files Browse the repository at this point in the history
This PR avoids creating the fix for `FURB113` if there are comments in
between the `append` calls.

fixes: #8105
  • Loading branch information
dhruvmanila authored Nov 9, 2023
1 parent 4fdf97a commit 4760af3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
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))`


0 comments on commit 4760af3

Please sign in to comment.