---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
SIM108 [*] Use ternary operator `b = c if a else d` instead of `if`-`else`-block
 --> SIM108.py:2:1
  |
1 |   # SIM108
2 | / if a:
3 | |     b = c
4 | | else:
5 | |     b = d
  | |_________^
6 |
7 |   # OK
  |
help: Replace `if`-`else`-block with `b = c if a else d`
  |
1 | # SIM108
  - if a:
  -     b = c
  - else:
  -     b = d
2 + b = c if a else d
3 |
  |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use ternary operator `b = 1 if a else 2` instead of `if`-`else`-block
  --> SIM108.py:30:5
   |
28 |       pass
29 |   else:
30 | /     if a:
31 | |         b = 1
32 | |     else:
33 | |         b = 2
   | |_____________^
   |
help: Replace `if`-`else`-block with `b = 1 if a else 2`
   |
29 | else:
   -     if a:
   -         b = 1
   -     else:
   -         b = 2
30 +     b = 1 if a else 2
31 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM108 Use ternary operator `abc = x if x > 0 else -x` instead of `if`-`else`-block
  --> SIM108.py:58:1
   |
57 |   # SIM108 (without fix due to comments)
58 | / if x > 0:
59 | |     # test test
60 | |     abc = x
61 | | else:
62 | |     # test test test
63 | |     abc = -x
   | |____________^
   |
help: Replace `if`-`else`-block with `abc = x if x > 0 else -x`

SIM108 [*] Use ternary operator `b = "cccccccccccccccccccccccccccccccccß" if a else "ddddddddddddddddddddddddddddddddd💣"` instead of `if`-`else`-block
  --> SIM108.py:82:1
   |
81 |   # SIM108
82 | / if a:
83 | |     b = "cccccccccccccccccccccccccccccccccß"
84 | | else:
85 | |     b = "ddddddddddddddddddddddddddddddddd💣"
   | |_____________________________________________^
   |
help: Replace `if`-`else`-block with `b = "cccccccccccccccccccccccccccccccccß" if a else "ddddddddddddddddddddddddddddddddd💣"`
   |
81 | # SIM108
   - if a:
   -     b = "cccccccccccccccccccccccccccccccccß"
   - else:
   -     b = "ddddddddddddddddddddddddddddddddd💣"
82 + b = "cccccccccccccccccccccccccccccccccß" if a else "ddddddddddddddddddddddddddddddddd💣"
83 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM108 Use ternary operator `exitcode = 0 if True else 1` instead of `if`-`else`-block
   --> SIM108.py:105:1
    |
104 |   # SIM108 (without fix due to trailing comment)
105 | / if True:
106 | |     exitcode = 0
107 | | else:
108 | |     exitcode = 1  # Trailing comment
    | |________________^
    |
help: Replace `if`-`else`-block with `exitcode = 0 if True else 1`

SIM108 Use ternary operator `x = 3 if True else 5` instead of `if`-`else`-block
   --> SIM108.py:112:1
    |
111 |   # SIM108
112 | / if True: x = 3  # Foo
113 | | else: x = 5
    | |___________^
    |
help: Replace `if`-`else`-block with `x = 3 if True else 5`

SIM108 Use ternary operator `x = 3 if True else 5` instead of `if`-`else`-block
   --> SIM108.py:117:1
    |
116 |   # SIM108
117 | / if True:  # Foo
118 | |     x = 3
119 | | else:
120 | |     x = 5
    | |_________^
    |
help: Replace `if`-`else`-block with `x = 3 if True else 5`

SIM108 [*] Use binary operator `z = cond or other_cond` instead of `if`-`else`-block
   --> SIM108.py:141:1
    |
139 |   # SIM108 - should suggest
140 |   # z = cond or other_cond
141 | / if cond:
142 | |     z = cond 
143 | | else:
144 | |     z = other_cond
    | |__________________^
145 |
146 |   # SIM108 - should suggest
    |
help: Replace `if`-`else`-block with `z = cond or other_cond`
    |
140 | # z = cond or other_cond
    - if cond:
    -     z = cond 
    - else:
    -     z = other_cond
141 + z = cond or other_cond
142 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use binary operator `z = cond and other_cond` instead of `if`-`else`-block
   --> SIM108.py:148:1
    |
146 |   # SIM108 - should suggest
147 |   # z = cond and other_cond
148 | / if not cond:
149 | |     z = cond
150 | | else:
151 | |     z = other_cond
    | |__________________^
152 |
153 |   # SIM108 - should suggest
    |
help: Replace `if`-`else`-block with `z = cond and other_cond`
    |
147 | # z = cond and other_cond
    - if not cond:
    -     z = cond
    - else:
    -     z = other_cond
