Skip to content

Commit

Permalink
Tweaks to name, message
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 9, 2023
1 parent 2ce5fc2 commit 866b5bc
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 69 deletions.
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,8 +1414,8 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
}
}
Expr::NamedExpr(..) => {
if checker.enabled(Rule::AssignInAssert) {
ruff::rules::assign_in_assert(checker, expr);
if checker.enabled(Rule::AssignmentInAssert) {
ruff::rules::assignment_in_assert(checker, expr);
}
}
_ => {}
Expand Down
3 changes: 0 additions & 3 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,10 @@ where
msg,
range: _,
}) => {
let snapshot = self.semantic.flags;
self.semantic.flags |= SemanticModelFlags::ASSERT;
self.visit_boolean_test(test);
if let Some(expr) = msg {
self.visit_expr(expr);
}
self.semantic.flags = snapshot;
}
Stmt::While(ast::StmtWhile {
test,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Ruff, "016") => (RuleGroup::Unspecified, rules::ruff::rules::InvalidIndexType),
#[allow(deprecated)]
(Ruff, "017") => (RuleGroup::Nursery, rules::ruff::rules::QuadraticListSummation),
(Ruff, "018") => (RuleGroup::Preview, rules::ruff::rules::AssignInAssert),
(Ruff, "018") => (RuleGroup::Preview, rules::ruff::rules::AssignmentInAssert),
(Ruff, "100") => (RuleGroup::Unspecified, rules::ruff::rules::UnusedNOQA),
(Ruff, "200") => (RuleGroup::Unspecified, rules::ruff::rules::InvalidPyprojectToml),

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod tests {
)]
#[test_case(Rule::QuadraticListSummation, Path::new("RUF017_1.py"))]
#[test_case(Rule::QuadraticListSummation, Path::new("RUF017_0.py"))]
#[test_case(Rule::AssignInAssert, Path::new("RUF018.py"))]
#[test_case(Rule::AssignmentInAssert, Path::new("RUF018.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(
Expand Down
46 changes: 0 additions & 46 deletions crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs

This file was deleted.

53 changes: 53 additions & 0 deletions crates/ruff_linter/src/rules/ruff/rules/assignment_in_assert.rs
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()));
}
}
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/ruff/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(crate) use ambiguous_unicode_character::*;
pub(crate) use assign_in_assert::*;
pub(crate) use assignment_in_assert::*;
pub(crate) use asyncio_dangling_task::*;
pub(crate) use collection_literal_concatenation::*;
pub(crate) use explicit_f_string_type_conversion::*;
Expand All @@ -17,7 +17,7 @@ pub(crate) use unreachable::*;
pub(crate) use unused_noqa::*;

mod ambiguous_unicode_character;
mod assign_in_assert;
mod assignment_in_assert;
mod asyncio_dangling_task;
mod collection_literal_concatenation;
mod confusables;
Expand Down
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
|


13 changes: 0 additions & 13 deletions crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,11 +1330,6 @@ impl<'a> SemanticModel<'a> {
self.flags.intersects(SemanticModelFlags::SUBSCRIPT)
}

/// Return `true` if the model is in an assert statement.
pub const fn in_assert(&self) -> bool {
self.flags.intersects(SemanticModelFlags::ASSERT)
}

/// Return `true` if the model is in a type-checking block.
pub const fn in_type_checking_block(&self) -> bool {
self.flags
Expand Down Expand Up @@ -1605,14 +1600,6 @@ bitflags! {
/// ```
const FUTURE_ANNOTATIONS = 1 << 14;

/// The model is in an assert statement.
///
/// For example, the model could be visiting `x` in:
/// ```python
/// assert x
/// ```
const ASSERT = 1 << 15;

/// The context is in any type annotation.
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_ANNOTATION.bits();

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ mod tests {
Rule::TooManyPublicMethods,
Rule::UndocumentedWarn,
Rule::UnnecessaryEnumerate,
Rule::AssignInAssert,
Rule::AssignmentInAssert,
];

#[allow(clippy::needless_pass_by_value)]
Expand Down

0 comments on commit 866b5bc

Please sign in to comment.