diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/potential_index_error.py b/crates/ruff_linter/resources/test/fixtures/pylint/potential_index_error.py index a80dc7a6be878a..7f1a0b47264d51 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/potential_index_error.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/potential_index_error.py @@ -1,11 +1,9 @@ print([1, 2, 3][3]) # PLE0643 print([1, 2, 3][-4]) # PLE0643 -print([1, 2, 3][9223372036854775807]) # PLE0643 -print([1, 2, 3][-9223372036854775807]) # PLE0643 +print([1, 2, 3][999999999999999999999999999999999999999999]) # PLE0643 +print([1, 2, 3][-999999999999999999999999999999999999999999]) # PLE0643 print([1, 2, 3][2]) # OK print([1, 2, 3][0]) # OK print([1, 2, 3][-3]) # OK print([1, 2, 3][3:]) # OK -print([1, 2, 3][-9223372036854775808]) # OK (i64 overflow, ignored) -print([1, 2, 3][9223372036854775808]) # OK (i64 overflow, ignored) diff --git a/crates/ruff_linter/src/rules/pylint/rules/potential_index_error.rs b/crates/ruff_linter/src/rules/pylint/rules/potential_index_error.rs index c4b71ab044a287..e0f48b779035c5 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/potential_index_error.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/potential_index_error.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use ruff_python_ast::{self as ast, Expr}; use ruff_diagnostics::{Diagnostic, Violation}; @@ -45,10 +47,7 @@ pub(crate) fn potential_index_error(checker: &mut Checker, value: &Expr, slice: Expr::NumberLiteral(ast::ExprNumberLiteral { value: ast::Number::Int(number_value), range, - }) => match number_value.as_i64() { - Some(value) => (value, *range), - None => return, - }, + }) => (number_value.to_owned(), *range), Expr::UnaryOp(ast::ExprUnaryOp { op: ast::UnaryOp::USub, operand, @@ -57,16 +56,23 @@ pub(crate) fn potential_index_error(checker: &mut Checker, value: &Expr, slice: Expr::NumberLiteral(ast::ExprNumberLiteral { value: ast::Number::Int(number_value), .. - }) => match number_value.as_i64() { - Some(value) => (-value, *range), - None => return, - }, + }) => ( + ast::Int::from_str(&format!("-{}", number_value.to_string())).unwrap(), + *range, + ), _ => return, }, _ => return, }; - if number_value >= length || number_value < -length { + let emit = if let Some(number) = number_value.as_i64() { + number >= length || number < -length + } else { + // this should be impossible + true + }; + + if emit { checker .diagnostics .push(Diagnostic::new(PotentialIndexError, range)); diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0643_potential_index_error.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0643_potential_index_error.py.snap index ce3b518f4e4a4b..2cfa565dc5eb80 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0643_potential_index_error.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0643_potential_index_error.py.snap @@ -6,7 +6,7 @@ potential_index_error.py:1:17: PLE0643 Potential IndexError 1 | print([1, 2, 3][3]) # PLE0643 | ^ PLE0643 2 | print([1, 2, 3][-4]) # PLE0643 -3 | print([1, 2, 3][9223372036854775807]) # PLE0643 +3 | print([1, 2, 3][999999999999999999999999999999999999999999]) # PLE0643 | potential_index_error.py:2:17: PLE0643 Potential IndexError @@ -14,25 +14,25 @@ potential_index_error.py:2:17: PLE0643 Potential IndexError 1 | print([1, 2, 3][3]) # PLE0643 2 | print([1, 2, 3][-4]) # PLE0643 | ^^ PLE0643 -3 | print([1, 2, 3][9223372036854775807]) # PLE0643 -4 | print([1, 2, 3][-9223372036854775807]) # PLE0643 +3 | print([1, 2, 3][999999999999999999999999999999999999999999]) # PLE0643 +4 | print([1, 2, 3][-999999999999999999999999999999999999999999]) # PLE0643 | potential_index_error.py:3:17: PLE0643 Potential IndexError | 1 | print([1, 2, 3][3]) # PLE0643 2 | print([1, 2, 3][-4]) # PLE0643 -3 | print([1, 2, 3][9223372036854775807]) # PLE0643 - | ^^^^^^^^^^^^^^^^^^^ PLE0643 -4 | print([1, 2, 3][-9223372036854775807]) # PLE0643 +3 | print([1, 2, 3][999999999999999999999999999999999999999999]) # PLE0643 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLE0643 +4 | print([1, 2, 3][-999999999999999999999999999999999999999999]) # PLE0643 | potential_index_error.py:4:17: PLE0643 Potential IndexError | 2 | print([1, 2, 3][-4]) # PLE0643 -3 | print([1, 2, 3][9223372036854775807]) # PLE0643 -4 | print([1, 2, 3][-9223372036854775807]) # PLE0643 - | ^^^^^^^^^^^^^^^^^^^^ PLE0643 +3 | print([1, 2, 3][999999999999999999999999999999999999999999]) # PLE0643 +4 | print([1, 2, 3][-999999999999999999999999999999999999999999]) # PLE0643 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLE0643 5 | 6 | print([1, 2, 3][2]) # OK |