-
Notifications
You must be signed in to change notification settings - Fork 895
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
Showing
10 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
crates/ruff/resources/test/fixtures/pylint/continue_in_finally.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,95 @@ | ||
while True: | ||
try: | ||
pass | ||
finally: | ||
continue # [continue-in-finally] | ||
|
||
while True: | ||
try: | ||
pass | ||
except Exception: | ||
continue | ||
finally: | ||
try: | ||
pass | ||
finally: | ||
continue # [continue-in-finally] | ||
pass | ||
|
||
while True: | ||
try: | ||
pass | ||
finally: | ||
test = "aa" | ||
match test: | ||
case "aa": | ||
continue # [continue-in-finally] | ||
|
||
while True: | ||
try: | ||
pass | ||
finally: | ||
with "aa" as f: | ||
continue # [continue-in-finally] | ||
|
||
while True: | ||
try: | ||
pass | ||
finally: | ||
if True: | ||
continue # [continue-in-finally] | ||
continue # [continue-in-finally] | ||
|
||
def test(): | ||
while True: | ||
continue | ||
try: | ||
pass | ||
finally: | ||
continue # [continue-in-finally] | ||
|
||
|
||
while True: | ||
try: | ||
pass | ||
finally: | ||
continue # [continue-in-finally] | ||
|
||
def test(): | ||
while True: | ||
continue | ||
|
||
|
||
while True: | ||
try: | ||
pass | ||
finally: | ||
for i in range(12): | ||
continue | ||
continue # [continue-in-finally] | ||
|
||
while True: | ||
pass | ||
else: | ||
continue # [continue-in-finally] | ||
|
||
def test(): | ||
continue | ||
while True: | ||
continue | ||
|
||
|
||
while True: | ||
try: | ||
pass | ||
finally: | ||
if True: | ||
pass | ||
elif False: | ||
continue # [continue-in-finally] | ||
else: | ||
continue # [continue-in-finally] | ||
for i in range(10): | ||
pass | ||
else: | ||
continue # [continue-in-finally] |
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
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,80 @@ | ||
use rustpython_parser::ast::{Stmt, StmtKind}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::types::Range; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `continue` statements inside `finally` | ||
/// | ||
/// ## Why is this bad? | ||
/// `continue` statements were not allowed within `finally` clauses prior to | ||
/// Python 3.8. Using a `continue` statement within a `finally` clause can | ||
/// cause a `SyntaxError`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// while True: | ||
/// try: | ||
/// pass | ||
/// finally: | ||
/// continue | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// while True: | ||
/// try: | ||
/// pass | ||
/// except Exception: | ||
/// pass | ||
/// else: | ||
/// continue | ||
/// ``` | ||
#[violation] | ||
pub struct ContinueInFinally; | ||
|
||
impl Violation for ContinueInFinally { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`continue` not supported inside `finally` clause") | ||
} | ||
} | ||
|
||
fn traverse_body(checker: &mut Checker, body: &[Stmt]) { | ||
for stmt in body { | ||
if matches!(stmt.node, StmtKind::Continue { .. }) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(ContinueInFinally, Range::from(stmt))); | ||
} | ||
|
||
match &stmt.node { | ||
StmtKind::If { body, orelse, .. } | ||
| StmtKind::Try { body, orelse, .. } | ||
| StmtKind::TryStar { body, orelse, .. } => { | ||
traverse_body(checker, body); | ||
traverse_body(checker, orelse); | ||
} | ||
StmtKind::For { orelse, .. } | ||
| StmtKind::AsyncFor { orelse, .. } | ||
| StmtKind::While { orelse, .. } => traverse_body(checker, orelse), | ||
StmtKind::With { body, .. } | StmtKind::AsyncWith { body, .. } => { | ||
traverse_body(checker, body); | ||
} | ||
StmtKind::Match { cases, .. } => { | ||
for case in cases { | ||
traverse_body(checker, &case.body); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
/// PLE0116 | ||
pub fn continue_in_finally(checker: &mut Checker, body: &[Stmt]) { | ||
traverse_body(checker, body); | ||
} |
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
6 changes: 6 additions & 0 deletions
6
...rc/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0116_continue_in_finally.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,6 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
[] | ||
|
Oops, something went wrong.