From 2dea20f4e9b8e293a4d03f82dab33669ca174070 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 29 Jan 2024 11:46:29 -0800 Subject: [PATCH] Remove preview gating for `flake8-simplify` rules (#9686) ## Summary Un-gates detecting `dict.get` rewrites in `if` expressions (rather than just `if` statements). --- .../src/rules/flake8_simplify/mod.rs | 1 - .../if_else_block_instead_of_dict_get.rs | 4 - ...ke8_simplify__tests__SIM401_SIM401.py.snap | 40 ++++ ...ify__tests__preview__SIM401_SIM401.py.snap | 202 ------------------ 4 files changed, 40 insertions(+), 207 deletions(-) delete mode 100644 crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM401_SIM401.py.snap diff --git a/crates/ruff_linter/src/rules/flake8_simplify/mod.rs b/crates/ruff_linter/src/rules/flake8_simplify/mod.rs index 3ebe88af1f0f4..e68c9d6b471ca 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/mod.rs @@ -57,7 +57,6 @@ mod tests { } #[test_case(Rule::YodaConditions, Path::new("SIM300.py"))] - #[test_case(Rule::IfElseBlockInsteadOfDictGet, Path::new("SIM401.py"))] fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!( "preview__{}_{}", diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs index 50d6e8b7bb129..1c710427fe077 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs @@ -224,10 +224,6 @@ pub(crate) fn if_exp_instead_of_dict_get( body: &Expr, orelse: &Expr, ) { - if checker.settings.preview.is_disabled() { - return; - } - let Expr::Compare(ast::ExprCompare { left: test_key, ops, diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM401_SIM401.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM401_SIM401.py.snap index 452916c60e98b..15ceaf67f67b6 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM401_SIM401.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM401_SIM401.py.snap @@ -159,4 +159,44 @@ SIM401.py:45:5: SIM401 [*] Use `vars[idx] = a_dict.get(key, "default")` instead 50 47 | ### 51 48 | # Negative cases +SIM401.py:123:7: SIM401 [*] Use `a_dict.get(key, "default3")` instead of an `if` block + | +122 | # SIM401 +123 | var = a_dict[key] if key in a_dict else "default3" + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM401 +124 | +125 | # SIM401 + | + = help: Replace with `a_dict.get(key, "default3")` + +ℹ Unsafe fix +120 120 | ### +121 121 | +122 122 | # SIM401 +123 |-var = a_dict[key] if key in a_dict else "default3" + 123 |+var = a_dict.get(key, "default3") +124 124 | +125 125 | # SIM401 +126 126 | var = "default-1" if key not in a_dict else a_dict[key] + +SIM401.py:126:7: SIM401 [*] Use `a_dict.get(key, "default-1")` instead of an `if` block + | +125 | # SIM401 +126 | var = "default-1" if key not in a_dict else a_dict[key] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM401 +127 | +128 | # OK (default contains effect) + | + = help: Replace with `a_dict.get(key, "default-1")` + +ℹ Unsafe fix +123 123 | var = a_dict[key] if key in a_dict else "default3" +124 124 | +125 125 | # SIM401 +126 |-var = "default-1" if key not in a_dict else a_dict[key] + 126 |+var = a_dict.get(key, "default-1") +127 127 | +128 128 | # OK (default contains effect) +129 129 | var = a_dict[key] if key in a_dict else val1 + val2 + diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM401_SIM401.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM401_SIM401.py.snap deleted file mode 100644 index 15ceaf67f67b6..0000000000000 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__preview__SIM401_SIM401.py.snap +++ /dev/null @@ -1,202 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs ---- -SIM401.py:6:1: SIM401 [*] Use `var = a_dict.get(key, "default1")` instead of an `if` block - | - 5 | # SIM401 (pattern-1) - 6 | / if key in a_dict: - 7 | | var = a_dict[key] - 8 | | else: - 9 | | var = "default1" - | |____________________^ SIM401 -10 | -11 | # SIM401 (pattern-2) - | - = help: Replace with `var = a_dict.get(key, "default1")` - -ℹ Unsafe fix -3 3 | ### -4 4 | -5 5 | # SIM401 (pattern-1) -6 |-if key in a_dict: -7 |- var = a_dict[key] -8 |-else: -9 |- var = "default1" - 6 |+var = a_dict.get(key, "default1") -10 7 | -11 8 | # SIM401 (pattern-2) -12 9 | if key not in a_dict: - -SIM401.py:12:1: SIM401 [*] Use `var = a_dict.get(key, "default2")` instead of an `if` block - | -11 | # SIM401 (pattern-2) -12 | / if key not in a_dict: -13 | | var = "default2" -14 | | else: -15 | | var = a_dict[key] - | |_____________________^ SIM401 -16 | -17 | # OK (default contains effect) - | - = help: Replace with `var = a_dict.get(key, "default2")` - -ℹ Unsafe fix -9 9 | var = "default1" -10 10 | -11 11 | # SIM401 (pattern-2) -12 |-if key not in a_dict: -13 |- var = "default2" -14 |-else: -15 |- var = a_dict[key] - 12 |+var = a_dict.get(key, "default2") -16 13 | -17 14 | # OK (default contains effect) -18 15 | if key in a_dict: - -SIM401.py:24:1: SIM401 [*] Use `var = a_dict.get(keys[idx], "default")` instead of an `if` block - | -23 | # SIM401 (complex expression in key) -24 | / if keys[idx] in a_dict: -25 | | var = a_dict[keys[idx]] -26 | | else: -27 | | var = "default" - | |___________________^ SIM401 -28 | -29 | # SIM401 (complex expression in dict) - | - = help: Replace with `var = a_dict.get(keys[idx], "default")` - -ℹ Unsafe fix -21 21 | var = val1 + val2 -22 22 | -23 23 | # SIM401 (complex expression in key) -24 |-if keys[idx] in a_dict: -25 |- var = a_dict[keys[idx]] -26 |-else: -27 |- var = "default" - 24 |+var = a_dict.get(keys[idx], "default") -28 25 | -29 26 | # SIM401 (complex expression in dict) -30 27 | if key in dicts[idx]: - -SIM401.py:30:1: SIM401 [*] Use `var = dicts[idx].get(key, "default")` instead of an `if` block - | -29 | # SIM401 (complex expression in dict) -30 | / if key in dicts[idx]: -31 | | var = dicts[idx][key] -32 | | else: -33 | | var = "default" - | |___________________^ SIM401 -34 | -35 | # SIM401 (complex expression in var) - | - = help: Replace with `var = dicts[idx].get(key, "default")` - -ℹ Unsafe fix -27 27 | var = "default" -28 28 | -29 29 | # SIM401 (complex expression in dict) -30 |-if key in dicts[idx]: -31 |- var = dicts[idx][key] -32 |-else: -33 |- var = "default" - 30 |+var = dicts[idx].get(key, "default") -34 31 | -35 32 | # SIM401 (complex expression in var) -36 33 | if key in a_dict: - -SIM401.py:36:1: SIM401 [*] Use `vars[idx] = a_dict.get(key, "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789")` instead of an `if` block - | -35 | # SIM401 (complex expression in var) -36 | / if key in a_dict: -37 | | vars[idx] = a_dict[key] -38 | | else: -39 | | vars[idx] = "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789" - | |___________________________________________________________________________^ SIM401 -40 | -41 | # SIM401 - | - = help: Replace with `vars[idx] = a_dict.get(key, "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789")` - -ℹ Unsafe fix -33 33 | var = "default" -34 34 | -35 35 | # SIM401 (complex expression in var) -36 |-if key in a_dict: -37 |- vars[idx] = a_dict[key] -38 |-else: -39 |- vars[idx] = "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789" - 36 |+vars[idx] = a_dict.get(key, "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789") -40 37 | -41 38 | # SIM401 -42 39 | if foo(): - -SIM401.py:45:5: SIM401 [*] Use `vars[idx] = a_dict.get(key, "default")` instead of an `if` block - | -43 | pass -44 | else: -45 | if key in a_dict: - | _____^ -46 | | vars[idx] = a_dict[key] -47 | | else: -48 | | vars[idx] = "default" - | |_____________________________^ SIM401 -49 | -50 | ### - | - = help: Replace with `vars[idx] = a_dict.get(key, "default")` - -ℹ Unsafe fix -42 42 | if foo(): -43 43 | pass -44 44 | else: -45 |- if key in a_dict: -46 |- vars[idx] = a_dict[key] -47 |- else: -48 |- vars[idx] = "default" - 45 |+ vars[idx] = a_dict.get(key, "default") -49 46 | -50 47 | ### -51 48 | # Negative cases - -SIM401.py:123:7: SIM401 [*] Use `a_dict.get(key, "default3")` instead of an `if` block - | -122 | # SIM401 -123 | var = a_dict[key] if key in a_dict else "default3" - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM401 -124 | -125 | # SIM401 - | - = help: Replace with `a_dict.get(key, "default3")` - -ℹ Unsafe fix -120 120 | ### -121 121 | -122 122 | # SIM401 -123 |-var = a_dict[key] if key in a_dict else "default3" - 123 |+var = a_dict.get(key, "default3") -124 124 | -125 125 | # SIM401 -126 126 | var = "default-1" if key not in a_dict else a_dict[key] - -SIM401.py:126:7: SIM401 [*] Use `a_dict.get(key, "default-1")` instead of an `if` block - | -125 | # SIM401 -126 | var = "default-1" if key not in a_dict else a_dict[key] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM401 -127 | -128 | # OK (default contains effect) - | - = help: Replace with `a_dict.get(key, "default-1")` - -ℹ Unsafe fix -123 123 | var = a_dict[key] if key in a_dict else "default3" -124 124 | -125 125 | # SIM401 -126 |-var = "default-1" if key not in a_dict else a_dict[key] - 126 |+var = a_dict.get(key, "default-1") -127 127 | -128 128 | # OK (default contains effect) -129 129 | var = a_dict[key] if key in a_dict else val1 + val2 - -