Skip to content

Commit

Permalink
tweak and add i64 capability
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Jan 19, 2024
1 parent 652603a commit 6ba0c85
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
print([1, 2, 3][3]) # PLE0643
print([1, 2, 3][-4]) # PLE0643
print([1, 2, 3][2147483647]) # PLE0643
print([1, 2, 3][-2147483647]) # PLE0643
print([1, 2, 3][9223372036854775807]) # PLE0643
print([1, 2, 3][-9223372036854775807]) # 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][2147483648]) # OK (i32 overflow, ignored)
print([1, 2, 3][-2147483648]) # OK (i32 overflow, ignored)
print([1, 2, 3][-9223372036854775808]) # OK (i64 overflow, ignored)
print([1, 2, 3][9223372036854775808]) # OK (i64 overflow, ignored)
29 changes: 13 additions & 16 deletions crates/ruff_linter/src/rules/pylint/rules/potential_index_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use ruff_python_ast::{self as ast, Expr};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_text_size::TextRange;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -32,22 +31,24 @@ impl Violation for PotentialIndexError {
pub(crate) fn potential_index_error(checker: &mut Checker, value: &Expr, slice: &Expr) {
let length = match value {
Expr::Tuple(ast::ExprTuple { elts, .. }) | Expr::List(ast::ExprList { elts, .. }) => {
i32::try_from(elts.len())
match i64::try_from(elts.len()) {
Ok(length) => length,
Err(_) => return,
}
}
_ => {
return;
}
};

let Ok(length) = length else {
return;
};

let (number_value, range) = match slice {
Expr::NumberLiteral(ast::ExprNumberLiteral {
value: ast::Number::Int(number_value),
range,
}) => (number_value.as_i32(), *range),
}) => match number_value.as_i64() {
Some(value) => (-value, *range),
None => return,
},
Expr::UnaryOp(ast::ExprUnaryOp {
op: ast::UnaryOp::USub,
operand,
Expand All @@ -56,17 +57,13 @@ 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_i32() {
Some(value) => (Some(-value), *range),
None => (None, TextRange::default()),
}) => match number_value.as_i64() {
Some(value) => (-value, *range),
None => return,
},
_ => (None, TextRange::default()),
_ => return,
},
_ => (None, TextRange::default()),
};

let Some(number_value) = number_value else {
return;
_ => return,
};

if number_value >= length || number_value < -length {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
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][2147483647]) # PLE0643
|

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][2147483647]) # PLE0643
4 | print([1, 2, 3][-2147483647]) # PLE0643
3 | print([1, 2, 3][9223372036854775807]) # PLE0643
4 | print([1, 2, 3][-9223372036854775807]) # 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][2147483647]) # PLE0643
| ^^^^^^^^^^ PLE0643
4 | print([1, 2, 3][-2147483647]) # PLE0643
3 | print([1, 2, 3][9223372036854775807]) # PLE0643
| ^^^^^^^^^^^^^^^^^^^ PLE0643
4 | print([1, 2, 3][-9223372036854775807]) # PLE0643
|

potential_index_error.py:4:17: PLE0643 Potential IndexError
|
2 | print([1, 2, 3][-4]) # PLE0643
3 | print([1, 2, 3][2147483647]) # PLE0643
4 | print([1, 2, 3][-2147483647]) # PLE0643
| ^^^^^^^^^^^ PLE0643
3 | print([1, 2, 3][9223372036854775807]) # PLE0643
4 | print([1, 2, 3][-9223372036854775807]) # PLE0643
| ^^^^^^^^^^^^^^^^^^^^ PLE0643
5 |
6 | print([1, 2, 3][2]) # OK
|
Expand Down

0 comments on commit 6ba0c85

Please sign in to comment.