From 37e9acf0a4762cd4cbd37ae09ecd39e7cce5b4b3 Mon Sep 17 00:00:00 2001 From: InSyncWithFoo Date: Sat, 7 Dec 2024 05:10:27 +0000 Subject: [PATCH] Introduce `::Likely` --- .../resources/test/fixtures/ruff/RUF046.py | 60 +- .../resources/test/fixtures/ruff/RUF057.py | 9 +- .../ruff/rules/unnecessary_cast_to_int.rs | 28 +- ...uff__tests__preview__RUF046_RUF046.py.snap | 688 +----------------- ...uff__tests__preview__RUF057_RUF057.py.snap | 243 ++++--- 5 files changed, 188 insertions(+), 840 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF046.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF046.py index 0676e375a0aa64..5aacc51ae87675 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF046.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF046.py @@ -47,15 +47,45 @@ int(math.floor()) int(math.trunc()) + +### No errors + int(1 and 0) int(0 or -1) +int(foo if ... else 4) + +int(round()) +int(round(ndigits=2)) +int(round(3.4)) +int(round(3.4, 0)) +int(round(3.4, 2)) +int(round(5, foo)) + +int(3.14) +int(2.8j) + async def f(): int(await f()) int(foo.bar) int(bar([1][False])) +int(1 == 1) +int(1 != 1) +int(1 < 1) +int(1 <= 1) +int(1 > 1) +int(1 >= 1) +int(1 in 1) +int(1 not in 1) +int(1 is 1) +int(2 is not 3) +int(foo in 1) +int(foo not in 1) +int(foo is 1) +int(foo is not 1) + int(1 == 2 == 3) int(foo == 1) int(foo != 1) @@ -80,36 +110,6 @@ async def f(): int(foo & 1) int(foo // 1) -int(foo if ... else 4) - -int(round()) -int(round(ndigits=2)) -int(round(3.4)) -int(round(3.4, 0)) -int(round(3.4, 2)) -int(round(5, foo)) - - -### No errors - -int(3.14) -int(2.8j) - -int(1 == 1) -int(1 != 1) -int(1 < 1) -int(1 <= 1) -int(1 > 1) -int(1 >= 1) -int(1 in 1) -int(1 not in 1) -int(1 is 1) -int(2 is not 3) -int(foo in 1) -int(foo not in 1) -int(foo is 1) -int(foo is not 1) - int(v := 3.7) int(not 109) diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF057.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF057.py index 24f1536cf93180..f7f40b831ff0f4 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF057.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF057.py @@ -9,13 +9,10 @@ round(1, ndigits=0) round(number=1, ndigits=0) +round(1, None) round(1, ndigits=None) round(number=1, ndigits=None) -round(1., 0) -round(1., ndigits=0) -round(number=1., ndigits=0) - round(1., None) round(1., ndigits=None) round(number=1., ndigits=None) @@ -26,6 +23,10 @@ ### No errors +round(1., 0) +round(1., ndigits=0) +round(number=1., ndigits=0) + round(1, 1) round(1, bar) round(1., bar) diff --git a/crates/ruff_linter/src/rules/ruff/rules/unnecessary_cast_to_int.rs b/crates/ruff_linter/src/rules/ruff/rules/unnecessary_cast_to_int.rs index a2d83af48c5bd1..8b824127de2609 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/unnecessary_cast_to_int.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/unnecessary_cast_to_int.rs @@ -59,13 +59,13 @@ impl AlwaysFixableViolation for UnnecessaryCastToInt { #[derive(Debug, PartialEq, Eq, Clone, Copy, is_macro::Is)] pub(crate) enum IsStrictlyInt { /// The value is known with absolute certainty to be a strict instance of `int`. - #[is(name = "true")] True, /// The value is known with absolute certainty to *not* be a strict instance of `int`. - #[is(name = "false")] False, - /// It is not possible to statically determine that the value is a strict instance of `int`. - #[is(name = "maybe")] + /// The evaluation context has a high chance of producing a strict instance of `int`. + Likely, + /// It is not possible to statically determine that the value + /// is or is not a strict instance of `int`. Maybe, } @@ -79,8 +79,8 @@ pub(crate) fn unnecessary_cast_to_int(checker: &mut Checker, call: &ExprCall) { let applicability = match expr_is_strictly_int(semantic, argument) { IsStrictlyInt::True => Applicability::Safe, - IsStrictlyInt::Maybe => Applicability::Unsafe, - IsStrictlyInt::False => return, + IsStrictlyInt::Likely => Applicability::Unsafe, + _ => return, }; let edit = replace_with_inner(checker.locator(), call.range, argument.range()); @@ -177,6 +177,11 @@ pub(crate) fn expr_is_strictly_int(semantic: &SemanticModel, expr: &Expr) -> IsS Operator::MatMult => IsStrictlyInt::False, _ => IsStrictlyInt::True, }, + [IsStrictlyInt::Likely, IsStrictlyInt::Likely] => match op { + Operator::Div => IsStrictlyInt::Maybe, + Operator::MatMult => IsStrictlyInt::Maybe, + _ => IsStrictlyInt::Likely, + }, _ => IsStrictlyInt::Maybe, } } @@ -187,6 +192,7 @@ pub(crate) fn expr_is_strictly_int(semantic: &SemanticModel, expr: &Expr) -> IsS match [body_is_strictly_int, else_is_strictly_int] { [IsStrictlyInt::True, IsStrictlyInt::True] => IsStrictlyInt::True, + [IsStrictlyInt::Likely, IsStrictlyInt::Likely] => IsStrictlyInt::Likely, [IsStrictlyInt::False, IsStrictlyInt::False] => IsStrictlyInt::False, _ => IsStrictlyInt::Maybe, } @@ -241,7 +247,7 @@ fn call_strictly_returns_int(semantic: &SemanticModel, call: &ExprCall) -> IsStr | ["math", "comb" | "factorial" | "gcd" | "lcm" | "isqrt" | "perm"] => IsStrictlyInt::True, // Depends on `__ceil__`/`__floor__`/`__trunc__` - ["math", "ceil" | "floor" | "trunc"] => IsStrictlyInt::Maybe, + ["math", "ceil" | "floor" | "trunc"] => IsStrictlyInt::Likely, // Depends on `ndigits` and `number.__round__` ["" | "builtins", "round"] => round_call_strictly_returns_int(semantic, arguments), @@ -265,14 +271,16 @@ fn round_call_strictly_returns_int( return IsStrictlyInt::Maybe; }; - match expr_is_strictly_int(semantic, number) { + let number_is_strictly_int = expr_is_strictly_int(semantic, number); + + match number_is_strictly_int { IsStrictlyInt::Maybe => return IsStrictlyInt::Maybe, IsStrictlyInt::False => return IsStrictlyInt::Maybe, - IsStrictlyInt::True => {} + _ => {} }; match ndigits { - None | Some(Expr::NoneLiteral(..)) => IsStrictlyInt::True, + None | Some(Expr::NoneLiteral(..)) => number_is_strictly_int, Some(Expr::NumberLiteral(ExprNumberLiteral { value, .. })) => { if is_literal_zero(value) { diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF046_RUF046.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF046_RUF046.py.snap index b74bce6b9e936c..846ed0867a4393 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF046_RUF046.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF046_RUF046.py.snap @@ -679,7 +679,7 @@ RUF046.py:47:1: RUF046 [*] Value being casted is already an integer 47 |+(math.floor()) 48 48 | int(math.trunc()) 49 49 | -50 50 | int(1 and 0) +50 50 | RUF046.py:48:1: RUF046 [*] Value being casted is already an integer | @@ -687,8 +687,6 @@ RUF046.py:48:1: RUF046 [*] Value being casted is already an integer 47 | int(math.floor()) 48 | int(math.trunc()) | ^^^^^^^^^^^^^^^^^ RUF046 -49 | -50 | int(1 and 0) | = help: Remove unnecessary conversion to `int` @@ -699,685 +697,5 @@ RUF046.py:48:1: RUF046 [*] Value being casted is already an integer 48 |-int(math.trunc()) 48 |+(math.trunc()) 49 49 | -50 50 | int(1 and 0) -51 51 | int(0 or -1) - -RUF046.py:50:1: RUF046 [*] Value being casted is already an integer - | -48 | int(math.trunc()) -49 | -50 | int(1 and 0) - | ^^^^^^^^^^^^ RUF046 -51 | int(0 or -1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -47 47 | int(math.floor()) -48 48 | int(math.trunc()) -49 49 | -50 |-int(1 and 0) - 50 |+(1 and 0) -51 51 | int(0 or -1) -52 52 | -53 53 | async def f(): - -RUF046.py:51:1: RUF046 [*] Value being casted is already an integer - | -50 | int(1 and 0) -51 | int(0 or -1) - | ^^^^^^^^^^^^ RUF046 -52 | -53 | async def f(): - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -48 48 | int(math.trunc()) -49 49 | -50 50 | int(1 and 0) -51 |-int(0 or -1) - 51 |+(0 or -1) -52 52 | -53 53 | async def f(): -54 54 | int(await f()) - -RUF046.py:54:5: RUF046 [*] Value being casted is already an integer - | -53 | async def f(): -54 | int(await f()) - | ^^^^^^^^^^^^^^ RUF046 -55 | -56 | int(foo.bar) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -51 51 | int(0 or -1) -52 52 | -53 53 | async def f(): -54 |- int(await f()) - 54 |+ (await f()) -55 55 | -56 56 | int(foo.bar) -57 57 | int(bar([1][False])) - -RUF046.py:56:1: RUF046 [*] Value being casted is already an integer - | -54 | int(await f()) -55 | -56 | int(foo.bar) - | ^^^^^^^^^^^^ RUF046 -57 | int(bar([1][False])) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -53 53 | async def f(): -54 54 | int(await f()) -55 55 | -56 |-int(foo.bar) - 56 |+(foo.bar) -57 57 | int(bar([1][False])) -58 58 | -59 59 | int(1 == 2 == 3) - -RUF046.py:57:1: RUF046 [*] Value being casted is already an integer - | -56 | int(foo.bar) -57 | int(bar([1][False])) - | ^^^^^^^^^^^^^^^^^^^^ RUF046 -58 | -59 | int(1 == 2 == 3) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -54 54 | int(await f()) -55 55 | -56 56 | int(foo.bar) -57 |-int(bar([1][False])) - 57 |+(bar([1][False])) -58 58 | -59 59 | int(1 == 2 == 3) -60 60 | int(foo == 1) - -RUF046.py:59:1: RUF046 [*] Value being casted is already an integer - | -57 | int(bar([1][False])) -58 | -59 | int(1 == 2 == 3) - | ^^^^^^^^^^^^^^^^ RUF046 -60 | int(foo == 1) -61 | int(foo != 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -56 56 | int(foo.bar) -57 57 | int(bar([1][False])) -58 58 | -59 |-int(1 == 2 == 3) - 59 |+(1 == 2 == 3) -60 60 | int(foo == 1) -61 61 | int(foo != 1) -62 62 | int(foo < 1) - -RUF046.py:60:1: RUF046 [*] Value being casted is already an integer - | -59 | int(1 == 2 == 3) -60 | int(foo == 1) - | ^^^^^^^^^^^^^ RUF046 -61 | int(foo != 1) -62 | int(foo < 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -57 57 | int(bar([1][False])) -58 58 | -59 59 | int(1 == 2 == 3) -60 |-int(foo == 1) - 60 |+(foo == 1) -61 61 | int(foo != 1) -62 62 | int(foo < 1) -63 63 | int(foo <= 1) - -RUF046.py:61:1: RUF046 [*] Value being casted is already an integer - | -59 | int(1 == 2 == 3) -60 | int(foo == 1) -61 | int(foo != 1) - | ^^^^^^^^^^^^^ RUF046 -62 | int(foo < 1) -63 | int(foo <= 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -58 58 | -59 59 | int(1 == 2 == 3) -60 60 | int(foo == 1) -61 |-int(foo != 1) - 61 |+(foo != 1) -62 62 | int(foo < 1) -63 63 | int(foo <= 1) -64 64 | int(foo > 1) - -RUF046.py:62:1: RUF046 [*] Value being casted is already an integer - | -60 | int(foo == 1) -61 | int(foo != 1) -62 | int(foo < 1) - | ^^^^^^^^^^^^ RUF046 -63 | int(foo <= 1) -64 | int(foo > 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -59 59 | int(1 == 2 == 3) -60 60 | int(foo == 1) -61 61 | int(foo != 1) -62 |-int(foo < 1) - 62 |+(foo < 1) -63 63 | int(foo <= 1) -64 64 | int(foo > 1) -65 65 | int(foo >= 1) - -RUF046.py:63:1: RUF046 [*] Value being casted is already an integer - | -61 | int(foo != 1) -62 | int(foo < 1) -63 | int(foo <= 1) - | ^^^^^^^^^^^^^ RUF046 -64 | int(foo > 1) -65 | int(foo >= 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -60 60 | int(foo == 1) -61 61 | int(foo != 1) -62 62 | int(foo < 1) -63 |-int(foo <= 1) - 63 |+(foo <= 1) -64 64 | int(foo > 1) -65 65 | int(foo >= 1) -66 66 | - -RUF046.py:64:1: RUF046 [*] Value being casted is already an integer - | -62 | int(foo < 1) -63 | int(foo <= 1) -64 | int(foo > 1) - | ^^^^^^^^^^^^ RUF046 -65 | int(foo >= 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -61 61 | int(foo != 1) -62 62 | int(foo < 1) -63 63 | int(foo <= 1) -64 |-int(foo > 1) - 64 |+(foo > 1) -65 65 | int(foo >= 1) -66 66 | -67 67 | int(v := {}[{}['']]) - -RUF046.py:65:1: RUF046 [*] Value being casted is already an integer - | -63 | int(foo <= 1) -64 | int(foo > 1) -65 | int(foo >= 1) - | ^^^^^^^^^^^^^ RUF046 -66 | -67 | int(v := {}[{}['']]) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -62 62 | int(foo < 1) -63 63 | int(foo <= 1) -64 64 | int(foo > 1) -65 |-int(foo >= 1) - 65 |+(foo >= 1) -66 66 | -67 67 | int(v := {}[{}['']]) -68 68 | - -RUF046.py:67:1: RUF046 [*] Value being casted is already an integer - | -65 | int(foo >= 1) -66 | -67 | int(v := {}[{}['']]) - | ^^^^^^^^^^^^^^^^^^^^ RUF046 -68 | -69 | int(foo + 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -64 64 | int(foo > 1) -65 65 | int(foo >= 1) -66 66 | -67 |-int(v := {}[{}['']]) - 67 |+(v := {}[{}['']]) -68 68 | -69 69 | int(foo + 1) -70 70 | int(foo - 1) - -RUF046.py:69:1: RUF046 [*] Value being casted is already an integer - | -67 | int(v := {}[{}['']]) -68 | -69 | int(foo + 1) - | ^^^^^^^^^^^^ RUF046 -70 | int(foo - 1) -71 | int(foo * 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -66 66 | -67 67 | int(v := {}[{}['']]) -68 68 | -69 |-int(foo + 1) - 69 |+(foo + 1) -70 70 | int(foo - 1) -71 71 | int(foo * 1) -72 72 | int(foo @ 1) - -RUF046.py:70:1: RUF046 [*] Value being casted is already an integer - | -69 | int(foo + 1) -70 | int(foo - 1) - | ^^^^^^^^^^^^ RUF046 -71 | int(foo * 1) -72 | int(foo @ 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -67 67 | int(v := {}[{}['']]) -68 68 | -69 69 | int(foo + 1) -70 |-int(foo - 1) - 70 |+(foo - 1) -71 71 | int(foo * 1) -72 72 | int(foo @ 1) -73 73 | int(foo / 1) - -RUF046.py:71:1: RUF046 [*] Value being casted is already an integer - | -69 | int(foo + 1) -70 | int(foo - 1) -71 | int(foo * 1) - | ^^^^^^^^^^^^ RUF046 -72 | int(foo @ 1) -73 | int(foo / 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -68 68 | -69 69 | int(foo + 1) -70 70 | int(foo - 1) -71 |-int(foo * 1) - 71 |+(foo * 1) -72 72 | int(foo @ 1) -73 73 | int(foo / 1) -74 74 | int(foo % 1) - -RUF046.py:72:1: RUF046 [*] Value being casted is already an integer - | -70 | int(foo - 1) -71 | int(foo * 1) -72 | int(foo @ 1) - | ^^^^^^^^^^^^ RUF046 -73 | int(foo / 1) -74 | int(foo % 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -69 69 | int(foo + 1) -70 70 | int(foo - 1) -71 71 | int(foo * 1) -72 |-int(foo @ 1) - 72 |+(foo @ 1) -73 73 | int(foo / 1) -74 74 | int(foo % 1) -75 75 | int(foo ** 1) - -RUF046.py:73:1: RUF046 [*] Value being casted is already an integer - | -71 | int(foo * 1) -72 | int(foo @ 1) -73 | int(foo / 1) - | ^^^^^^^^^^^^ RUF046 -74 | int(foo % 1) -75 | int(foo ** 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -70 70 | int(foo - 1) -71 71 | int(foo * 1) -72 72 | int(foo @ 1) -73 |-int(foo / 1) - 73 |+(foo / 1) -74 74 | int(foo % 1) -75 75 | int(foo ** 1) -76 76 | int(foo << 1) - -RUF046.py:74:1: RUF046 [*] Value being casted is already an integer - | -72 | int(foo @ 1) -73 | int(foo / 1) -74 | int(foo % 1) - | ^^^^^^^^^^^^ RUF046 -75 | int(foo ** 1) -76 | int(foo << 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -71 71 | int(foo * 1) -72 72 | int(foo @ 1) -73 73 | int(foo / 1) -74 |-int(foo % 1) - 74 |+(foo % 1) -75 75 | int(foo ** 1) -76 76 | int(foo << 1) -77 77 | int(foo >> 1) - -RUF046.py:75:1: RUF046 [*] Value being casted is already an integer - | -73 | int(foo / 1) -74 | int(foo % 1) -75 | int(foo ** 1) - | ^^^^^^^^^^^^^ RUF046 -76 | int(foo << 1) -77 | int(foo >> 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -72 72 | int(foo @ 1) -73 73 | int(foo / 1) -74 74 | int(foo % 1) -75 |-int(foo ** 1) - 75 |+(foo ** 1) -76 76 | int(foo << 1) -77 77 | int(foo >> 1) -78 78 | int(foo | 1) - -RUF046.py:76:1: RUF046 [*] Value being casted is already an integer - | -74 | int(foo % 1) -75 | int(foo ** 1) -76 | int(foo << 1) - | ^^^^^^^^^^^^^ RUF046 -77 | int(foo >> 1) -78 | int(foo | 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -73 73 | int(foo / 1) -74 74 | int(foo % 1) -75 75 | int(foo ** 1) -76 |-int(foo << 1) - 76 |+(foo << 1) -77 77 | int(foo >> 1) -78 78 | int(foo | 1) -79 79 | int(foo ^ 1) - -RUF046.py:77:1: RUF046 [*] Value being casted is already an integer - | -75 | int(foo ** 1) -76 | int(foo << 1) -77 | int(foo >> 1) - | ^^^^^^^^^^^^^ RUF046 -78 | int(foo | 1) -79 | int(foo ^ 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -74 74 | int(foo % 1) -75 75 | int(foo ** 1) -76 76 | int(foo << 1) -77 |-int(foo >> 1) - 77 |+(foo >> 1) -78 78 | int(foo | 1) -79 79 | int(foo ^ 1) -80 80 | int(foo & 1) - -RUF046.py:78:1: RUF046 [*] Value being casted is already an integer - | -76 | int(foo << 1) -77 | int(foo >> 1) -78 | int(foo | 1) - | ^^^^^^^^^^^^ RUF046 -79 | int(foo ^ 1) -80 | int(foo & 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -75 75 | int(foo ** 1) -76 76 | int(foo << 1) -77 77 | int(foo >> 1) -78 |-int(foo | 1) - 78 |+(foo | 1) -79 79 | int(foo ^ 1) -80 80 | int(foo & 1) -81 81 | int(foo // 1) - -RUF046.py:79:1: RUF046 [*] Value being casted is already an integer - | -77 | int(foo >> 1) -78 | int(foo | 1) -79 | int(foo ^ 1) - | ^^^^^^^^^^^^ RUF046 -80 | int(foo & 1) -81 | int(foo // 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -76 76 | int(foo << 1) -77 77 | int(foo >> 1) -78 78 | int(foo | 1) -79 |-int(foo ^ 1) - 79 |+(foo ^ 1) -80 80 | int(foo & 1) -81 81 | int(foo // 1) -82 82 | - -RUF046.py:80:1: RUF046 [*] Value being casted is already an integer - | -78 | int(foo | 1) -79 | int(foo ^ 1) -80 | int(foo & 1) - | ^^^^^^^^^^^^ RUF046 -81 | int(foo // 1) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -77 77 | int(foo >> 1) -78 78 | int(foo | 1) -79 79 | int(foo ^ 1) -80 |-int(foo & 1) - 80 |+(foo & 1) -81 81 | int(foo // 1) -82 82 | -83 83 | int(foo if ... else 4) - -RUF046.py:81:1: RUF046 [*] Value being casted is already an integer - | -79 | int(foo ^ 1) -80 | int(foo & 1) -81 | int(foo // 1) - | ^^^^^^^^^^^^^ RUF046 -82 | -83 | int(foo if ... else 4) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -78 78 | int(foo | 1) -79 79 | int(foo ^ 1) -80 80 | int(foo & 1) -81 |-int(foo // 1) - 81 |+(foo // 1) -82 82 | -83 83 | int(foo if ... else 4) -84 84 | - -RUF046.py:83:1: RUF046 [*] Value being casted is already an integer - | -81 | int(foo // 1) -82 | -83 | int(foo if ... else 4) - | ^^^^^^^^^^^^^^^^^^^^^^ RUF046 -84 | -85 | int(round()) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -80 80 | int(foo & 1) -81 81 | int(foo // 1) -82 82 | -83 |-int(foo if ... else 4) - 83 |+(foo if ... else 4) -84 84 | -85 85 | int(round()) -86 86 | int(round(ndigits=2)) - -RUF046.py:85:1: RUF046 [*] Value being casted is already an integer - | -83 | int(foo if ... else 4) -84 | -85 | int(round()) - | ^^^^^^^^^^^^ RUF046 -86 | int(round(ndigits=2)) -87 | int(round(3.4)) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -82 82 | -83 83 | int(foo if ... else 4) -84 84 | -85 |-int(round()) - 85 |+(round()) -86 86 | int(round(ndigits=2)) -87 87 | int(round(3.4)) -88 88 | int(round(3.4, 0)) - -RUF046.py:86:1: RUF046 [*] Value being casted is already an integer - | -85 | int(round()) -86 | int(round(ndigits=2)) - | ^^^^^^^^^^^^^^^^^^^^^ RUF046 -87 | int(round(3.4)) -88 | int(round(3.4, 0)) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -83 83 | int(foo if ... else 4) -84 84 | -85 85 | int(round()) -86 |-int(round(ndigits=2)) - 86 |+(round(ndigits=2)) -87 87 | int(round(3.4)) -88 88 | int(round(3.4, 0)) -89 89 | int(round(3.4, 2)) - -RUF046.py:87:1: RUF046 [*] Value being casted is already an integer - | -85 | int(round()) -86 | int(round(ndigits=2)) -87 | int(round(3.4)) - | ^^^^^^^^^^^^^^^ RUF046 -88 | int(round(3.4, 0)) -89 | int(round(3.4, 2)) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -84 84 | -85 85 | int(round()) -86 86 | int(round(ndigits=2)) -87 |-int(round(3.4)) - 87 |+(round(3.4)) -88 88 | int(round(3.4, 0)) -89 89 | int(round(3.4, 2)) -90 90 | int(round(5, foo)) - -RUF046.py:88:1: RUF046 [*] Value being casted is already an integer - | -86 | int(round(ndigits=2)) -87 | int(round(3.4)) -88 | int(round(3.4, 0)) - | ^^^^^^^^^^^^^^^^^^ RUF046 -89 | int(round(3.4, 2)) -90 | int(round(5, foo)) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -85 85 | int(round()) -86 86 | int(round(ndigits=2)) -87 87 | int(round(3.4)) -88 |-int(round(3.4, 0)) - 88 |+(round(3.4, 0)) -89 89 | int(round(3.4, 2)) -90 90 | int(round(5, foo)) -91 91 | - -RUF046.py:89:1: RUF046 [*] Value being casted is already an integer - | -87 | int(round(3.4)) -88 | int(round(3.4, 0)) -89 | int(round(3.4, 2)) - | ^^^^^^^^^^^^^^^^^^ RUF046 -90 | int(round(5, foo)) - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -86 86 | int(round(ndigits=2)) -87 87 | int(round(3.4)) -88 88 | int(round(3.4, 0)) -89 |-int(round(3.4, 2)) - 89 |+(round(3.4, 2)) -90 90 | int(round(5, foo)) -91 91 | -92 92 | - -RUF046.py:90:1: RUF046 [*] Value being casted is already an integer - | -88 | int(round(3.4, 0)) -89 | int(round(3.4, 2)) -90 | int(round(5, foo)) - | ^^^^^^^^^^^^^^^^^^ RUF046 - | - = help: Remove unnecessary conversion to `int` - -ℹ Unsafe fix -87 87 | int(round(3.4)) -88 88 | int(round(3.4, 0)) -89 89 | int(round(3.4, 2)) -90 |-int(round(5, foo)) - 90 |+(round(5, foo)) -91 91 | -92 92 | -93 93 | ### No errors +50 50 | +51 51 | ### No errors diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF057_RUF057.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF057_RUF057.py.snap index c8ab2394ead134..180aa183f75d5f 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF057_RUF057.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF057_RUF057.py.snap @@ -40,7 +40,7 @@ RUF057.py:9:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` 9 |+round(1) 10 10 | round(number=1, ndigits=0) 11 11 | -12 12 | round(1, ndigits=None) +12 12 | round(1, None) RUF057.py:10:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` | @@ -49,7 +49,7 @@ RUF057.py:10:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` 10 | round(number=1, ndigits=0) | ^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF057 11 | -12 | round(1, ndigits=None) +12 | round(1, None) | = help: Remove second argument @@ -60,16 +60,17 @@ RUF057.py:10:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` 10 |-round(number=1, ndigits=0) 10 |+round(1) 11 11 | -12 12 | round(1, ndigits=None) -13 13 | round(number=1, ndigits=None) +12 12 | round(1, None) +13 13 | round(1, ndigits=None) RUF057.py:12:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` | 10 | round(number=1, ndigits=0) 11 | -12 | round(1, ndigits=None) - | ^^^^^^^^^^^^^^^^^^^^^^ RUF057 -13 | round(number=1, ndigits=None) +12 | round(1, None) + | ^^^^^^^^^^^^^^ RUF057 +13 | round(1, ndigits=None) +14 | round(number=1, ndigits=None) | = help: Remove second argument @@ -77,150 +78,170 @@ RUF057.py:12:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` 9 9 | round(1, ndigits=0) 10 10 | round(number=1, ndigits=0) 11 11 | -12 |-round(1, ndigits=None) +12 |-round(1, None) 12 |+round(1) -13 13 | round(number=1, ndigits=None) -14 14 | -15 15 | round(1., 0) +13 13 | round(1, ndigits=None) +14 14 | round(number=1, ndigits=None) +15 15 | RUF057.py:13:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` | -12 | round(1, ndigits=None) -13 | round(number=1, ndigits=None) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF057 -14 | -15 | round(1., 0) +12 | round(1, None) +13 | round(1, ndigits=None) + | ^^^^^^^^^^^^^^^^^^^^^^ RUF057 +14 | round(number=1, ndigits=None) | = help: Remove second argument ℹ Safe fix 10 10 | round(number=1, ndigits=0) 11 11 | -12 12 | round(1, ndigits=None) -13 |-round(number=1, ndigits=None) +12 12 | round(1, None) +13 |-round(1, ndigits=None) 13 |+round(1) -14 14 | -15 15 | round(1., 0) -16 16 | round(1., ndigits=0) +14 14 | round(number=1, ndigits=None) +15 15 | +16 16 | round(1., None) -RUF057.py:19:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` +RUF057.py:14:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` | -17 | round(number=1., ndigits=0) -18 | -19 | round(1., None) - | ^^^^^^^^^^^^^^^ RUF057 -20 | round(1., ndigits=None) -21 | round(number=1., ndigits=None) +12 | round(1, None) +13 | round(1, ndigits=None) +14 | round(number=1, ndigits=None) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF057 +15 | +16 | round(1., None) | = help: Remove second argument ℹ Safe fix -16 16 | round(1., ndigits=0) -17 17 | round(number=1., ndigits=0) -18 18 | -19 |-round(1., None) - 19 |+round(1.) -20 20 | round(1., ndigits=None) -21 21 | round(number=1., ndigits=None) -22 22 | - -RUF057.py:20:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` +11 11 | +12 12 | round(1, None) +13 13 | round(1, ndigits=None) +14 |-round(number=1, ndigits=None) + 14 |+round(1) +15 15 | +16 16 | round(1., None) +17 17 | round(1., ndigits=None) + +RUF057.py:16:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` + | +14 | round(number=1, ndigits=None) +15 | +16 | round(1., None) + | ^^^^^^^^^^^^^^^ RUF057 +17 | round(1., ndigits=None) +18 | round(number=1., ndigits=None) | -19 | round(1., None) -20 | round(1., ndigits=None) + = help: Remove second argument + +ℹ Safe fix +13 13 | round(1, ndigits=None) +14 14 | round(number=1, ndigits=None) +15 15 | +16 |-round(1., None) + 16 |+round(1.) +17 17 | round(1., ndigits=None) +18 18 | round(number=1., ndigits=None) +19 19 | + +RUF057.py:17:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` + | +16 | round(1., None) +17 | round(1., ndigits=None) | ^^^^^^^^^^^^^^^^^^^^^^^ RUF057 -21 | round(number=1., ndigits=None) +18 | round(number=1., ndigits=None) | = help: Remove second argument ℹ Safe fix -17 17 | round(number=1., ndigits=0) -18 18 | -19 19 | round(1., None) -20 |-round(1., ndigits=None) - 20 |+round(1.) -21 21 | round(number=1., ndigits=None) -22 22 | -23 23 | round(foo, None) - -RUF057.py:21:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` - | -19 | round(1., None) -20 | round(1., ndigits=None) -21 | round(number=1., ndigits=None) +14 14 | round(number=1, ndigits=None) +15 15 | +16 16 | round(1., None) +17 |-round(1., ndigits=None) + 17 |+round(1.) +18 18 | round(number=1., ndigits=None) +19 19 | +20 20 | round(foo, None) + +RUF057.py:18:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` + | +16 | round(1., None) +17 | round(1., ndigits=None) +18 | round(number=1., ndigits=None) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF057 -22 | -23 | round(foo, None) +19 | +20 | round(foo, None) | = help: Remove second argument ℹ Safe fix -18 18 | -19 19 | round(1., None) -20 20 | round(1., ndigits=None) -21 |-round(number=1., ndigits=None) - 21 |+round(1.) -22 22 | -23 23 | round(foo, None) -24 24 | round(foo, ndigits=None) - -RUF057.py:23:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` - | -21 | round(number=1., ndigits=None) -22 | -23 | round(foo, None) +15 15 | +16 16 | round(1., None) +17 17 | round(1., ndigits=None) +18 |-round(number=1., ndigits=None) + 18 |+round(1.) +19 19 | +20 20 | round(foo, None) +21 21 | round(foo, ndigits=None) + +RUF057.py:20:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` + | +18 | round(number=1., ndigits=None) +19 | +20 | round(foo, None) | ^^^^^^^^^^^^^^^^ RUF057 -24 | round(foo, ndigits=None) -25 | round(number=foo, ndigits=None) +21 | round(foo, ndigits=None) +22 | round(number=foo, ndigits=None) | = help: Remove second argument ℹ Safe fix -20 20 | round(1., ndigits=None) -21 21 | round(number=1., ndigits=None) -22 22 | -23 |-round(foo, None) - 23 |+round(foo) -24 24 | round(foo, ndigits=None) -25 25 | round(number=foo, ndigits=None) -26 26 | - -RUF057.py:24:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` - | -23 | round(foo, None) -24 | round(foo, ndigits=None) +17 17 | round(1., ndigits=None) +18 18 | round(number=1., ndigits=None) +19 19 | +20 |-round(foo, None) + 20 |+round(foo) +21 21 | round(foo, ndigits=None) +22 22 | round(number=foo, ndigits=None) +23 23 | + +RUF057.py:21:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` + | +20 | round(foo, None) +21 | round(foo, ndigits=None) | ^^^^^^^^^^^^^^^^^^^^^^^^ RUF057 -25 | round(number=foo, ndigits=None) +22 | round(number=foo, ndigits=None) | = help: Remove second argument ℹ Safe fix -21 21 | round(number=1., ndigits=None) -22 22 | -23 23 | round(foo, None) -24 |-round(foo, ndigits=None) - 24 |+round(foo) -25 25 | round(number=foo, ndigits=None) -26 26 | -27 27 | ### No errors - -RUF057.py:25:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` - | -23 | round(foo, None) -24 | round(foo, ndigits=None) -25 | round(number=foo, ndigits=None) +18 18 | round(number=1., ndigits=None) +19 19 | +20 20 | round(foo, None) +21 |-round(foo, ndigits=None) + 21 |+round(foo) +22 22 | round(number=foo, ndigits=None) +23 23 | +24 24 | ### No errors + +RUF057.py:22:1: RUF057 [*] Unnecessary argument to `round`'s `ndigits` + | +20 | round(foo, None) +21 | round(foo, ndigits=None) +22 | round(number=foo, ndigits=None) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF057 -26 | -27 | ### No errors +23 | +24 | ### No errors | = help: Remove second argument ℹ Safe fix -22 22 | -23 23 | round(foo, None) -24 24 | round(foo, ndigits=None) -25 |-round(number=foo, ndigits=None) - 25 |+round(foo) -26 26 | -27 27 | ### No errors -28 28 | +19 19 | +20 20 | round(foo, None) +21 21 | round(foo, ndigits=None) +22 |-round(number=foo, ndigits=None) + 22 |+round(foo) +23 23 | +24 24 | ### No errors +25 25 |