-
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.
- Loading branch information
1 parent
2ce5fc2
commit 866b5bc
Showing
10 changed files
with
83 additions
and
69 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
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
46 changes: 0 additions & 46 deletions
46
crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs
This file was deleted.
Oops, something went wrong.
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
23 changes: 23 additions & 0 deletions
23
...inter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new
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,23 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
assertion_line: 52 | ||
--- | ||
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
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