Skip to content

Commit

Permalink
Revert
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Dec 4, 2024
1 parent 4e98b97 commit cd3e3eb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
32 changes: 22 additions & 10 deletions crates/ruff_linter/src/rules/ruff/rules/unnecessary_cast_to_int.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::checkers::ast::Checker;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_diagnostics::{AlwaysFixableViolation, Applicability, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::{Arguments, Expr, ExprCall, ExprName, ExprNumberLiteral, Number};
use ruff_python_semantic::analyze::typing;
Expand All @@ -13,9 +13,12 @@ use ruff_text_size::TextRange;
/// Such a conversion is unnecessary.
///
/// ## Known problems
/// When values incorrectly override the `__round__`, `__ceil__`, `__floor__`,
/// or `__trunc__` operators such that they don't return an integer,
/// this rule may produce false positives.
/// This rule is prone to false positives due to type inference limitations.
///
/// It assumes that `round`, `math.ceil`, `math.floor`, `math.trunc`
/// always return `int`, which might not be the case for objects
/// with the corresponding dunder methods overridden.
/// In such cases, the fix is marked as unsafe.
///
/// ## Example
///
Expand Down Expand Up @@ -59,24 +62,33 @@ pub(crate) fn unnecessary_cast_to_int(checker: &mut Checker, call: &ExprCall) {
return;
};

let edit = match qualified_name.segments() {
let (edit, applicability) = match qualified_name.segments() {
// Always returns a strict instance of `int`
["" | "builtins", "len" | "id" | "hash" | "ord" | "int"]
| ["math", "comb" | "factorial" | "gcd" | "lcm" | "isqrt" | "perm" | "ceil" | "floor" | "trunc"] => {
replace_with_inner(checker, outer_range, inner_range)
}
| ["math", "comb" | "factorial" | "gcd" | "lcm" | "isqrt" | "perm"] => (
replace_with_inner(checker, outer_range, inner_range),
Applicability::Safe,
),

// Depends on `ndigits` and `number.__round__`
["" | "builtins", "round"] => {
match replace_with_shortened_round_call(checker, outer_range, arguments) {
None => return,
Some(edit) => edit,
Some(edit) => (edit, Applicability::Unsafe),
}
}

// Depends on `__ceil__`/`__floor__`/`__trunc__`
["math", "ceil" | "floor" | "trunc"] => (
replace_with_inner(checker, outer_range, inner_range),
Applicability::Unsafe,
),

_ => return,
};

let diagnostic = Diagnostic::new(UnnecessaryCastToInt, call.range);
let fix = Fix::safe_edit(edit);
let fix = Fix::applicable_edit(edit, applicability);

checker.diagnostics.push(diagnostic.with_fix(fix));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ RUF046.py:23:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
20 20 |
21 21 | ### Unsafe
22 22 |
Expand All @@ -257,7 +257,7 @@ RUF046.py:24:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
21 21 | ### Unsafe
22 22 |
23 23 | int(math.ceil())
Expand All @@ -276,7 +276,7 @@ RUF046.py:25:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
22 22 |
23 23 | int(math.ceil())
24 24 | int(math.floor())
Expand All @@ -296,7 +296,7 @@ RUF046.py:31:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
28 28 | ### `round()`
29 29 |
30 30 | ## Errors
Expand All @@ -316,7 +316,7 @@ RUF046.py:32:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
29 29 |
30 30 | ## Errors
31 31 | int(round(0))
Expand All @@ -337,7 +337,7 @@ RUF046.py:33:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
30 30 | ## Errors
31 31 | int(round(0))
32 32 | int(round(0, 0))
Expand All @@ -357,7 +357,7 @@ RUF046.py:35:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
32 32 | int(round(0, 0))
33 33 | int(round(0, None))
34 34 |
Expand All @@ -377,7 +377,7 @@ RUF046.py:36:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
33 33 | int(round(0, None))
34 34 |
35 35 | int(round(0.1))
Expand All @@ -398,7 +398,7 @@ RUF046.py:41:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
38 38 | # Argument type is not checked
39 39 | foo = type("Foo", (), {"__round__": lambda self: 4.2})()
40 40 |
Expand All @@ -419,7 +419,7 @@ RUF046.py:43:1: RUF046 [*] Value being casted is already an integer
|
= help: Remove `int()` wrapper call

Safe fix
Unsafe fix
40 40 |
41 41 | int(round(foo))
42 42 | int(round(foo, 0))
Expand Down

0 comments on commit cd3e3eb

Please sign in to comment.