From 4dd2c03144828caf4e49c2ec61280e0311b10aaf Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Mon, 7 Jul 2025 14:17:55 -0700 Subject: [PATCH] [`flake8-simplify`] Make example error out-of-the-box (`SIM116`) (#19111) ## Summary Part of #18972 This PR makes [if-else-block-instead-of-dict-lookup (SIM116)](https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-lookup/#if-else-block-instead-of-dict-lookup-sim116)'s example error out-of-the-box [Old example](https://play.ruff.rs/718f17ee-fbe2-4520-97c6-153bc0f4502d) ```py if x == 1: return "Hello" elif x == 2: return "Goodbye" else: return "Goodnight" ``` [New example](https://play.ruff.rs/8a9b47b4-da46-4a50-8576-362cdd707cee) ```py def find_phrase(x): if x == 1: return "Hello" elif x == 2: return "Goodbye" elif x == 3: return "Good morning" else: return "Goodnight" ``` The "Use instead" section was also updated to reflect the new case. I also changed it to use an intermediary variable since I find the `return .get` very ugly and hard to read. ## Test Plan N/A, no functionality/tests affected --- .../if_else_block_instead_of_dict_lookup.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_lookup.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_lookup.rs index f9806190de..e3e7a41b63 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_lookup.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_lookup.rs @@ -18,17 +18,22 @@ use crate::checkers::ast::Checker; /// /// ## Example /// ```python -/// if x == 1: -/// return "Hello" -/// elif x == 2: -/// return "Goodbye" -/// else: -/// return "Goodnight" +/// def find_phrase(x): +/// if x == 1: +/// return "Hello" +/// elif x == 2: +/// return "Goodbye" +/// elif x == 3: +/// return "Good morning" +/// else: +/// return "Goodnight" /// ``` /// /// Use instead: /// ```python -/// return {1: "Hello", 2: "Goodbye"}.get(x, "Goodnight") +/// def find_phrase(x): +/// phrases = {1: "Hello", 2: "Goodye", 3: "Good morning"} +/// return phrases.get(x, "Goodnight") /// ``` #[derive(ViolationMetadata)] pub(crate) struct IfElseBlockInsteadOfDictLookup;