From e3e98fc3a29bf3318f70840f0e3b91231b800756 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Mon, 25 Nov 2024 20:46:43 -0600 Subject: [PATCH 01/11] update fixture --- .../resources/test/fixtures/refurb/FURB157.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py b/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py index facb2a373568f..b8c0c296f1d49 100644 --- a/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py +++ b/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py @@ -42,3 +42,15 @@ Decimal("_") # Ok Decimal(" ") # Ok Decimal("10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") # Ok + +# Non-finite variants +# https://github.com/astral-sh/ruff/issues/14587 +Decimal(float(" nan ")) # Decimal(" nan ") +Decimal(float(" +nan ")) # Decimal(" +nan ") +Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +Decimal(float(" inf ")) # Decimal(" inf ") +Decimal(float(" +inf ")) # Decimal(" +inf ") +Decimal(float(" -inf ")) # Decimal(" -inf ") +Decimal(float(" infinity ")) # Decimal(" infinity ") +Decimal(float(" +infinity ")) # Decimal(" +infinity ") +Decimal(float(" -infinity ")) # Decimal(" -infinity ") From 30215005804f2744e1c692e784d962b9dc3f0c09 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Mon, 25 Nov 2024 20:47:19 -0600 Subject: [PATCH 02/11] handle non-finite decimals --- .../rules/verbose_decimal_constructor.rs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs index 559458cc57ef8..5b0f979adbc39 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs @@ -152,14 +152,30 @@ pub(crate) fn verbose_decimal_constructor(checker: &mut Checker, call: &ast::Exp let Some(float) = float.as_string_literal_expr() else { return; }; + + let normalized_float_string = float.value.to_str().trim().to_lowercase(); + if !matches!( - float.value.to_str().to_lowercase().as_str(), - "inf" | "-inf" | "infinity" | "-infinity" | "nan" + normalized_float_string.as_str(), + "inf" + | "+inf" + | "-inf" + | "infinity" + | "+infinity" + | "-infinity" + | "nan" + | "+nan" + | "-nan" ) { return; } - let replacement = checker.locator().slice(float).to_string(); + let mut replacement = checker.locator().slice(float).to_string(); + // `Decimal(float("-nan")) == Decimal("nan")` + if &normalized_float_string == "-nan" { + replacement.remove(replacement.find('-').unwrap()); + } + dbg!(&replacement); let mut diagnostic = Diagnostic::new( VerboseDecimalConstructor { replacement: replacement.clone(), From cea3f17611cf1fce0c9febf15b6df62c101950b1 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Mon, 25 Nov 2024 20:47:26 -0600 Subject: [PATCH 03/11] update snapshot --- ...es__refurb__tests__FURB157_FURB157.py.snap | 181 +++++++++++++++++- 1 file changed, 180 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap index cdf82865ff52c..aa2e88446d16d 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap @@ -1,6 +1,5 @@ --- source: crates/ruff_linter/src/rules/refurb/mod.rs -snapshot_kind: text --- FURB157.py:5:9: FURB157 [*] Verbose expression in `Decimal` constructor | @@ -207,3 +206,183 @@ FURB157.py:24:9: FURB157 [*] Verbose expression in `Decimal` constructor 25 25 | 26 26 | # Ok 27 27 | Decimal("2e-4") + +FURB157.py:48:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +46 | # Non-finite variants +47 | # https://github.com/astral-sh/ruff/issues/14587 +48 | Decimal(float(" nan ")) # Decimal(" nan ") + | ^^^^^^^^^^^^^^ FURB157 +49 | Decimal(float(" +nan ")) # Decimal(" +nan ") +50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! + | + = help: Replace with `" nan "` + +ℹ Safe fix +45 45 | +46 46 | # Non-finite variants +47 47 | # https://github.com/astral-sh/ruff/issues/14587 +48 |-Decimal(float(" nan ")) # Decimal(" nan ") + 48 |+Decimal(" nan ") # Decimal(" nan ") +49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") +50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +51 51 | Decimal(float(" inf ")) # Decimal(" inf ") + +FURB157.py:49:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +47 | # https://github.com/astral-sh/ruff/issues/14587 +48 | Decimal(float(" nan ")) # Decimal(" nan ") +49 | Decimal(float(" +nan ")) # Decimal(" +nan ") + | ^^^^^^^^^^^^^^^ FURB157 +50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +51 | Decimal(float(" inf ")) # Decimal(" inf ") + | + = help: Replace with `" +nan "` + +ℹ Safe fix +46 46 | # Non-finite variants +47 47 | # https://github.com/astral-sh/ruff/issues/14587 +48 48 | Decimal(float(" nan ")) # Decimal(" nan ") +49 |-Decimal(float(" +nan ")) # Decimal(" +nan ") + 49 |+Decimal(" +nan ") # Decimal(" +nan ") +50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +51 51 | Decimal(float(" inf ")) # Decimal(" inf ") +52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") + +FURB157.py:50:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +48 | Decimal(float(" nan ")) # Decimal(" nan ") +49 | Decimal(float(" +nan ")) # Decimal(" +nan ") +50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! + | ^^^^^^^^^^^^^^^ FURB157 +51 | Decimal(float(" inf ")) # Decimal(" inf ") +52 | Decimal(float(" +inf ")) # Decimal(" +inf ") + | + = help: Replace with `" nan "` + +ℹ Safe fix +47 47 | # https://github.com/astral-sh/ruff/issues/14587 +48 48 | Decimal(float(" nan ")) # Decimal(" nan ") +49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") +50 |-Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! + 50 |+Decimal(" nan ") # Decimal(" nan "), notice the difference! +51 51 | Decimal(float(" inf ")) # Decimal(" inf ") +52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") + +FURB157.py:51:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +49 | Decimal(float(" +nan ")) # Decimal(" +nan ") +50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +51 | Decimal(float(" inf ")) # Decimal(" inf ") + | ^^^^^^^^^^^^^^ FURB157 +52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 | Decimal(float(" -inf ")) # Decimal(" -inf ") + | + = help: Replace with `" inf "` + +ℹ Safe fix +48 48 | Decimal(float(" nan ")) # Decimal(" nan ") +49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") +50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +51 |-Decimal(float(" inf ")) # Decimal(" inf ") + 51 |+Decimal(" inf ") # Decimal(" inf ") +52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") + +FURB157.py:52:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +51 | Decimal(float(" inf ")) # Decimal(" inf ") +52 | Decimal(float(" +inf ")) # Decimal(" +inf ") + | ^^^^^^^^^^^^^^^ FURB157 +53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 | Decimal(float(" infinity ")) # Decimal(" infinity ") + | + = help: Replace with `" +inf "` + +ℹ Safe fix +49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") +50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +51 51 | Decimal(float(" inf ")) # Decimal(" inf ") +52 |-Decimal(float(" +inf ")) # Decimal(" +inf ") + 52 |+Decimal(" +inf ") # Decimal(" +inf ") +53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +55 55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") + +FURB157.py:53:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +51 | Decimal(float(" inf ")) # Decimal(" inf ") +52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 | Decimal(float(" -inf ")) # Decimal(" -inf ") + | ^^^^^^^^^^^^^^^ FURB157 +54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") + | + = help: Replace with `" -inf "` + +ℹ Safe fix +50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +51 51 | Decimal(float(" inf ")) # Decimal(" inf ") +52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 |-Decimal(float(" -inf ")) # Decimal(" -inf ") + 53 |+Decimal(" -inf ") # Decimal(" -inf ") +54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +55 55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +56 56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") + +FURB157.py:54:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 | Decimal(float(" infinity ")) # Decimal(" infinity ") + | ^^^^^^^^^^^^^^^^^^^ FURB157 +55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") + | + = help: Replace with `" infinity "` + +ℹ Safe fix +51 51 | Decimal(float(" inf ")) # Decimal(" inf ") +52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 |-Decimal(float(" infinity ")) # Decimal(" infinity ") + 54 |+Decimal(" infinity ") # Decimal(" infinity ") +55 55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +56 56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") + +FURB157.py:55:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") + | ^^^^^^^^^^^^^^^^^^^^ FURB157 +56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") + | + = help: Replace with `" +infinity "` + +ℹ Safe fix +52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +55 |-Decimal(float(" +infinity ")) # Decimal(" +infinity ") + 55 |+Decimal(" +infinity ") # Decimal(" +infinity ") +56 56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") + +FURB157.py:56:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") + | ^^^^^^^^^^^^^^^^^^^^ FURB157 + | + = help: Replace with `" -infinity "` + +ℹ Safe fix +53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +55 55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +56 |-Decimal(float(" -infinity ")) # Decimal(" -infinity ") + 56 |+Decimal(" -infinity ") # Decimal(" -infinity ") From 7ba2cb2b9bd4283c7c320680d11de021d614edd3 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Mon, 25 Nov 2024 20:50:15 -0600 Subject: [PATCH 04/11] remove dbg --- .../src/rules/refurb/rules/verbose_decimal_constructor.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs index 5b0f979adbc39..7ad4156836da1 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs @@ -175,7 +175,6 @@ pub(crate) fn verbose_decimal_constructor(checker: &mut Checker, call: &ast::Exp if &normalized_float_string == "-nan" { replacement.remove(replacement.find('-').unwrap()); } - dbg!(&replacement); let mut diagnostic = Diagnostic::new( VerboseDecimalConstructor { replacement: replacement.clone(), From f94538540c5d0b1f34373786e5b02d00618dec0c Mon Sep 17 00:00:00 2001 From: Dylan <53534755+dylwil3@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:35:11 -0600 Subject: [PATCH 05/11] as_str to borrow Co-authored-by: Micha Reiser --- .../src/rules/refurb/rules/verbose_decimal_constructor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs index 7ad4156836da1..17a13cea2001e 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs @@ -156,7 +156,7 @@ pub(crate) fn verbose_decimal_constructor(checker: &mut Checker, call: &ast::Exp let normalized_float_string = float.value.to_str().trim().to_lowercase(); if !matches!( - normalized_float_string.as_str(), + &normalized_float_string, "inf" | "+inf" | "-inf" From 25beaed9c31074396d4ed54f005ce561016f4f47 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Tue, 26 Nov 2024 10:39:54 -0600 Subject: [PATCH 06/11] remove riddle --- crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py b/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py index b8c0c296f1d49..2314bba4f7045 100644 --- a/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py +++ b/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py @@ -47,7 +47,9 @@ # https://github.com/astral-sh/ruff/issues/14587 Decimal(float(" nan ")) # Decimal(" nan ") Decimal(float(" +nan ")) # Decimal(" +nan ") -Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +# In this one case, " -nan ", the fix has to be +# `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +Decimal(float(" -nan ")) # Decimal(" nan ") Decimal(float(" inf ")) # Decimal(" inf ") Decimal(float(" +inf ")) # Decimal(" +inf ") Decimal(float(" -inf ")) # Decimal(" -inf ") From a2c82df5d1c9abbd26cb201845307a9037d015f7 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Tue, 26 Nov 2024 10:44:23 -0600 Subject: [PATCH 07/11] revert to as_str --- .../src/rules/refurb/rules/verbose_decimal_constructor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs index 17a13cea2001e..7ad4156836da1 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs @@ -156,7 +156,7 @@ pub(crate) fn verbose_decimal_constructor(checker: &mut Checker, call: &ast::Exp let normalized_float_string = float.value.to_str().trim().to_lowercase(); if !matches!( - &normalized_float_string, + normalized_float_string.as_str(), "inf" | "+inf" | "-inf" From f1b96adea44e334164634419ba3b38b370b680d7 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Tue, 26 Nov 2024 10:45:53 -0600 Subject: [PATCH 08/11] update snapshot --- ...es__refurb__tests__FURB157_FURB157.py.snap | 200 +++++++++--------- 1 file changed, 100 insertions(+), 100 deletions(-) diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap index aa2e88446d16d..5387b57883b48 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap @@ -214,7 +214,7 @@ FURB157.py:48:9: FURB157 [*] Verbose expression in `Decimal` constructor 48 | Decimal(float(" nan ")) # Decimal(" nan ") | ^^^^^^^^^^^^^^ FURB157 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") -50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +50 | # In this one case, " -nan ", the fix has to be | = help: Replace with `" nan "` @@ -225,8 +225,8 @@ FURB157.py:48:9: FURB157 [*] Verbose expression in `Decimal` constructor 48 |-Decimal(float(" nan ")) # Decimal(" nan ") 48 |+Decimal(" nan ") # Decimal(" nan ") 49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") -50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! -51 51 | Decimal(float(" inf ")) # Decimal(" inf ") +50 50 | # In this one case, " -nan ", the fix has to be +51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` FURB157.py:49:9: FURB157 [*] Verbose expression in `Decimal` constructor | @@ -234,8 +234,8 @@ FURB157.py:49:9: FURB157 [*] Verbose expression in `Decimal` constructor 48 | Decimal(float(" nan ")) # Decimal(" nan ") 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") | ^^^^^^^^^^^^^^^ FURB157 -50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! -51 | Decimal(float(" inf ")) # Decimal(" inf ") +50 | # In this one case, " -nan ", the fix has to be +51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` | = help: Replace with `" +nan "` @@ -245,144 +245,144 @@ FURB157.py:49:9: FURB157 [*] Verbose expression in `Decimal` constructor 48 48 | Decimal(float(" nan ")) # Decimal(" nan ") 49 |-Decimal(float(" +nan ")) # Decimal(" +nan ") 49 |+Decimal(" +nan ") # Decimal(" +nan ") -50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! -51 51 | Decimal(float(" inf ")) # Decimal(" inf ") -52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +50 50 | # In this one case, " -nan ", the fix has to be +51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +52 52 | Decimal(float(" -nan ")) # Decimal(" nan ") -FURB157.py:50:9: FURB157 [*] Verbose expression in `Decimal` constructor +FURB157.py:52:9: FURB157 [*] Verbose expression in `Decimal` constructor | -48 | Decimal(float(" nan ")) # Decimal(" nan ") -49 | Decimal(float(" +nan ")) # Decimal(" +nan ") -50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! +50 | # In this one case, " -nan ", the fix has to be +51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +52 | Decimal(float(" -nan ")) # Decimal(" nan ") | ^^^^^^^^^^^^^^^ FURB157 -51 | Decimal(float(" inf ")) # Decimal(" inf ") -52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +53 | Decimal(float(" inf ")) # Decimal(" inf ") +54 | Decimal(float(" +inf ")) # Decimal(" +inf ") | = help: Replace with `" nan "` ℹ Safe fix -47 47 | # https://github.com/astral-sh/ruff/issues/14587 -48 48 | Decimal(float(" nan ")) # Decimal(" nan ") 49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") -50 |-Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! - 50 |+Decimal(" nan ") # Decimal(" nan "), notice the difference! -51 51 | Decimal(float(" inf ")) # Decimal(" inf ") -52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") -53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +50 50 | # In this one case, " -nan ", the fix has to be +51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +52 |-Decimal(float(" -nan ")) # Decimal(" nan ") + 52 |+Decimal(" nan ") # Decimal(" nan ") +53 53 | Decimal(float(" inf ")) # Decimal(" inf ") +54 54 | Decimal(float(" +inf ")) # Decimal(" +inf ") +55 55 | Decimal(float(" -inf ")) # Decimal(" -inf ") -FURB157.py:51:9: FURB157 [*] Verbose expression in `Decimal` constructor +FURB157.py:53:9: FURB157 [*] Verbose expression in `Decimal` constructor | -49 | Decimal(float(" +nan ")) # Decimal(" +nan ") -50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! -51 | Decimal(float(" inf ")) # Decimal(" inf ") +51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +52 | Decimal(float(" -nan ")) # Decimal(" nan ") +53 | Decimal(float(" inf ")) # Decimal(" inf ") | ^^^^^^^^^^^^^^ FURB157 -52 | Decimal(float(" +inf ")) # Decimal(" +inf ") -53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +54 | Decimal(float(" +inf ")) # Decimal(" +inf ") +55 | Decimal(float(" -inf ")) # Decimal(" -inf ") | = help: Replace with `" inf "` ℹ Safe fix -48 48 | Decimal(float(" nan ")) # Decimal(" nan ") -49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") -50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! -51 |-Decimal(float(" inf ")) # Decimal(" inf ") - 51 |+Decimal(" inf ") # Decimal(" inf ") -52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") -53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") -54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +50 50 | # In this one case, " -nan ", the fix has to be +51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +52 52 | Decimal(float(" -nan ")) # Decimal(" nan ") +53 |-Decimal(float(" inf ")) # Decimal(" inf ") + 53 |+Decimal(" inf ") # Decimal(" inf ") +54 54 | Decimal(float(" +inf ")) # Decimal(" +inf ") +55 55 | Decimal(float(" -inf ")) # Decimal(" -inf ") +56 56 | Decimal(float(" infinity ")) # Decimal(" infinity ") -FURB157.py:52:9: FURB157 [*] Verbose expression in `Decimal` constructor +FURB157.py:54:9: FURB157 [*] Verbose expression in `Decimal` constructor | -50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! -51 | Decimal(float(" inf ")) # Decimal(" inf ") -52 | Decimal(float(" +inf ")) # Decimal(" +inf ") +52 | Decimal(float(" -nan ")) # Decimal(" nan ") +53 | Decimal(float(" inf ")) # Decimal(" inf ") +54 | Decimal(float(" +inf ")) # Decimal(" +inf ") | ^^^^^^^^^^^^^^^ FURB157 -53 | Decimal(float(" -inf ")) # Decimal(" -inf ") -54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +55 | Decimal(float(" -inf ")) # Decimal(" -inf ") +56 | Decimal(float(" infinity ")) # Decimal(" infinity ") | = help: Replace with `" +inf "` ℹ Safe fix -49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") -50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! -51 51 | Decimal(float(" inf ")) # Decimal(" inf ") -52 |-Decimal(float(" +inf ")) # Decimal(" +inf ") - 52 |+Decimal(" +inf ") # Decimal(" +inf ") -53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") -54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") -55 55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +52 52 | Decimal(float(" -nan ")) # Decimal(" nan ") +53 53 | Decimal(float(" inf ")) # Decimal(" inf ") +54 |-Decimal(float(" +inf ")) # Decimal(" +inf ") + 54 |+Decimal(" +inf ") # Decimal(" +inf ") +55 55 | Decimal(float(" -inf ")) # Decimal(" -inf ") +56 56 | Decimal(float(" infinity ")) # Decimal(" infinity ") +57 57 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") -FURB157.py:53:9: FURB157 [*] Verbose expression in `Decimal` constructor +FURB157.py:55:9: FURB157 [*] Verbose expression in `Decimal` constructor | -51 | Decimal(float(" inf ")) # Decimal(" inf ") -52 | Decimal(float(" +inf ")) # Decimal(" +inf ") -53 | Decimal(float(" -inf ")) # Decimal(" -inf ") +53 | Decimal(float(" inf ")) # Decimal(" inf ") +54 | Decimal(float(" +inf ")) # Decimal(" +inf ") +55 | Decimal(float(" -inf ")) # Decimal(" -inf ") | ^^^^^^^^^^^^^^^ FURB157 -54 | Decimal(float(" infinity ")) # Decimal(" infinity ") -55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +56 | Decimal(float(" infinity ")) # Decimal(" infinity ") +57 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") | = help: Replace with `" -inf "` ℹ Safe fix -50 50 | Decimal(float(" -nan ")) # Decimal(" nan "), notice the difference! -51 51 | Decimal(float(" inf ")) # Decimal(" inf ") -52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") -53 |-Decimal(float(" -inf ")) # Decimal(" -inf ") - 53 |+Decimal(" -inf ") # Decimal(" -inf ") -54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") -55 55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") -56 56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") +52 52 | Decimal(float(" -nan ")) # Decimal(" nan ") +53 53 | Decimal(float(" inf ")) # Decimal(" inf ") +54 54 | Decimal(float(" +inf ")) # Decimal(" +inf ") +55 |-Decimal(float(" -inf ")) # Decimal(" -inf ") + 55 |+Decimal(" -inf ") # Decimal(" -inf ") +56 56 | Decimal(float(" infinity ")) # Decimal(" infinity ") +57 57 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +58 58 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") -FURB157.py:54:9: FURB157 [*] Verbose expression in `Decimal` constructor +FURB157.py:56:9: FURB157 [*] Verbose expression in `Decimal` constructor | -52 | Decimal(float(" +inf ")) # Decimal(" +inf ") -53 | Decimal(float(" -inf ")) # Decimal(" -inf ") -54 | Decimal(float(" infinity ")) # Decimal(" infinity ") +54 | Decimal(float(" +inf ")) # Decimal(" +inf ") +55 | Decimal(float(" -inf ")) # Decimal(" -inf ") +56 | Decimal(float(" infinity ")) # Decimal(" infinity ") | ^^^^^^^^^^^^^^^^^^^ FURB157 -55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") -56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") +57 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +58 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") | = help: Replace with `" infinity "` ℹ Safe fix -51 51 | Decimal(float(" inf ")) # Decimal(" inf ") -52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") -53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") -54 |-Decimal(float(" infinity ")) # Decimal(" infinity ") - 54 |+Decimal(" infinity ") # Decimal(" infinity ") -55 55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") -56 56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") - -FURB157.py:55:9: FURB157 [*] Verbose expression in `Decimal` constructor - | -53 | Decimal(float(" -inf ")) # Decimal(" -inf ") -54 | Decimal(float(" infinity ")) # Decimal(" infinity ") -55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +53 53 | Decimal(float(" inf ")) # Decimal(" inf ") +54 54 | Decimal(float(" +inf ")) # Decimal(" +inf ") +55 55 | Decimal(float(" -inf ")) # Decimal(" -inf ") +56 |-Decimal(float(" infinity ")) # Decimal(" infinity ") + 56 |+Decimal(" infinity ") # Decimal(" infinity ") +57 57 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +58 58 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") + +FURB157.py:57:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +55 | Decimal(float(" -inf ")) # Decimal(" -inf ") +56 | Decimal(float(" infinity ")) # Decimal(" infinity ") +57 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") | ^^^^^^^^^^^^^^^^^^^^ FURB157 -56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") +58 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") | = help: Replace with `" +infinity "` ℹ Safe fix -52 52 | Decimal(float(" +inf ")) # Decimal(" +inf ") -53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") -54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") -55 |-Decimal(float(" +infinity ")) # Decimal(" +infinity ") - 55 |+Decimal(" +infinity ") # Decimal(" +infinity ") -56 56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") - -FURB157.py:56:9: FURB157 [*] Verbose expression in `Decimal` constructor - | -54 | Decimal(float(" infinity ")) # Decimal(" infinity ") -55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") -56 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") +54 54 | Decimal(float(" +inf ")) # Decimal(" +inf ") +55 55 | Decimal(float(" -inf ")) # Decimal(" -inf ") +56 56 | Decimal(float(" infinity ")) # Decimal(" infinity ") +57 |-Decimal(float(" +infinity ")) # Decimal(" +infinity ") + 57 |+Decimal(" +infinity ") # Decimal(" +infinity ") +58 58 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") + +FURB157.py:58:9: FURB157 [*] Verbose expression in `Decimal` constructor + | +56 | Decimal(float(" infinity ")) # Decimal(" infinity ") +57 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +58 | Decimal(float(" -infinity ")) # Decimal(" -infinity ") | ^^^^^^^^^^^^^^^^^^^^ FURB157 | = help: Replace with `" -infinity "` ℹ Safe fix -53 53 | Decimal(float(" -inf ")) # Decimal(" -inf ") -54 54 | Decimal(float(" infinity ")) # Decimal(" infinity ") -55 55 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") -56 |-Decimal(float(" -infinity ")) # Decimal(" -infinity ") - 56 |+Decimal(" -infinity ") # Decimal(" -infinity ") +55 55 | Decimal(float(" -inf ")) # Decimal(" -inf ") +56 56 | Decimal(float(" infinity ")) # Decimal(" infinity ") +57 57 | Decimal(float(" +infinity ")) # Decimal(" +infinity ") +58 |-Decimal(float(" -infinity ")) # Decimal(" -infinity ") + 58 |+Decimal(" -infinity ") # Decimal(" -infinity ") From 697b5ba3d9042eefae2dcafc62667ddc7d293129 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Sun, 1 Dec 2024 13:50:21 -0600 Subject: [PATCH 09/11] fix typo in fixture --- crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py b/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py index 2314bba4f7045..936bfdae3d8de 100644 --- a/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py +++ b/crates/ruff_linter/resources/test/fixtures/refurb/FURB157.py @@ -48,7 +48,7 @@ Decimal(float(" nan ")) # Decimal(" nan ") Decimal(float(" +nan ")) # Decimal(" +nan ") # In this one case, " -nan ", the fix has to be -# `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +# `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` Decimal(float(" -nan ")) # Decimal(" nan ") Decimal(float(" inf ")) # Decimal(" inf ") Decimal(float(" +inf ")) # Decimal(" +inf ") From 2beb6f224962f68e3fcff00fee11f99bd23c14b2 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Sun, 1 Dec 2024 14:17:57 -0600 Subject: [PATCH 10/11] update snapshot --- ...rules__refurb__tests__FURB157_FURB157.py.snap | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap index 5387b57883b48..967e552a1b2c8 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB157_FURB157.py.snap @@ -226,7 +226,7 @@ FURB157.py:48:9: FURB157 [*] Verbose expression in `Decimal` constructor 48 |+Decimal(" nan ") # Decimal(" nan ") 49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") 50 50 | # In this one case, " -nan ", the fix has to be -51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +51 51 | # `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` FURB157.py:49:9: FURB157 [*] Verbose expression in `Decimal` constructor | @@ -235,7 +235,7 @@ FURB157.py:49:9: FURB157 [*] Verbose expression in `Decimal` constructor 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") | ^^^^^^^^^^^^^^^ FURB157 50 | # In this one case, " -nan ", the fix has to be -51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +51 | # `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` | = help: Replace with `" +nan "` @@ -246,13 +246,13 @@ FURB157.py:49:9: FURB157 [*] Verbose expression in `Decimal` constructor 49 |-Decimal(float(" +nan ")) # Decimal(" +nan ") 49 |+Decimal(" +nan ") # Decimal(" +nan ") 50 50 | # In this one case, " -nan ", the fix has to be -51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +51 51 | # `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` 52 52 | Decimal(float(" -nan ")) # Decimal(" nan ") FURB157.py:52:9: FURB157 [*] Verbose expression in `Decimal` constructor | 50 | # In this one case, " -nan ", the fix has to be -51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +51 | # `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` 52 | Decimal(float(" -nan ")) # Decimal(" nan ") | ^^^^^^^^^^^^^^^ FURB157 53 | Decimal(float(" inf ")) # Decimal(" inf ") @@ -263,7 +263,7 @@ FURB157.py:52:9: FURB157 [*] Verbose expression in `Decimal` constructor ℹ Safe fix 49 49 | Decimal(float(" +nan ")) # Decimal(" +nan ") 50 50 | # In this one case, " -nan ", the fix has to be -51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +51 51 | # `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` 52 |-Decimal(float(" -nan ")) # Decimal(" nan ") 52 |+Decimal(" nan ") # Decimal(" nan ") 53 53 | Decimal(float(" inf ")) # Decimal(" inf ") @@ -272,7 +272,7 @@ FURB157.py:52:9: FURB157 [*] Verbose expression in `Decimal` constructor FURB157.py:53:9: FURB157 [*] Verbose expression in `Decimal` constructor | -51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +51 | # `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` 52 | Decimal(float(" -nan ")) # Decimal(" nan ") 53 | Decimal(float(" inf ")) # Decimal(" inf ") | ^^^^^^^^^^^^^^ FURB157 @@ -283,7 +283,7 @@ FURB157.py:53:9: FURB157 [*] Verbose expression in `Decimal` constructor ℹ Safe fix 50 50 | # In this one case, " -nan ", the fix has to be -51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +51 51 | # `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` 52 52 | Decimal(float(" -nan ")) # Decimal(" nan ") 53 |-Decimal(float(" inf ")) # Decimal(" inf ") 53 |+Decimal(" inf ") # Decimal(" inf ") @@ -303,7 +303,7 @@ FURB157.py:54:9: FURB157 [*] Verbose expression in `Decimal` constructor = help: Replace with `" +inf "` ℹ Safe fix -51 51 | # `Decimal(" nan ")`` because `Deimcal("-nan") != Decimal(float("-nan"))` +51 51 | # `Decimal(" nan ")`` because `Decimal("-nan") != Decimal(float("-nan"))` 52 52 | Decimal(float(" -nan ")) # Decimal(" nan ") 53 53 | Decimal(float(" inf ")) # Decimal(" inf ") 54 |-Decimal(float(" +inf ")) # Decimal(" +inf ") From f5d4676a416013ca934c52237858db95bffd4247 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Sun, 1 Dec 2024 14:21:58 -0600 Subject: [PATCH 11/11] avoid allocation --- .../rules/verbose_decimal_constructor.rs | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs index 7ad4156836da1..9e726d8c8e0cf 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs @@ -153,26 +153,31 @@ pub(crate) fn verbose_decimal_constructor(checker: &mut Checker, call: &ast::Exp return; }; - let normalized_float_string = float.value.to_str().trim().to_lowercase(); - - if !matches!( - normalized_float_string.as_str(), - "inf" - | "+inf" - | "-inf" - | "infinity" - | "+infinity" - | "-infinity" - | "nan" - | "+nan" - | "-nan" - ) { + let trimmed = float.value.to_str().trim(); + let mut matches_non_finite_keyword = false; + for non_finite_keyword in [ + "inf", + "+inf", + "-inf", + "infinity", + "+infinity", + "-infinity", + "nan", + "+nan", + "-nan", + ] { + if trimmed.eq_ignore_ascii_case(non_finite_keyword) { + matches_non_finite_keyword = true; + break; + } + } + if !matches_non_finite_keyword { return; } let mut replacement = checker.locator().slice(float).to_string(); // `Decimal(float("-nan")) == Decimal("nan")` - if &normalized_float_string == "-nan" { + if trimmed.eq_ignore_ascii_case("-nan") { replacement.remove(replacement.find('-').unwrap()); } let mut diagnostic = Diagnostic::new(