Skip to content

Commit

Permalink
[ruff] Detect more strict-integer expressions (RUF046)
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Dec 20, 2024
1 parent d3f51cf commit 11aa3c1
Show file tree
Hide file tree
Showing 3 changed files with 691 additions and 159 deletions.
86 changes: 86 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF046.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@
int(round(1.))
int(round(1., None))

int(1)
int(v := 1)
int(~1)
int(-1)
int(+1)

int(1 + 1)
int(1 - 1)
int(1 * 1)
int(1 % 1)
int(1 ** 1)
int(1 << 1)
int(1 >> 1)
int(1 | 1)
int(1 ^ 1)
int(1 & 1)
int(1 // 1)

int(1 if ... else 2)

int(1 and 0)
int(0 or -1)


if int(1 + 2) * 3:
...


### Unsafe

Expand Down Expand Up @@ -68,3 +95,62 @@

int(round(0, 0), base)
int(round(0, 0, extra=keyword))

int(foo if ... else 4)

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)
int(foo < 1)
int(foo <= 1)
int(foo > 1)
int(foo >= 1)

int(v := {}[{}['']])

int(foo + 1)
int(foo - 1)
int(foo * 1)
int(foo @ 1)
int(foo / 1)
int(foo % 1)
int(foo ** 1)
int(foo << 1)
int(foo >> 1)
int(foo | 1)
int(foo ^ 1)
int(foo & 1)
int(foo // 1)

int(v := 3.7)

int(not 109)

int(1 / 1)
int(1 @ 1)

int(1. if ... else .2)
39 changes: 37 additions & 2 deletions crates/ruff_linter/src/rules/ruff/rules/unnecessary_cast_to_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::checkers::ast::Checker;
use ruff_diagnostics::{AlwaysFixableViolation, Applicability, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::{Arguments, Expr, ExprCall, ExprNumberLiteral, Number};
use ruff_python_semantic::analyze::type_inference::{NumberLike, PythonType, ResolvedPythonType};
use ruff_python_semantic::analyze::typing;
use ruff_python_semantic::SemanticModel;
use ruff_text_size::TextRange;
use ruff_text_size::{Ranged, TextRange};

/// ## What it does
/// Checks for `int` conversions of values that are already integers.
Expand Down Expand Up @@ -54,10 +55,44 @@ impl AlwaysFixableViolation for UnnecessaryCastToInt {
pub(crate) fn unnecessary_cast_to_int(checker: &mut Checker, call: &ExprCall) {
let semantic = checker.semantic();

let Some(Expr::Call(inner_call)) = single_argument_to_int_call(semantic, call) else {
let Some(argument) = single_argument_to_int_call(semantic, call) else {
return;
};

if matches!(
ResolvedPythonType::from(argument),
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer))
) {
simplify_expression(checker, call, argument);
return;
}

if let Expr::Call(inner_call) = argument {
simplify_call(checker, call, inner_call);
}
}

fn simplify_expression(checker: &mut Checker, call: &ExprCall, argument: &Expr) {
let (locator, semantic) = (checker.locator(), checker.semantic());

let argument_expr = locator.slice(argument.range());

let has_parent_expr = semantic.current_expression_parent().is_some();
let new_content = if has_parent_expr || argument.is_named_expr() {
format!("({argument_expr})")
} else {
argument_expr.to_string()
};

let edit = Edit::range_replacement(new_content, call.range);
let fix = Fix::safe_edit(edit);

let diagnostic = Diagnostic::new(UnnecessaryCastToInt, call.range);

checker.diagnostics.push(diagnostic.with_fix(fix));
}

fn simplify_call(checker: &mut Checker, call: &ExprCall, inner_call: &ExprCall) {
let (func, arguments) = (&inner_call.func, &inner_call.arguments);
let (outer_range, inner_range) = (call.range, inner_call.range);

Expand Down
Loading

0 comments on commit 11aa3c1

Please sign in to comment.