SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Fix void_function_in_ternary incorrectly triggered with if statement

Open yuya-okuse opened this issue 1 year ago • 3 comments
trafficstars

Fixes #5611

This PR corrects that VoidFunctionInTernaryConditionRule incorrectly triggered with if statement. This new rule is triggered or not depending on the return type of the function.

yuya-okuse avatar Jul 15 '24 12:07 yuya-okuse

2 Warnings
:warning: If this is a user-facing change, please include a CHANGELOG entry to credit yourself!
You can find it at CHANGELOG.md.
:warning: This PR may need tests.
17 Messages
:book: Linting Aerial with this PR took 0.16s vs 0.16s on main (0% slower)
:book: Linting Alamofire with this PR took 0.16s vs 0.16s on main (0% slower)
:book: Linting Brave with this PR took 0.61s vs 0.61s on main (0% slower)
:book: Linting DuckDuckGo with this PR took 0.48s vs 0.48s on main (0% slower)
:book: Linting Firefox with this PR took 1.35s vs 1.37s on main (1% faster)
:book: Linting Kickstarter with this PR took 0.74s vs 0.74s on main (0% slower)
:book: Linting Moya with this PR took 0.12s vs 0.12s on main (0% slower)
:book: Linting NetNewsWire with this PR took 0.28s vs 0.28s on main (0% slower)
:book: Linting Nimble with this PR took 0.14s vs 0.14s on main (0% slower)
:book: Linting PocketCasts with this PR took 0.68s vs 0.65s on main (4% slower)
:book: Linting Quick with this PR took 0.12s vs 0.12s on main (0% slower)
:book: Linting Realm with this PR took 0.41s vs 0.41s on main (0% slower)
:book: Linting Sourcery with this PR took 0.27s vs 0.27s on main (0% slower)
:book: Linting Swift with this PR took 0.39s vs 0.39s on main (0% slower)
:book: Linting VLC with this PR took 0.19s vs 0.19s on main (0% slower)
:book: Linting Wire with this PR took 1.6s vs 1.58s on main (1% slower)
:book: Linting WordPress with this PR took 1.05s vs 1.03s on main (1% slower)

Here's an example of your CHANGELOG entry:

* Fix void_function_in_ternary incorrectly triggered with if statement.  
  [u-abyss](https://github.com/u-abyss)
  [#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)

note: There are two invisible spaces after the entry's text.

Generated by :no_entry_sign: Danger

SwiftLintBot avatar Jul 15 '24 12:07 SwiftLintBot

@SimplyDanny Added handling of nested if and switch statements. I have a question. I believe that when you initialize a variable in an if or switch expression, the compiler prevents you from writing a statement that returns void in it. If there is an appropriate case for initializing a variable in an if or switch expression, I would appreciate it if you could let me know.

yuya-okuse avatar Oct 12 '24 08:10 yuya-okuse

@SimplyDanny Added handling of nested if and switch statements. I have a question. I believe that when you initialize a variable in an if or switch expression, the compiler prevents you from writing a statement that returns void in it. If there is an appropriate case for initializing a variable in an if or switch expression, I would appreciate it if you could let me know.

Can you provide an example?

The code

func f(i: Int) -> Int {
  if i > 1 {
    let j = i - 1
    j
  } else {
    0
  }
}

wouldn't compile. There you need the return before j.

SimplyDanny avatar Oct 13 '24 11:10 SimplyDanny