diff --git a/crates/ruff_linter/resources/test/fixtures/refurb/FURB116.py b/crates/ruff_linter/resources/test/fixtures/refurb/FURB116.py index 6ec754dadc..22248f449d 100644 --- a/crates/ruff_linter/resources/test/fixtures/refurb/FURB116.py +++ b/crates/ruff_linter/resources/test/fixtures/refurb/FURB116.py @@ -42,3 +42,11 @@ print(hex(sys # for negatives numbers autofix is display-only print(bin(-1)[2:]) + + +print( + bin( + 1337 + # text + )[2:] +) diff --git a/crates/ruff_linter/src/rules/refurb/rules/fstring_number_format.rs b/crates/ruff_linter/src/rules/refurb/rules/fstring_number_format.rs index 68c95def5d..0c834fe237 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/fstring_number_format.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/fstring_number_format.rs @@ -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 }; diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB116_FURB116.py.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB116_FURB116.py.snap index 6b3566c216..1964a91421 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB116_FURB116.py.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB116_FURB116.py.snap @@ -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 diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__fstring_number_format_python_311.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__fstring_number_format_python_311.snap index 5a2810bb6e..00ecebd1ae 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__fstring_number_format_python_311.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__fstring_number_format_python_311.snap @@ -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