-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
pylint
] - add potential-index-error
rule (PLE0643
)
- Loading branch information
1 parent
2b60552
commit d411cd4
Showing
8 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
crates/ruff_linter/resources/test/fixtures/pylint/potential_index_error.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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][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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
crates/ruff_linter/src/rules/pylint/rules/potential_index_error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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; | ||
|
||
/// ## What it does | ||
/// Checks for potential hard-coded IndexErrors, which occurs when accessing | ||
/// a list or tuple with an index that is known to be out of bounds. | ||
/// | ||
/// ## Why is this bad? | ||
/// This will cause a runtime error. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// print([1, 2, 3][123]) | ||
/// ``` | ||
/// | ||
#[violation] | ||
pub struct PotentialIndexError; | ||
|
||
impl Violation for PotentialIndexError { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Potential IndexError") | ||
} | ||
} | ||
|
||
/// PLE0643 | ||
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()) | ||
} | ||
_ => { | ||
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), | ||
Expr::UnaryOp(ast::ExprUnaryOp { | ||
op: ast::UnaryOp::USub, | ||
operand, | ||
range, | ||
}) => match operand.as_ref() { | ||
Expr::NumberLiteral(ast::ExprNumberLiteral { | ||
value: ast::Number::Int(number_value), | ||
.. | ||
}) => match number_value.as_i32() { | ||
Some(value) => (Some(-value), *range), | ||
None => (None, TextRange::default()), | ||
}, | ||
_ => (None, TextRange::default()), | ||
}, | ||
_ => (None, TextRange::default()), | ||
}; | ||
|
||
let Some(number_value) = number_value else { | ||
return; | ||
}; | ||
|
||
if number_value >= length || number_value < -length { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(PotentialIndexError, range)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0643_potential_index_error.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
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 | ||
| | ||
|
||
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 | ||
| | ||
|
||
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 | ||
5 | | ||
6 | print([1, 2, 3][2]) # OK | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.