From 31388bc25ade7424f1780a19fe4a05403a930e64 Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 9 Oct 2023 14:28:05 +0900 Subject: [PATCH 1/5] assign-in-assert --- .../resources/test/fixtures/ruff/RUF018.py | 7 +++ .../src/checkers/ast/analyze/expression.rs | 5 ++ crates/ruff_linter/src/checkers/ast/mod.rs | 3 ++ crates/ruff_linter/src/codes.rs | 1 + crates/ruff_linter/src/rules/ruff/mod.rs | 1 + .../src/rules/ruff/rules/assign_in_assert.rs | 46 +++++++++++++++++++ .../ruff_linter/src/rules/ruff/rules/mod.rs | 2 + ..._rules__ruff__tests__RUF018_RUF018.py.snap | 22 +++++++++ crates/ruff_python_semantic/src/model.rs | 13 ++++++ crates/ruff_workspace/src/configuration.rs | 1 + ruff.schema.json | 1 + 11 files changed, 102 insertions(+) create mode 100644 crates/ruff_linter/resources/test/fixtures/ruff/RUF018.py create mode 100644 crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs create mode 100644 crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF018.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF018.py new file mode 100644 index 0000000000000..f7d4e24c6ea3b --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF018.py @@ -0,0 +1,7 @@ +# RUF018 +assert (x := 0) == 0 +assert x, (y := "error") + +# OK +if z := 0: + pass diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index 948bbb6f648af..3c0d38ad6495e 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -1413,6 +1413,11 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) { pylint::rules::repeated_equality_comparison(checker, bool_op); } } + Expr::NamedExpr(..) => { + if checker.enabled(Rule::AssignInAssert) { + ruff::rules::assign_in_assert(checker, expr); + } + } _ => {} }; } diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index dbfa15f65655a..66b56e78918f9 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -690,10 +690,13 @@ 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, diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index 968a46f221a57..49b9a1d677046 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -865,6 +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, "100") => (RuleGroup::Unspecified, rules::ruff::rules::UnusedNOQA), (Ruff, "200") => (RuleGroup::Unspecified, rules::ruff::rules::InvalidPyprojectToml), diff --git a/crates/ruff_linter/src/rules/ruff/mod.rs b/crates/ruff_linter/src/rules/ruff/mod.rs index cdda430bdefaf..8eb8077548d8d 100644 --- a/crates/ruff_linter/src/rules/ruff/mod.rs +++ b/crates/ruff_linter/src/rules/ruff/mod.rs @@ -42,6 +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"))] fn rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy()); let diagnostics = test_path( diff --git a/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs b/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs new file mode 100644 index 0000000000000..645f1553dbdb3 --- /dev/null +++ b/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs @@ -0,0 +1,46 @@ +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 assert statements containing assignment expressions. +/// +/// ## Why is this bad? +/// Assignment expression in assert statements get optimized away when running +/// Python with the `-O` option. +/// +/// ## Examples +/// ```python +/// assert (x := 0) == 0 +/// ``` +/// +/// Use instead: +/// ```python +/// x = 0 +/// assert x == 0 +/// ``` +/// +/// ## References +/// - [Python documentation: command option `-O`](https://docs.python.org/3/using/cmdline.html#cmdoption-O) +#[violation] +pub struct AssignInAssert; + +impl Violation for AssignInAssert { + #[derive_message_formats] + fn message(&self) -> String { + format!("Assignment expression in assert statement is not allowed") + } +} + +/// RUF018 +pub(crate) fn assign_in_assert(checker: &mut Checker, value: &Expr) { + if checker.semantic().in_assert() { + checker + .diagnostics + .push(Diagnostic::new(AssignInAssert, value.range())); + } +} diff --git a/crates/ruff_linter/src/rules/ruff/rules/mod.rs b/crates/ruff_linter/src/rules/ruff/rules/mod.rs index 8ffa324791c30..6404c01ccea09 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/mod.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/mod.rs @@ -1,4 +1,5 @@ pub(crate) use ambiguous_unicode_character::*; +pub(crate) use assign_in_assert::*; pub(crate) use asyncio_dangling_task::*; pub(crate) use collection_literal_concatenation::*; pub(crate) use explicit_f_string_type_conversion::*; @@ -16,6 +17,7 @@ pub(crate) use unreachable::*; pub(crate) use unused_noqa::*; mod ambiguous_unicode_character; +mod assign_in_assert; mod asyncio_dangling_task; mod collection_literal_concatenation; mod confusables; diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap new file mode 100644 index 0000000000000..d8c6fc14277d9 --- /dev/null +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap @@ -0,0 +1,22 @@ +--- +source: crates/ruff_linter/src/rules/ruff/mod.rs +--- +RUF018.py:2:9: RUF018 Assignment expression in assert statement is not allowed + | +1 | # RUF018 +2 | assert (x := 0) == 0 + | ^^^^^^ RUF018 +3 | assert x, (y := "") + | + +RUF018.py:3:12: RUF018 Assignment expression in assert statement is not allowed + | +1 | # RUF018 +2 | assert (x := 0) == 0 +3 | assert x, (y := "") + | ^^^^^^^ RUF018 +4 | +5 | # OK + | + + diff --git a/crates/ruff_python_semantic/src/model.rs b/crates/ruff_python_semantic/src/model.rs index 8f742ae452ada..799c80f4e64ad 100644 --- a/crates/ruff_python_semantic/src/model.rs +++ b/crates/ruff_python_semantic/src/model.rs @@ -1330,6 +1330,11 @@ 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 @@ -1600,6 +1605,14 @@ 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(); diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index d480ec3e73c57..2dddf54886f59 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -1047,6 +1047,7 @@ mod tests { Rule::TooManyPublicMethods, Rule::UndocumentedWarn, Rule::UnnecessaryEnumerate, + Rule::AssignInAssert, ]; #[allow(clippy::needless_pass_by_value)] diff --git a/ruff.schema.json b/ruff.schema.json index 0149b046ee775..cd341eb9f1efb 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -3145,6 +3145,7 @@ "RUF015", "RUF016", "RUF017", + "RUF018", "RUF1", "RUF10", "RUF100", From 9357316fbdfcdcc1ec2b6f3e3a8b5782ed35f08d Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 9 Oct 2023 14:38:51 +0900 Subject: [PATCH 2/5] Fix doc --- crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs b/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs index 645f1553dbdb3..55e714732270c 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs @@ -7,11 +7,11 @@ use ruff_text_size::Ranged; use crate::checkers::ast::Checker; /// ## What it does -/// Checks for assert statements containing assignment expressions. +/// Checks for assignment expressions in assert statements. /// /// ## Why is this bad? -/// Assignment expression in assert statements get optimized away when running -/// Python with the `-O` option. +/// Assignment expressions in assert statements will not be executed when the +/// Python interpreter is run with the `-O` option. /// /// ## Examples /// ```python From c619a4d3c54d7fb3fe4f692938c9057cb52bc098 Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 9 Oct 2023 16:10:33 +0900 Subject: [PATCH 3/5] Update snapshot --- .../ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap index d8c6fc14277d9..85d7442e1ee26 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap @@ -6,15 +6,15 @@ RUF018.py:2:9: RUF018 Assignment expression in assert statement is not allowed 1 | # RUF018 2 | assert (x := 0) == 0 | ^^^^^^ RUF018 -3 | assert x, (y := "") +3 | assert x, (y := "error") | RUF018.py:3:12: RUF018 Assignment expression in assert statement is not allowed | 1 | # RUF018 2 | assert (x := 0) == 0 -3 | assert x, (y := "") - | ^^^^^^^ RUF018 +3 | assert x, (y := "error") + | ^^^^^^^^^^^^ RUF018 4 | 5 | # OK | From 866b5bc1ac55d1913593b645e44dfedc0ae90909 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 9 Oct 2023 14:55:00 -0400 Subject: [PATCH 4/5] Tweaks to name, message --- .../src/checkers/ast/analyze/expression.rs | 4 +- crates/ruff_linter/src/checkers/ast/mod.rs | 3 -- crates/ruff_linter/src/codes.rs | 2 +- crates/ruff_linter/src/rules/ruff/mod.rs | 2 +- .../src/rules/ruff/rules/assign_in_assert.rs | 46 ---------------- .../rules/ruff/rules/assignment_in_assert.rs | 53 +++++++++++++++++++ .../ruff_linter/src/rules/ruff/rules/mod.rs | 4 +- ...es__ruff__tests__RUF018_RUF018.py.snap.new | 23 ++++++++ crates/ruff_python_semantic/src/model.rs | 13 ----- crates/ruff_workspace/src/configuration.rs | 2 +- 10 files changed, 83 insertions(+), 69 deletions(-) delete mode 100644 crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs create mode 100644 crates/ruff_linter/src/rules/ruff/rules/assignment_in_assert.rs create mode 100644 crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index 3c0d38ad6495e..707d8e762bf98 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -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); } } _ => {} diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index 66b56e78918f9..dbfa15f65655a 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -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, diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index 49b9a1d677046..f0b9a80804051 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -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), diff --git a/crates/ruff_linter/src/rules/ruff/mod.rs b/crates/ruff_linter/src/rules/ruff/mod.rs index 8eb8077548d8d..60ac081d0bcd6 100644 --- a/crates/ruff_linter/src/rules/ruff/mod.rs +++ b/crates/ruff_linter/src/rules/ruff/mod.rs @@ -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( diff --git a/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs b/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs deleted file mode 100644 index 55e714732270c..0000000000000 --- a/crates/ruff_linter/src/rules/ruff/rules/assign_in_assert.rs +++ /dev/null @@ -1,46 +0,0 @@ -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 assignment expressions in assert statements. -/// -/// ## Why is this bad? -/// Assignment expressions in assert statements will not be executed when the -/// Python interpreter is run with the `-O` option. -/// -/// ## Examples -/// ```python -/// assert (x := 0) == 0 -/// ``` -/// -/// Use instead: -/// ```python -/// x = 0 -/// assert x == 0 -/// ``` -/// -/// ## References -/// - [Python documentation: command option `-O`](https://docs.python.org/3/using/cmdline.html#cmdoption-O) -#[violation] -pub struct AssignInAssert; - -impl Violation for AssignInAssert { - #[derive_message_formats] - fn message(&self) -> String { - format!("Assignment expression in assert statement is not allowed") - } -} - -/// RUF018 -pub(crate) fn assign_in_assert(checker: &mut Checker, value: &Expr) { - if checker.semantic().in_assert() { - checker - .diagnostics - .push(Diagnostic::new(AssignInAssert, value.range())); - } -} diff --git a/crates/ruff_linter/src/rules/ruff/rules/assignment_in_assert.rs b/crates/ruff_linter/src/rules/ruff/rules/assignment_in_assert.rs new file mode 100644 index 0000000000000..05761fe9967df --- /dev/null +++ b/crates/ruff_linter/src/rules/ruff/rules/assignment_in_assert.rs @@ -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())); + } +} diff --git a/crates/ruff_linter/src/rules/ruff/rules/mod.rs b/crates/ruff_linter/src/rules/ruff/rules/mod.rs index 6404c01ccea09..910c6e6379900 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/mod.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/mod.rs @@ -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::*; @@ -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; diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new new file mode 100644 index 0000000000000..a80530998df7a --- /dev/null +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new @@ -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 + | + + diff --git a/crates/ruff_python_semantic/src/model.rs b/crates/ruff_python_semantic/src/model.rs index 799c80f4e64ad..8f742ae452ada 100644 --- a/crates/ruff_python_semantic/src/model.rs +++ b/crates/ruff_python_semantic/src/model.rs @@ -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 @@ -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(); diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 2dddf54886f59..629b2c913bc23 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -1047,7 +1047,7 @@ mod tests { Rule::TooManyPublicMethods, Rule::UndocumentedWarn, Rule::UnnecessaryEnumerate, - Rule::AssignInAssert, + Rule::AssignmentInAssert, ]; #[allow(clippy::needless_pass_by_value)] From f597a7c08fbf8d9d6350008684ed72123a1f9b93 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 9 Oct 2023 15:26:39 -0400 Subject: [PATCH 5/5] Update fixture --- ..._rules__ruff__tests__RUF018_RUF018.py.snap | 4 ++-- ...es__ruff__tests__RUF018_RUF018.py.snap.new | 23 ------------------- 2 files changed, 2 insertions(+), 25 deletions(-) delete mode 100644 crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap index 85d7442e1ee26..d4ceedb6246cf 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs --- -RUF018.py:2:9: RUF018 Assignment expression in assert statement is not allowed +RUF018.py:2:9: RUF018 Avoid assignment expressions in `assert` statements | 1 | # RUF018 2 | assert (x := 0) == 0 @@ -9,7 +9,7 @@ RUF018.py:2:9: RUF018 Assignment expression in assert statement is not allowed 3 | assert x, (y := "error") | -RUF018.py:3:12: RUF018 Assignment expression in assert statement is not allowed +RUF018.py:3:12: RUF018 Avoid assignment expressions in `assert` statements | 1 | # RUF018 2 | assert (x := 0) == 0 diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new deleted file mode 100644 index a80530998df7a..0000000000000 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF018_RUF018.py.snap.new +++ /dev/null @@ -1,23 +0,0 @@ ---- -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 - | - -