diff --git a/crates/ruff_linter/src/preview.rs b/crates/ruff_linter/src/preview.rs index f932b2b0592fb..a674894012c73 100644 --- a/crates/ruff_linter/src/preview.rs +++ b/crates/ruff_linter/src/preview.rs @@ -200,13 +200,6 @@ pub(crate) const fn is_allow_nested_roots_enabled(settings: &LinterSettings) -> settings.preview.is_enabled() } -// https://github.com/astral-sh/ruff/pull/18208 -pub(crate) const fn is_multiple_with_statements_fix_safe_enabled( - settings: &LinterSettings, -) -> bool { - settings.preview.is_enabled() -} - // https://github.com/astral-sh/ruff/pull/18400 pub(crate) const fn is_ignore_init_files_in_useless_alias_enabled( settings: &LinterSettings, diff --git a/crates/ruff_linter/src/rules/flake8_simplify/mod.rs b/crates/ruff_linter/src/rules/flake8_simplify/mod.rs index d3ac85a8fb1e2..a66b4f6f1eeb8 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/mod.rs @@ -59,7 +59,6 @@ mod tests { Ok(()) } - #[test_case(Rule::MultipleWithStatements, Path::new("SIM117.py"))] #[test_case(Rule::SplitStaticString, Path::new("SIM905.py"))] fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!( diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs index 248a7250e7d93..973adba1a83f1 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs @@ -10,7 +10,6 @@ use super::fix_with; use crate::Fix; use crate::checkers::ast::Checker; use crate::fix::edits::fits; -use crate::preview::is_multiple_with_statements_fix_safe_enabled; use crate::{FixAvailability, Violation}; /// ## What it does @@ -45,15 +44,8 @@ use crate::{FixAvailability, Violation}; /// pass /// ``` /// -/// ## Fix safety -/// -/// This fix is marked as always unsafe unless [preview] mode is enabled, in which case it is always -/// marked as safe. Note that the fix is unavailable if it would remove comments (in either case). -/// /// ## References /// - [Python documentation: The `with` statement](https://docs.python.org/3/reference/compound_stmts.html#the-with-statement) -/// -/// [preview]: https://docs.astral.sh/ruff/preview/ #[derive(ViolationMetadata)] pub(crate) struct MultipleWithStatements; @@ -195,11 +187,7 @@ pub(crate) fn multiple_with_statements( checker.settings().tab_size, ) }) { - if is_multiple_with_statements_fix_safe_enabled(checker.settings()) { - Ok(Some(Fix::safe_edit(edit))) - } else { - Ok(Some(Fix::unsafe_edit(edit))) - } + Ok(Some(Fix::safe_edit(edit))) } else { Ok(None) } diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM117_SIM117.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM117_SIM117.py.snap index f6f16d932acb7..62d5972419eea 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM117_SIM117.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM117_SIM117.py.snap @@ -20,7 +20,6 @@ help: Combine `with` statements 4 | 5 | # SIM117 6 | with A(): -note: This is an unsafe fix and may change runtime behavior SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:7:1 @@ -46,7 +45,6 @@ help: Combine `with` statements 10 | 11 | # SIM117 12 | with A() as a: -note: This is an unsafe fix and may change runtime behavior SIM117 Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:13:1 @@ -84,7 +82,6 @@ help: Combine `with` statements 22 | 23 | # OK 24 | with A() as a: -note: This is an unsafe fix and may change runtime behavior SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:47:1 @@ -107,7 +104,6 @@ help: Combine `with` statements 49 | 50 | while True: 51 | # SIM117 -note: This is an unsafe fix and may change runtime behavior SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:53:5 @@ -144,7 +140,6 @@ help: Combine `with` statements 64 | "this for some reason") 65 | 66 | # SIM117 -note: This is an unsafe fix and may change runtime behavior SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:68:1 @@ -171,7 +166,6 @@ help: Combine `with` statements 73 | 74 | # SIM117 75 | with A() as a: -note: This is an unsafe fix and may change runtime behavior SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:76:1 @@ -203,7 +197,6 @@ help: Combine `with` statements 81 | 82 | # SIM117 83 | with ( -note: This is an unsafe fix and may change runtime behavior SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:84:1 @@ -237,7 +230,6 @@ help: Combine `with` statements 90 | 91 | # SIM117 (auto-fixable) 92 | with A("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as a: -note: This is an unsafe fix and may change runtime behavior SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:95:1 @@ -260,7 +252,6 @@ help: Combine `with` statements 97 | 98 | # SIM117 (not auto-fixable too long) 99 | with A("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ890") as a: -note: This is an unsafe fix and may change runtime behavior SIM117 Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:100:1 @@ -319,7 +310,6 @@ help: Combine `with` statements 136 | 137 | # Allow cascading for some statements. 138 | import anyio -note: This is an unsafe fix and may change runtime behavior SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements --> SIM117.py:163:1 @@ -339,4 +329,3 @@ help: Combine `with` statements - pass 163 + async with asyncio.timeout(1), A(), B(): 164 + pass -note: This is an unsafe fix and may change runtime behavior diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM117_SIM117.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM117_SIM117.py.snap deleted file mode 100644 index 62d5972419eea..0000000000000 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM117_SIM117.py.snap +++ /dev/null @@ -1,331 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs ---- -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:2:1 - | -1 | # SIM117 -2 | / with A() as a: -3 | | with B() as b: - | |__________________^ -4 | print("hello") - | -help: Combine `with` statements -1 | # SIM117 - - with A() as a: - - with B() as b: - - print("hello") -2 + with A() as a, B() as b: -3 + print("hello") -4 | -5 | # SIM117 -6 | with A(): - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:7:1 - | - 6 | # SIM117 - 7 | / with A(): - 8 | | with B(): - | |_____________^ - 9 | with C(): -10 | print("hello") - | -help: Combine `with` statements -4 | print("hello") -5 | -6 | # SIM117 - - with A(): - - with B(): - - with C(): - - print("hello") -7 + with A(), B(): -8 + with C(): -9 + print("hello") -10 | -11 | # SIM117 -12 | with A() as a: - -SIM117 Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:13:1 - | -12 | # SIM117 -13 | / with A() as a: -14 | | # Unfixable due to placement of this comment. -15 | | with B() as b: - | |__________________^ -16 | print("hello") - | -help: Combine `with` statements - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:19:1 - | -18 | # SIM117 -19 | / with A() as a: -20 | | with B() as b: - | |__________________^ -21 | # Fixable due to placement of this comment. -22 | print("hello") - | -help: Combine `with` statements -16 | print("hello") -17 | -18 | # SIM117 - - with A() as a: - - with B() as b: - - # Fixable due to placement of this comment. - - print("hello") -19 + with A() as a, B() as b: -20 + # Fixable due to placement of this comment. -21 + print("hello") -22 | -23 | # OK -24 | with A() as a: - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:47:1 - | -46 | # SIM117 -47 | / async with A() as a: -48 | | async with B() as b: - | |________________________^ -49 | print("hello") - | -help: Combine `with` statements -44 | print("hello") -45 | -46 | # SIM117 - - async with A() as a: - - async with B() as b: - - print("hello") -47 + async with A() as a, B() as b: -48 + print("hello") -49 | -50 | while True: -51 | # SIM117 - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:53:5 - | -51 | while True: -52 | # SIM117 -53 | / with A() as a: -54 | | with B() as b: - | |______________________^ -55 | """this -56 | is valid""" - | -help: Combine `with` statements -50 | -51 | while True: -52 | # SIM117 - - with A() as a: - - with B() as b: - - """this -53 + with A() as a, B() as b: -54 + """this -55 | is valid""" -56 | - - """the indentation on -57 + """the indentation on -58 | this line is significant""" -59 | - - "this is" \ -60 + "this is" \ -61 | "allowed too" -62 | - - ("so is" -63 + ("so is" -64 | "this for some reason") -65 | -66 | # SIM117 - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:68:1 - | -67 | # SIM117 -68 | / with ( -69 | | A() as a, -70 | | B() as b, -71 | | ): -72 | | with C() as c: - | |__________________^ -73 | print("hello") - | -help: Combine `with` statements -67 | # SIM117 -68 | with ( -69 | A() as a, - - B() as b, -70 + B() as b,C() as c -71 | ): - - with C() as c: - - print("hello") -72 + print("hello") -73 | -74 | # SIM117 -75 | with A() as a: - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:76:1 - | -75 | # SIM117 -76 | / with A() as a: -77 | | with ( -78 | | B() as b, -79 | | C() as c, -80 | | ): - | |______^ -81 | print("hello") - | -help: Combine `with` statements -73 | print("hello") -74 | -75 | # SIM117 - - with A() as a: - - with ( - - B() as b, - - C() as c, - - ): - - print("hello") -76 + with ( -77 + A() as a, B() as b, -78 + C() as c, -79 + ): -80 + print("hello") -81 | -82 | # SIM117 -83 | with ( - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:84:1 - | -83 | # SIM117 -84 | / with ( -85 | | A() as a, -86 | | B() as b, -87 | | ): -88 | | with ( -89 | | C() as c, -90 | | D() as d, -91 | | ): - | |______^ -92 | print("hello") - | -help: Combine `with` statements -83 | # SIM117 -84 | with ( -85 | A() as a, - - B() as b, -86 + B() as b,C() as c, -87 + D() as d, -88 | ): - - with ( - - C() as c, - - D() as d, - - ): - - print("hello") -89 + print("hello") -90 | -91 | # SIM117 (auto-fixable) -92 | with A("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as a: - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:95:1 - | -94 | # SIM117 (auto-fixable) -95 | / with A("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as a: -96 | | with B("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as b: - | |__________________________________________________^ -97 | print("hello") - | -help: Combine `with` statements -92 | print("hello") -93 | -94 | # SIM117 (auto-fixable) - - with A("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as a: - - with B("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as b: - - print("hello") -95 + with A("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as a, B("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as b: -96 + print("hello") -97 | -98 | # SIM117 (not auto-fixable too long) -99 | with A("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ890") as a: - -SIM117 Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:100:1 - | - 99 | # SIM117 (not auto-fixable too long) -100 | / with A("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ890") as a: -101 | | with B("01ß9💣2ℝ8901ß9💣2ℝ8901ß9💣2ℝ89") as b: - | |__________________________________________________^ -102 | print("hello") - | -help: Combine `with` statements - -SIM117 Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:106:5 - | -104 | # From issue #3025. -105 | async def main(): -106 | / async with A() as a: # SIM117. -107 | | async with B() as b: - | |____________________________^ -108 | print("async-inside!") - | -help: Combine `with` statements - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:126:1 - | -125 | # SIM117 -126 | / with A() as a: -127 | | with B() as b: - | |__________________^ -128 | type ListOrSet[T] = list[T] | set[T] - | -help: Combine `with` statements -123 | f(b2, c2, d2) -124 | -125 | # SIM117 - - with A() as a: - - with B() as b: - - type ListOrSet[T] = list[T] | set[T] -126 + with A() as a, B() as b: -127 + type ListOrSet[T] = list[T] | set[T] -128 | - - class ClassA[T: str]: - - def method1(self) -> T: - - ... -129 + class ClassA[T: str]: -130 + def method1(self) -> T: -131 + ... -132 | - - f" something { my_dict["key"] } something else " -133 + f" something { my_dict["key"] } something else " -134 | - - f"foo {f"bar {x}"} baz" -135 + f"foo {f"bar {x}"} baz" -136 | -137 | # Allow cascading for some statements. -138 | import anyio - -SIM117 [*] Use a single `with` statement with multiple contexts instead of nested `with` statements - --> SIM117.py:163:1 - | -162 | # Do not suppress combination, if a context manager is already combined with another. -163 | / async with asyncio.timeout(1), A(): -164 | | async with B(): - | |___________________^ -165 | pass - | -help: Combine `with` statements -160 | pass -161 | -162 | # Do not suppress combination, if a context manager is already combined with another. - - async with asyncio.timeout(1), A(): - - async with B(): - - pass -163 + async with asyncio.timeout(1), A(), B(): -164 + pass