---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
SIM108.py:2:1: SIM108 [*] Use ternary operator `b = c if a else d` instead of `if`-`else`-block
  |
1 |   # SIM108
2 | / if a:
3 | |     b = c
4 | | else:
5 | |     b = d
  | |_________^ SIM108
6 |   
7 |   # OK
  |
  = help: Replace `if`-`else`-block with `b = c if a else d`

ℹ Unsafe fix
1 1 | # SIM108
2   |-if a:
3   |-    b = c
4   |-else:
5   |-    b = d
  2 |+b = c if a else d
6 3 | 
7 4 | # OK
8 5 | b = c if a else d

SIM108.py:30:5: SIM108 [*] Use ternary operator `b = 1 if a else 2` instead of `if`-`else`-block
   |
28 |       pass
29 |   else:
30 |       if a:
   |  _____^
31 | |         b = 1
32 | |     else:
33 | |         b = 2
   | |_____________^ SIM108
   |
   = help: Replace `if`-`else`-block with `b = 1 if a else 2`

ℹ Unsafe fix
27 27 | if True:
28 28 |     pass
29 29 | else:
30    |-    if a:
31    |-        b = 1
32    |-    else:
33    |-        b = 2
   30 |+    b = 1 if a else 2
34 31 | 
35 32 | 
36 33 | import sys

SIM108.py:58:1: SIM108 Use ternary operator `abc = x if x > 0 else -x` instead of `if`-`else`-block
   |
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
   | |____________^ SIM108
   |
   = help: Replace `if`-`else`-block with `abc = x if x > 0 else -x`

SIM108.py:82:1: SIM108 [*] Use ternary operator `b = "cccccccccccccccccccccccccccccccccß" if a else "ddddddddddddddddddddddddddddddddd💣"` instead of `if`-`else`-block
   |
81 |   # SIM108
82 | / if a:
83 | |     b = "cccccccccccccccccccccccccccccccccß"
84 | | else:
85 | |     b = "ddddddddddddddddddddddddddddddddd💣"
   | |_____________________________________________^ SIM108
   |
   = help: Replace `if`-`else`-block with `b = "cccccccccccccccccccccccccccccccccß" if a else "ddddddddddddddddddddddddddddddddd💣"`

ℹ Unsafe fix
79 79 | 
80 80 | 
81 81 | # SIM108
82    |-if a:
83    |-    b = "cccccccccccccccccccccccccccccccccß"
84    |-else:
85    |-    b = "ddddddddddddddddddddddddddddddddd💣"
   82 |+b = "cccccccccccccccccccccccccccccccccß" if a else "ddddddddddddddddddddddddddddddddd💣"
86 83 | 
87 84 | 
88 85 | # OK (too long)

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

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

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


