-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b9b682
commit 3f76ac8
Showing
8 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
for i in range(10): | ||
try: | ||
print(f"{i}") # PERF203 | ||
except: | ||
print("error") | ||
|
||
try: | ||
for i in range(10): | ||
print(f"{i}") | ||
except: | ||
print("error") |
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
49 changes: 49 additions & 0 deletions
49
crates/ruff/src/rules/perflint/rules/loop_try_except_usage.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,49 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::prelude::{Stmt, StmtTry}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks to see if a for loop contains a try/except block. | ||
/// | ||
/// ## Why is this bad? | ||
/// Try/except blocks can be computationally expensive, especially prior to Python 3.10. | ||
/// Instead, you should refactor your code to put the entire loop into the `try` block. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// for _ in range(10): | ||
/// try: | ||
/// print("something") | ||
/// except: | ||
/// print("error") | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// try: | ||
/// for _ in range(10): | ||
/// print("something") | ||
/// except: | ||
/// print("error") | ||
/// ``` | ||
|
||
#[violation] | ||
pub(crate) struct LoopTryExceptUsage; | ||
|
||
impl Violation for LoopTryExceptUsage { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Try..except blocks have a significant overhead. Avoid using them inside a loop.") | ||
} | ||
} | ||
|
||
pub(crate) fn loop_try_except_usage(checker: &mut Checker, body: &[Stmt]) { | ||
body.iter() | ||
.filter_map(|el| match el { | ||
Stmt::Try(StmtTry { range, .. }) => Some(Diagnostic::new(LoopTryExceptUsage, *range)), | ||
_ => None, | ||
}) | ||
.for_each(|diagnostic| checker.diagnostics.push(diagnostic)); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub(crate) use incorrect_dict_iterator::{incorrect_dict_iterator, IncorrectDictIterator}; | ||
pub(crate) use loop_try_except_usage::{loop_try_except_usage, LoopTryExceptUsage}; | ||
|
||
mod incorrect_dict_iterator; | ||
mod loop_try_except_usage; |
17 changes: 17 additions & 0 deletions
17
...s/ruff/src/rules/perflint/snapshots/ruff__rules__perflint__tests__PERF203_PERF203.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,17 @@ | ||
--- | ||
source: crates/ruff/src/rules/perflint/mod.rs | ||
--- | ||
PERF203.py:2:5: PERF203 Try..except blocks have a significant overhead. Avoid using them inside a loop. | ||
| | ||
1 | for i in range(10): | ||
2 | try: | ||
| _____^ | ||
3 | | print(f"{i}") # PERF203 | ||
4 | | except: | ||
5 | | print("error") | ||
| |______________________^ PERF203 | ||
6 | | ||
7 | try: | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.