From 556116ee76c577e65178be0fbebce6d0272e9523 Mon Sep 17 00:00:00 2001 From: InSync Date: Fri, 17 Jan 2025 13:48:35 +0700 Subject: [PATCH] [`flake8-simplify`] Do not emit diagnostics for expressions inside string type annotations (`SIM222`, `SIM223`) (#15405) ## Summary Resolves #7127. ## Test Plan `cargo nextest run` and `cargo insta test`. --- .../resources/test/fixtures/flake8_simplify/SIM222.py | 4 ++++ .../resources/test/fixtures/flake8_simplify/SIM223.py | 4 ++++ .../src/rules/flake8_simplify/rules/ast_bool_op.rs | 8 ++++++++ ...__rules__flake8_simplify__tests__SIM223_SIM223.py.snap | 2 ++ 4 files changed, 18 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py index 8252420f6a..e2f909acf0 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py @@ -193,3 +193,7 @@ for x in (*a, *b) or [None]: for x in {**a, **b} or [None]: pass + + +# https://github.com/astral-sh/ruff/issues/7127 +def f(a: "'b' or 'c'"): ... diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM223.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM223.py index 1842f06994..12edbff6bd 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM223.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM223.py @@ -153,3 +153,7 @@ print(f"{a}{b}" and "bar") print(f"{a}{''}" and "bar") print(f"{''}{''}" and "bar") print(f"{1}{''}" and "bar") + + +# https://github.com/astral-sh/ruff/issues/7127 +def f(a: "'' and 'b'"): ... diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs index ca1b1c759f..d5cce6f530 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs @@ -833,6 +833,10 @@ fn is_short_circuit( /// SIM222 pub(crate) fn expr_or_true(checker: &mut Checker, expr: &Expr) { + if checker.semantic().in_string_type_definition() { + return; + } + if let Some((edit, remove)) = is_short_circuit(expr, BoolOp::Or, checker) { let mut diagnostic = Diagnostic::new( ExprOrTrue { @@ -848,6 +852,10 @@ pub(crate) fn expr_or_true(checker: &mut Checker, expr: &Expr) { /// SIM223 pub(crate) fn expr_and_false(checker: &mut Checker, expr: &Expr) { + if checker.semantic().in_string_type_definition() { + return; + } + if let Some((edit, remove)) = is_short_circuit(expr, BoolOp::And, checker) { let mut diagnostic = Diagnostic::new( ExprAndFalse { diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM223_SIM223.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM223_SIM223.py.snap index 5848727101..50e31f645c 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM223_SIM223.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM223_SIM223.py.snap @@ -1023,3 +1023,5 @@ SIM223.py:154:7: SIM223 [*] Use `f"{''}{''}"` instead of `f"{''}{''}" and ...` 154 |-print(f"{''}{''}" and "bar") 154 |+print(f"{''}{''}") 155 155 | print(f"{1}{''}" and "bar") +156 156 | +157 157 |