148 + z = cond and other_cond
149 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use binary operator `z = not cond and other_cond` instead of `if`-`else`-block
   --> SIM108.py:155:1
    |
153 |   # SIM108 - should suggest
154 |   # z = not cond and other_cond
155 | / if cond:
156 | |     z = not cond
157 | | else:
158 | |     z = other_cond
    | |__________________^
159 |
160 |   # SIM108 does not suggest
    |
help: Replace `if`-`else`-block with `z = not cond and other_cond`
    |
154 | # z = not cond and other_cond
    - if cond:
    -     z = not cond
    - else:
    -     z = other_cond
155 + z = not cond and other_cond
156 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use ternary operator `z = 1 if True else other` instead of `if`-`else`-block
   --> SIM108.py:167:1
    |
165 |   # (Of course, these specific expressions 
166 |   # should be simplified for other reasons...)
167 | / if True:
168 | |     z = 1
169 | | else:
170 | |     z = other
    | |_____________^
171 |
172 |   if False:
    |
help: Replace `if`-`else`-block with `z = 1 if True else other`
    |
166 | # should be simplified for other reasons...)
    - if True:
    -     z = 1
    - else:
    -     z = other
167 + z = 1 if True else other
168 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use ternary operator `z = 1 if False else other` instead of `if`-`else`-block
   --> SIM108.py:172:1
    |
170 |       z = other
171 |
172 | / if False:
173 | |     z = 1
174 | | else:
175 | |     z = other
    | |_____________^
176 |
177 |   if 1:
    |
help: Replace `if`-`else`-block with `z = 1 if False else other`
    |
171 |
    - if False:
    -     z = 1
    - else:
    -     z = other
172 + z = 1 if False else other
173 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use ternary operator `z = True if 1 else other` instead of `if`-`else`-block
   --> SIM108.py:177:1
    |
175 |       z = other
176 |
177 | / if 1:
178 | |     z = True
179 | | else:
180 | |     z = other
    | |_____________^
181 |
182 |   # SIM108 does not suggest a binary option in this
    |
help: Replace `if`-`else`-block with `z = True if 1 else other`
    |
176 |
    - if 1:
    -     z = True
    - else:
    -     z = other
177 + z = True if 1 else other
178 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use ternary operator `z = foo() if foo() else other` instead of `if`-`else`-block
   --> SIM108.py:185:1
    |
183 |   # case, since we'd be reducing the number of calls
184 |   # from Two to one.
185 | / if foo():
186 | |     z = foo()
187 | | else:
188 | |     z = other
    | |_____________^
189 |
190 |   # SIM108 does not suggest a binary option in this
    |
help: Replace `if`-`else`-block with `z = foo() if foo() else other`
    |
184 | # from Two to one.
    - if foo():
    -     z = foo()
    - else:
    -     z = other
185 + z = foo() if foo() else other
186 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use ternary operator `z = not foo() if foo() else other` instead of `if`-`else`-block
   --> SIM108.py:193:1
    |
191 |   # case, since we'd be reducing the number of calls
192 |   # from Two to one.
193 | / if foo():
194 | |     z = not foo()
195 | | else:
196 | |     z = other
    | |_____________^
    |
help: Replace `if`-`else`-block with `z = not foo() if foo() else other`
    |
192 | # from Two to one.
    - if foo():
    -     z = not foo()
    - else:
    -     z = other
193 + z = not foo() if foo() else other
194 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use ternary operator `var = "str" if cond else f"{first}-{second}"` instead of `if`-`else`-block
   --> SIM108.py:202:1
    |
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}"
    | |_____________________________^
206 |
207 |   if cond:
    |
help: Replace `if`-`else`-block with `var = "str" if cond else f"{first}-{second}"`
    |
201 | # single quotes
    - if cond:
    -     var = "str"
    - else:
    -     var = f"{first}-{second}"
202 + var = "str" if cond else f"{first}-{second}"
203 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM108 [*] Use ternary operator `var = "str" if cond else f'{first}-{second}'` instead of `if`-`else`-block
   --> SIM108.py:207:1
    |
205 |       var = f"{first}-{second}"
206 |
207 | / if cond:
208 | |     var = "str"
209 | | else:
210 | |     var = f'{first}-{second}'
    | |_____________________________^
    |
help: Replace `if`-`else`-block with `var = "str" if cond else f'{first}-{second}'`
    |
206 |
    - if cond:
    -     var = "str"
    - else:
    -     var = f'{first}-{second}'
207 + var = "str" if cond else f'{first}-{second}'
    |
note: This is an unsafe fix and may change runtime behavior
