---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
SIM401 [*] Use `var = a_dict.get(key, "default1")` instead of an `if` block
  --> SIM401.py:8:1
   |
 7 |   # SIM401 (pattern-1)
 8 | / if key in a_dict:
 9 | |     var = a_dict[key]
10 | | else:
11 | |     var = "default1"
   | |____________________^
12 |
13 |   # SIM401 (pattern-2)
   |
help: Replace with `var = a_dict.get(key, "default1")`
  |
7 | # SIM401 (pattern-1)
  - if key in a_dict:
  -     var = a_dict[key]
  - else:
  -     var = "default1"
8 + var = a_dict.get(key, "default1")
9 |
  |
note: This is an unsafe fix and may change runtime behavior

SIM401 [*] Use `var = a_dict.get(key, "default2")` instead of an `if` block
  --> SIM401.py:14:1
   |
13 |   # SIM401 (pattern-2)
14 | / if key not in a_dict:
15 | |     var = "default2"
16 | | else:
17 | |     var = a_dict[key]
   | |_____________________^
18 |
19 |   # OK (default contains effect)
   |
help: Replace with `var = a_dict.get(key, "default2")`
   |
13 | # SIM401 (pattern-2)
   - if key not in a_dict:
   -     var = "default2"
   - else:
   -     var = a_dict[key]
14 + var = a_dict.get(key, "default2")
15 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM401 [*] Use `var = a_dict.get(keys[idx], "default")` instead of an `if` block
  --> SIM401.py:26:1
   |
25 |   # SIM401 (complex expression in key)
26 | / if keys[idx] in a_dict:
27 | |     var = a_dict[keys[idx]]
28 | | else:
29 | |     var = "default"
   | |___________________^
30 |
31 |   dicts = {"key": a_dict}
   |
help: Replace with `var = a_dict.get(keys[idx], "default")`
   |
25 | # SIM401 (complex expression in key)
   - if keys[idx] in a_dict:
   -     var = a_dict[keys[idx]]
   - else:
   -     var = "default"
26 + var = a_dict.get(keys[idx], "default")
27 |
   |
note: This is an unsafe fix and may change runtime behavior

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
  --> SIM401.py:40:1
   |
39 |   # SIM401 (complex expression in var)
40 | / if key in a_dict:
41 | |     vars[idx] = a_dict[key]
42 | | else:
43 | |     vars[idx] = "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789"
   | |___________________________________________________________________________^
44 |
45 |   # 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")`
   |
39 | # SIM401 (complex expression in var)
   - if key in a_dict:
   -     vars[idx] = a_dict[key]
   - else:
   -     vars[idx] = "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789"
40 + vars[idx] = a_dict.get(key, "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789")
41 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM401 [*] Use `vars[idx] = a_dict.get(key, "default")` instead of an `if` block
  --> SIM401.py:49:5
   |
47 |       pass
48 |   else:
49 | /     if key in a_dict:
50 | |         vars[idx] = a_dict[key]
51 | |     else:
52 | |         vars[idx] = "default"
   | |_____________________________^
53 |
54 |   ###
   |
help: Replace with `vars[idx] = a_dict.get(key, "default")`
   |
48 | else:
   -     if key in a_dict:
   -         vars[idx] = a_dict[key]
   -     else:
   -         vars[idx] = "default"
49 +     vars[idx] = a_dict.get(key, "default")
50 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM401 [*] Use `a_dict.get(key, "default3")` instead of an `if` block
   --> SIM401.py:149:7
    |
148 | # SIM401
149 | var = a_dict[key] if key in a_dict else "default3"
    |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
150 |
151 | # SIM401
    |
help: Replace with `a_dict.get(key, "default3")`
    |
148 | # SIM401
    - var = a_dict[key] if key in a_dict else "default3"
149 + var = a_dict.get(key, "default3")
150 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM401 [*] Use `a_dict.get(key, "default-1")` instead of an `if` block
   --> SIM401.py:152:7
    |
151 | # SIM401
152 | var = "default-1" if key not in a_dict else a_dict[key]
    |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
153 |
154 | # OK (default contains effect)
    |
help: Replace with `a_dict.get(key, "default-1")`
    |
151 | # SIM401
    - var = "default-1" if key not in a_dict else a_dict[key]
152 + var = a_dict.get(key, "default-1")
153 |
    |
note: This is an unsafe fix and may change runtime behavior
