SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Add IfSwitchExpressionRule for enforcing if and switch statements. In…

Open AballahNsour opened this issue 1 year ago • 4 comments

…clude non-triggering and triggering examples.

Add IfSwitchExpressionRule for enforcing if and switch statements. Include non-triggering and triggering examples.

AballahNsour avatar May 17 '24 21:05 AballahNsour

1 Error
:no_entry_sign: Could not build branch
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.

Here's an example of your CHANGELOG entry:

* Add IfSwitchExpressionRule for enforcing if and switch statements. In….  
  [AballahNsour](https://github.com/AballahNsour)
  [#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 May 17 '24 21:05 SwiftLintBot

Hi @AballahNsour! Your examples are good, but the implementation of the rule is definitely not sufficient. Are you going to continue on this PR?

SimplyDanny avatar Mar 26 '25 08:03 SimplyDanny

hi @SimplyDanny , If you can give more guidance I can continue working on this RP. if not I not continue working on it

AballahNsour avatar Mar 31 '25 16:03 AballahNsour

So if we start with if statements only, we expect

func f(value: Bool) -> Int {
  if value {
    return 1
  } else {
    return 2
  }
}

to trigger the rule as it can be rewritten to

func f(value: Bool) -> Int {
  return if value {
    1
  } else {
    2
  }
}

or even

func f(value: Bool) -> Int {
  if value {
    1
  } else {
    2
  }
}

if one prefers implicit returns. Is that correct?

Your current implementation triggers on each and every if statement/expression, though. You need to add more logic to find the correct cases on which the rule shall trigger on considering the different examples you have already provided.

I have to say that his is not an easy rule to implement. Hence, it's fine for me if you disregard it. At the moment, though, I cannot see at least a direction of where this is heading given the rudimentary implementation.

SimplyDanny avatar Mar 31 '25 16:03 SimplyDanny