[refurb] Make fix unsafe if it deletes comments (FURB140) (#22679)

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan

<!-- How was it tested? -->
This commit is contained in:
chiri
2026-01-19 18:33:18 +03:00
committed by GitHub
parent bffc95873d
commit c5902a3cdb
3 changed files with 50 additions and 2 deletions

View File

@@ -57,3 +57,11 @@ from itertools import starmap as sm
[" ".join(x)(x, y) for x, y in zipped()]
[" ".join(x)(*x) for x in zipped()]
all(
predicate(a, b)
# text
for a, b
# text
in some_iterable
)

View File

@@ -1,4 +1,5 @@
use anyhow::{Result, bail};
use ruff_diagnostics::Applicability;
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::helpers::any_over_expr;
@@ -40,6 +41,9 @@ use crate::{Edit, Fix, FixAvailability, Violation};
/// all(starmap(predicate, some_iterable))
/// ```
///
/// ## Fix safety
/// This rule's fix is marked as safe, unless the expression contains comments.
///
/// ## References
/// - [Python documentation: `itertools.starmap`](https://docs.python.org/3/library/itertools.html#itertools.starmap)
///
@@ -155,7 +159,18 @@ pub(crate) fn reimplemented_starmap(checker: &Checker, target: &StarmapCandidate
)?,
target.range(),
);
Ok(Fix::safe_edits(import_edit, [main_edit]))
let applicability = if checker.comment_ranges().intersects(target.range()) {
Applicability::Unsafe
} else {
Applicability::Safe
};
Ok(Fix::applicable_edits(
import_edit,
[main_edit],
applicability,
))
});
}

View File

@@ -184,4 +184,29 @@ help: Replace with `itertools.starmap`
31 + set(sm(foo, [(85, 60), (100, 80)]))
32 |
33 | # Non-errors.
34 |
34 |
FURB140 [*] Use `itertools.starmap` instead of the generator
--> FURB140.py:62:5
|
61 | all(
62 | / predicate(a, b)
63 | | # text
64 | | for a, b
65 | | # text
66 | | in some_iterable
| |____________________^
67 | )
|
help: Replace with `itertools.starmap`
59 | [" ".join(x)(*x) for x in zipped()]
60 |
61 | all(
- predicate(a, b)
- # text
- for a, b
- # text
- in some_iterable
62 + sm(predicate, some_iterable)
63 | )
note: This is an unsafe fix and may change runtime behavior