diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM108.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM108.py index 25991d478233d..52da2ecd37e2b 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM108.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM108.py @@ -194,3 +194,17 @@ def f(): z = not foo() else: z = other + + +# These two cases double as tests for f-string quote preservation. The first +# f-string should preserve its double quotes, and the second should preserve +# single quotes +if cond: + var = "str" +else: + var = f"{first}-{second}" + +if cond: + var = "str" +else: + var = f'{first}-{second}' diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM108_SIM108.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM108_SIM108.py.snap index de1dcf780b6da..3306802cce870 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM108_SIM108.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM108_SIM108.py.snap @@ -301,3 +301,55 @@ SIM108.py:193:1: SIM108 [*] Use ternary operator `z = not foo() if foo() else ot 195 |-else: 196 |- z = other 193 |+z = not foo() if foo() else other +197 194 | +198 195 | +199 196 | # These two cases double as tests for f-string quote preservation. The first + +SIM108.py:202:1: SIM108 [*] Use ternary operator `var = "str" if cond else f"{first}-{second}"` instead of `if`-`else`-block + | +200 | # f-string should preserve its double quotes, and the second should preserve +201 | # single quotes +202 | / if cond: +203 | | var = "str" +204 | | else: +205 | | var = f"{first}-{second}" + | |_____________________________^ SIM108 +206 | +207 | if cond: + | + = help: Replace `if`-`else`-block with `var = "str" if cond else f"{first}-{second}"` + +ℹ Unsafe fix +199 199 | # These two cases double as tests for f-string quote preservation. The first +200 200 | # f-string should preserve its double quotes, and the second should preserve +201 201 | # single quotes +202 |-if cond: +203 |- var = "str" +204 |-else: +205 |- var = f"{first}-{second}" + 202 |+var = "str" if cond else f"{first}-{second}" +206 203 | +207 204 | if cond: +208 205 | var = "str" + +SIM108.py:207:1: SIM108 [*] Use ternary operator `var = "str" if cond else f'{first}-{second}'` instead of `if`-`else`-block + | +205 | var = f"{first}-{second}" +206 | +207 | / if cond: +208 | | var = "str" +209 | | else: +210 | | var = f'{first}-{second}' + | |_____________________________^ SIM108 + | + = help: Replace `if`-`else`-block with `var = "str" if cond else f'{first}-{second}'` + +ℹ Unsafe fix +204 204 | else: +205 205 | var = f"{first}-{second}" +206 206 | +207 |-if cond: +208 |- var = "str" +209 |-else: +210 |- var = f'{first}-{second}' + 207 |+var = "str" if cond else f'{first}-{second}' diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM108_SIM108.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM108_SIM108.py.snap index 723cf6fa66768..6b72655b2f802 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM108_SIM108.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM108_SIM108.py.snap @@ -301,3 +301,55 @@ SIM108.py:193:1: SIM108 [*] Use ternary operator `z = not foo() if foo() else ot 195 |-else: 196 |- z = other 193 |+z = not foo() if foo() else other +197 194 | +198 195 | +199 196 | # These two cases double as tests for f-string quote preservation. The first + +SIM108.py:202:1: SIM108 [*] Use ternary operator `var = "str" if cond else f"{first}-{second}"` instead of `if`-`else`-block + | +200 | # f-string should preserve its double quotes, and the second should preserve +201 | # single quotes +202 | / if cond: +203 | | var = "str" +204 | | else: +205 | | var = f"{first}-{second}" + | |_____________________________^ SIM108 +206 | +207 | if cond: + | + = help: Replace `if`-`else`-block with `var = "str" if cond else f"{first}-{second}"` + +ℹ Unsafe fix +199 199 | # These two cases double as tests for f-string quote preservation. The first +200 200 | # f-string should preserve its double quotes, and the second should preserve +201 201 | # single quotes +202 |-if cond: +203 |- var = "str" +204 |-else: +205 |- var = f"{first}-{second}" + 202 |+var = "str" if cond else f"{first}-{second}" +206 203 | +207 204 | if cond: +208 205 | var = "str" + +SIM108.py:207:1: SIM108 [*] Use ternary operator `var = "str" if cond else f'{first}-{second}'` instead of `if`-`else`-block + | +205 | var = f"{first}-{second}" +206 | +207 | / if cond: +208 | | var = "str" +209 | | else: +210 | | var = f'{first}-{second}' + | |_____________________________^ SIM108 + | + = help: Replace `if`-`else`-block with `var = "str" if cond else f'{first}-{second}'` + +ℹ Unsafe fix +204 204 | else: +205 205 | var = f"{first}-{second}" +206 206 | +207 |-if cond: +208 |- var = "str" +209 |-else: +210 |- var = f'{first}-{second}' + 207 |+var = "str" if cond else f'{first}-{second}'