---
source: crates/ruff/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
  |
2 |   # SIM108
3 | / if a:
4 | |     b = c
5 | | else:
6 | |     b = d
  | |_________^ SIM108
7 |   
8 |   # OK
  |
  = help: Replace `if`-`else`-block with `b = c if a else d`

ℹ Suggested 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:58:1: SIM108 Use ternary operator `abc = x if x > 0 else -x` instead of `if`-`else`-block
   |
58 |   # SIM108 (without fix due to comments)
59 | / if x > 0:
60 | |     # test test
61 | |     abc = x
62 | | else:
63 | |     # test test test
64 | |     abc = -x
   | |____________^ SIM108
   |

SIM108.py:82:1: SIM108 [*] Use ternary operator `b = cccccccccccccccccccccccccccccccccccc if a else ddddddddddddddddddddddddddddddddddddd` instead of `if`-`else`-block
   |
82 |   # SIM108
83 | / if a:
84 | |     b = cccccccccccccccccccccccccccccccccccc
85 | | else:
86 | |     b = ddddddddddddddddddddddddddddddddddddd
   | |_____________________________________________^ SIM108
   |
   = help: Replace `if`-`else`-block with `b = cccccccccccccccccccccccccccccccccccc if a else ddddddddddddddddddddddddddddddddddddd`

ℹ Suggested fix
79 79 | 
80 80 | 
81 81 | # SIM108
82    |-if a:
83    |-    b = cccccccccccccccccccccccccccccccccccc
84    |-else:
85    |-    b = ddddddddddddddddddddddddddddddddddddd
   82 |+b = cccccccccccccccccccccccccccccccccccc if a else ddddddddddddddddddddddddddddddddddddd
86 83 | 
87 84 | 
88 85 | # OK (too long)

SIM108.py:97:1: SIM108 Use ternary operator `exitcode = 0 if True else 1` instead of `if`-`else`-block
    |
 97 |   # SIM108 (without fix due to trailing comment)
 98 | / if True:
 99 | |     exitcode = 0
100 | | else:
101 | |     exitcode = 1  # Trailing comment
    | |________________^ SIM108
    |

SIM108.py:104:1: SIM108 Use ternary operator `x = 3 if True else 5` instead of `if`-`else`-block
    |
104 |   # SIM108
105 | / if True: x = 3  # Foo
106 | | else: x = 5
    | |___________^ SIM108
    |

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


