Skip to content

Commit

Permalink
tweak logic
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Jan 21, 2024
1 parent e23bb9d commit 59ed7ba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 15 additions & 9 deletions crates/ruff_linter/src/rules/pylint/rules/potential_index_error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use ruff_python_ast::{self as ast, Expr};

use ruff_diagnostics::{Diagnostic, Violation};
Expand Down Expand Up @@ -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,
Expand All @@ -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)).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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ 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
|
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
|
Expand Down

0 comments on commit 59ed7ba

Please sign in to comment.