Void Function in Ternary

Using ternary to call Void functions should be avoided

  • Identifier: void_function_in_ternary
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: idiomatic
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.1.0
  • Default configuration:
    KeyValue
    severity warning

Non Triggering Examples

let result = success ? foo() : bar()
if success {
    askQuestion()
} else {
    exit()
}
var price: Double {
    return hasDiscount ? calculatePriceWithDiscount() : calculateRegularPrice()
}
foo(x == 2 ? a() : b())
chevronView.image = collapsed ? .icon(.mediumChevronDown) : .icon(.mediumChevronUp)
array.map { elem in
    elem.isEmpty() ? .emptyValue() : .number(elem)
}
func compute(data: [Int]) -> Int {
    data.isEmpty ? 0 : expensiveComputation(data)
}
var value: Int {
    mode == .fast ? fastComputation() : expensiveComputation()
}
var value: Int {
    get {
        mode == .fast ? fastComputation() : expensiveComputation()
    }
}
subscript(index: Int) -> Int {
    get {
        index == 0 ? defaultValue() : compute(index)
    }
subscript(index: Int) -> Int {
    index == 0 ? defaultValue() : compute(index)

Triggering Examples

success ? askQuestion() : exit()
perform { elem in
    elem.isEmpty() ? .emptyValue() : .number(elem)
    return 1
}
DispatchQueue.main.async {
    self.sectionViewModels[section].collapsed.toggle()
    self.sectionViewModels[section].collapsed
        ? self.tableView.deleteRows(at: [IndexPath(row: 0, section: section)], with: .automatic)
        : self.tableView.insertRows(at: [IndexPath(row: 0, section: section)], with: .automatic)
    self.tableView.scrollToRow(at: IndexPath(row: NSNotFound, section: section), at: .top, animated: true)
}
subscript(index: Int) -> Int {
    index == 0 ? something() : somethingElse(index)
    return index
var value: Int {
    mode == .fast ? something() : somethingElse()
    return 0
}
var value: Int {
    get {
        mode == .fast ? something() : somethingElse()
        return 0
    }
}
subscript(index: Int) -> Int {
    get {
        index == 0 ? something() : somethingElse(index)
        return index
    }