mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 13:30:49 -05:00
[refurb] Make fix unsafe if it deletes comments (FURB116) (#22681)
<!-- 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? --> --------- Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
This commit is contained in:
@@ -42,3 +42,11 @@ print(hex(sys
|
||||
|
||||
# for negatives numbers autofix is display-only
|
||||
print(bin(-1)[2:])
|
||||
|
||||
|
||||
print(
|
||||
bin(
|
||||
1337
|
||||
# text
|
||||
)[2:]
|
||||
)
|
||||
|
||||
@@ -28,8 +28,9 @@ use crate::{Applicability, Edit, Fix, FixAvailability, Violation};
|
||||
///
|
||||
/// ## Fix safety
|
||||
/// The fix is only marked as safe for integer literals, all other cases
|
||||
/// are display-only, as they may change the runtime behaviour of the program
|
||||
/// or introduce syntax errors.
|
||||
/// are display-only, as they may change the runtime behavior of the program
|
||||
/// or introduce syntax errors. The fix for integer literals is also marked as unsafe
|
||||
/// if the expression contains comments that would be removed by the fix.
|
||||
#[derive(ViolationMetadata)]
|
||||
#[violation_metadata(stable_since = "0.13.0")]
|
||||
pub(crate) struct FStringNumberFormat {
|
||||
@@ -82,7 +83,7 @@ pub(crate) fn fstring_number_format(checker: &Checker, subscript: &ast::ExprSubs
|
||||
};
|
||||
|
||||
let Expr::NumberLiteral(ast::ExprNumberLiteral {
|
||||
value: ast::Number::Int(int),
|
||||
value: Number::Int(int),
|
||||
..
|
||||
}) = lower.as_ref()
|
||||
else {
|
||||
@@ -139,7 +140,11 @@ pub(crate) fn fstring_number_format(checker: &Checker, subscript: &ast::ExprSubs
|
||||
};
|
||||
|
||||
let applicability = if matches!(maybe_number, Expr::NumberLiteral(_)) {
|
||||
Applicability::Safe
|
||||
if checker.comment_ranges().intersects(subscript.range()) {
|
||||
Applicability::Unsafe
|
||||
} else {
|
||||
Applicability::Safe
|
||||
}
|
||||
} else {
|
||||
Applicability::DisplayOnly
|
||||
};
|
||||
|
||||
@@ -283,4 +283,30 @@ help: Replace with `f"{-1:b}"`
|
||||
43 | # for negatives numbers autofix is display-only
|
||||
- print(bin(-1)[2:])
|
||||
44 + print(f"{-1:b}")
|
||||
45 |
|
||||
46 |
|
||||
47 | print(
|
||||
note: This is a display-only fix and is likely to be incorrect
|
||||
|
||||
FURB116 [*] Replace `bin` call with `f"{1337:b}"`
|
||||
--> FURB116.py:48:5
|
||||
|
|
||||
47 | print(
|
||||
48 | / bin(
|
||||
49 | | 1337
|
||||
50 | | # text
|
||||
51 | | )[2:]
|
||||
| |_________^
|
||||
52 | )
|
||||
|
|
||||
help: Replace with `f"{1337:b}"`
|
||||
45 |
|
||||
46 |
|
||||
47 | print(
|
||||
- bin(
|
||||
- 1337
|
||||
- # text
|
||||
- )[2:]
|
||||
48 + f"{1337:b}"
|
||||
49 | )
|
||||
note: This is an unsafe fix and may change runtime behavior
|
||||
|
||||
@@ -254,4 +254,30 @@ help: Replace with `f"{-1:b}"`
|
||||
43 | # for negatives numbers autofix is display-only
|
||||
- print(bin(-1)[2:])
|
||||
44 + print(f"{-1:b}")
|
||||
45 |
|
||||
46 |
|
||||
47 | print(
|
||||
note: This is a display-only fix and is likely to be incorrect
|
||||
|
||||
FURB116 [*] Replace `bin` call with `f"{1337:b}"`
|
||||
--> FURB116.py:48:5
|
||||
|
|
||||
47 | print(
|
||||
48 | / bin(
|
||||
49 | | 1337
|
||||
50 | | # text
|
||||
51 | | )[2:]
|
||||
| |_________^
|
||||
52 | )
|
||||
|
|
||||
help: Replace with `f"{1337:b}"`
|
||||
45 |
|
||||
46 |
|
||||
47 | print(
|
||||
- bin(
|
||||
- 1337
|
||||
- # text
|
||||
- )[2:]
|
||||
48 + f"{1337:b}"
|
||||
49 | )
|
||||
note: This is an unsafe fix and may change runtime behavior
|
||||
|
||||
Reference in New Issue
Block a user