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

ℹ Unsafe fix
5  5  | a_dict = {}
6  6  | 
7  7  | # SIM401 (pattern-1)
8     |-if key in a_dict:
9     |-    var = a_dict[key]
10    |-else:
11    |-    var = "default1"
   8  |+var = a_dict.get(key, "default1")
12 9  | 
13 10 | # SIM401 (pattern-2)
14 11 | if key not in a_dict:

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

ℹ Unsafe fix
11 11 |     var = "default1"
12 12 | 
13 13 | # SIM401 (pattern-2)
14    |-if key not in a_dict:
15    |-    var = "default2"
16    |-else:
17    |-    var = a_dict[key]
   14 |+var = a_dict.get(key, "default2")
18 15 | 
19 16 | # OK (default contains effect)
20 17 | if key in a_dict:

SIM401.py:26:1: SIM401 [*] Use `var = a_dict.get(keys[idx], "default")` instead of an `if` block
   |
25 |   # SIM401 (complex expression in key)
26 | / if keys[idx] in a_dict:
27 | |     var = a_dict[keys[idx]]
28 | | else:
29 | |     var = "default"
   | |___________________^ SIM401
30 |
31 |   dicts = {"key": a_dict}
   |
   = help: Replace with `var = a_dict.get(keys[idx], "default")`

ℹ Unsafe fix
23 23 |     var = val1 + val2
24 24 | 
25 25 | # SIM401 (complex expression in key)
26    |-if keys[idx] in a_dict:
27    |-    var = a_dict[keys[idx]]
28    |-else:
29    |-    var = "default"
   26 |+var = a_dict.get(keys[idx], "default")
30 27 | 
31 28 | dicts = {"key": a_dict}
32 29 | 

SIM401.py:40: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
   |
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"
   | |___________________________________________________________________________^ SIM401
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")`

ℹ Unsafe fix
37 37 |     var = "default"
38 38 | 
39 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"
   40 |+vars[idx] = a_dict.get(key, "defaultß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789ß9💣2ℝ6789")
44 41 | 
45 42 | # SIM401
46 43 | if foo():

SIM401.py:49:5: SIM401 [*] Use `vars[idx] = a_dict.get(key, "default")` instead of an `if` block
   |
47 |       pass
48 |   else:
49 | /     if key in a_dict:
50 | |         vars[idx] = a_dict[key]
51 | |     else:
52 | |         vars[idx] = "default"
   | |_____________________________^ SIM401
53 |
54 |   ###
   |
   = help: Replace with `vars[idx] = a_dict.get(key, "default")`

ℹ Unsafe fix
46 46 | if foo():
47 47 |     pass
48 48 | else:
49    |-    if key in a_dict:
50    |-        vars[idx] = a_dict[key]
51    |-    else:
52    |-        vars[idx] = "default"
   49 |+    vars[idx] = a_dict.get(key, "default")
53 50 | 
54 51 | ###
55 52 | # Negative cases

SIM401.py:149:7: SIM401 [*] Use `a_dict.get(key, "default3")` instead of an `if` block
    |
148 | # SIM401
149 | var = a_dict[key] if key in a_dict else "default3"
    |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM401
150 |
151 | # SIM401
    |
    = help: Replace with `a_dict.get(key, "default3")`

ℹ Unsafe fix
146 146 | ###
147 147 | 
148 148 | # SIM401
149     |-var = a_dict[key] if key in a_dict else "default3"
    149 |+var = a_dict.get(key, "default3")
150 150 | 
151 151 | # SIM401
152 152 | var = "default-1" if key not in a_dict else a_dict[key]

SIM401.py:152:7: SIM401 [*] Use `a_dict.get(key, "default-1")` instead of an `if` block
    |
151 | # SIM401
152 | var = "default-1" if key not in a_dict else a_dict[key]
    |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SIM401
153 |
154 | # OK (default contains effect)
    |
    = help: Replace with `a_dict.get(key, "default-1")`

ℹ Unsafe fix
149 149 | var = a_dict[key] if key in a_dict else "default3"
150 150 | 
151 151 | # SIM401
152     |-var = "default-1" if key not in a_dict else a_dict[key]
    152 |+var = a_dict.get(key, "default-1")
153 153 | 
154 154 | # OK (default contains effect)
155 155 | var = a_dict[key] if key in a_dict else val1 + val2
