-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule: Prevent assignment expressions in assert statements (#7856)
- Loading branch information
Showing
9 changed files
with
93 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,7 @@ | ||
# RUF018 | ||
assert (x := 0) == 0 | ||
assert x, (y := "error") | ||
|
||
# OK | ||
if z := 0: | ||
pass |
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
53 changes: 53 additions & 0 deletions
53
crates/ruff_linter/src/rules/ruff/rules/assignment_in_assert.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,53 @@ | ||
use ruff_python_ast::Expr; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for named assignment expressions (e.g., `x := 0`) in `assert` | ||
/// statements. | ||
/// | ||
/// ## Why is this bad? | ||
/// Named assignment expressions (also known as "walrus operators") are used to | ||
/// assign a value to a variable as part of a larger expression. | ||
/// | ||
/// Named assignments are syntactically valid in `assert` statements. However, | ||
/// when the Python interpreter is run under the `-O` flag, `assert` statements | ||
/// are not executed. In this case, the named assignment will also be ignored, | ||
/// which may result in unexpected behavior (e.g., undefined variable | ||
/// accesses). | ||
/// | ||
/// ## Examples | ||
/// ```python | ||
/// assert (x := 0) == 0 | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// x = 0 | ||
/// assert x == 0 | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `-O`](https://docs.python.org/3/using/cmdline.html#cmdoption-O) | ||
#[violation] | ||
pub struct AssignmentInAssert; | ||
|
||
impl Violation for AssignmentInAssert { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Avoid assignment expressions in `assert` statements") | ||
} | ||
} | ||
|
||
/// RUF018 | ||
pub(crate) fn assignment_in_assert(checker: &mut Checker, value: &Expr) { | ||
if checker.semantic().current_statement().is_assert_stmt() { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(AssignmentInAssert, value.range())); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
...ff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.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,22 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
--- | ||
RUF018.py:2:9: RUF018 Avoid assignment expressions in `assert` statements | ||
| | ||
1 | # RUF018 | ||
2 | assert (x := 0) == 0 | ||
| ^^^^^^ RUF018 | ||
3 | assert x, (y := "error") | ||
| | ||
|
||
RUF018.py:3:12: RUF018 Avoid assignment expressions in `assert` statements | ||
| | ||
1 | # RUF018 | ||
2 | assert (x := 0) == 0 | ||
3 | assert x, (y := "error") | ||
| ^^^^^^^^^^^^ RUF018 | ||
4 | | ||
5 | # OK | ||
| | ||
|
||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.