[refurb] Make fix unsafe if it deletes comments (FURB164) (#22667)

<!--
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-20 19:47:00 +03:00
committed by GitHub
parent 1813a7032c
commit f0534e9cfd
3 changed files with 37 additions and 2 deletions

View File

@@ -68,4 +68,10 @@ _ = Decimal.from_float(float("\N{HYPHEN-MINUS}nan"))
# See: https://github.com/astral-sh/ruff/issues/21257
# fixes must be safe
_ = Fraction.from_float(f=4.2)
_ = Fraction.from_decimal(dec=4)
_ = Fraction.from_decimal(dec=4)
_ = (
Fraction
# text
.from_float(4.2)
)

View File

@@ -51,6 +51,7 @@ use crate::{Applicability, Edit, Fix, FixAvailability, Violation};
/// - The `from_*` methods provide type validation that the constructors don't
/// - Removing type validation can change program behavior
/// - The parameter names are different between methods and constructors
/// - The fix may remove comments attached to the original expression
///
/// The fix is marked as safe only when:
/// - The argument type is known to be valid for the target constructor
@@ -151,7 +152,7 @@ pub(crate) fn unnecessary_from_float(checker: &Checker, call: &ExprCall) {
let is_type_safe = is_valid_argument_type(arg_value, method_name, constructor, checker);
// Determine fix safety
let applicability = if is_type_safe {
let applicability = if is_type_safe && !checker.comment_ranges().intersects(call.range()) {
Applicability::Safe
} else {
Applicability::Unsafe

View File

@@ -712,6 +712,8 @@ help: Replace with `Fraction` constructor
- _ = Fraction.from_float(f=4.2)
70 + _ = Fraction(4.2)
71 | _ = Fraction.from_decimal(dec=4)
72 |
73 | _ = (
FURB164 [*] Verbose method `from_decimal` in `Fraction` construction
--> FURB164.py:71:5
@@ -720,6 +722,8 @@ FURB164 [*] Verbose method `from_decimal` in `Fraction` construction
70 | _ = Fraction.from_float(f=4.2)
71 | _ = Fraction.from_decimal(dec=4)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72 |
73 | _ = (
|
help: Replace with `Fraction` constructor
68 | # See: https://github.com/astral-sh/ruff/issues/21257
@@ -727,3 +731,27 @@ help: Replace with `Fraction` constructor
70 | _ = Fraction.from_float(f=4.2)
- _ = Fraction.from_decimal(dec=4)
71 + _ = Fraction(4)
72 |
73 | _ = (
74 | Fraction
FURB164 [*] Verbose method `from_float` in `Fraction` construction
--> FURB164.py:74:5
|
73 | _ = (
74 | / Fraction
75 | | # text
76 | | .from_float(4.2)
| |____________________^
77 | )
|
help: Replace with `Fraction` constructor
71 | _ = Fraction.from_decimal(dec=4)
72 |
73 | _ = (
- Fraction
- # text
- .from_float(4.2)
74 + Fraction(4.2)
75 | )
note: This is an unsafe fix and may change runtime behavior