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 12e059a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 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)
25 changes: 10 additions & 15 deletions crates/ruff_linter/src/rules/pylint/rules/potential_index_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,21 @@ 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())
elts.len() as i64
}
_ => {
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 +55,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 12e059a

Please sign in to comment.