Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IfSwitchExpressionRule for enforcing if and switch statements. In… #5595

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AballahNsour
Copy link

…clude non-triggering and triggering examples.

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

…clude non-triggering and triggering examples.

Add IfSwitchExpressionRule for enforcing if and switch statements. Include non-triggering and triggering examples.
@SwiftLintBot
Copy link

1 Error
🚫 Could not build branch
2 Warnings
⚠️ If this is a user-facing change, please include a CHANGELOG entry to credit yourself!
You can find it at CHANGELOG.md.
⚠️ 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 🚫 Danger

@SimplyDanny
Copy link
Collaborator

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 SimplyDanny marked this pull request as draft March 26, 2025 08:15
@AballahNsour
Copy link
Author

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

@SimplyDanny
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants