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

Rule Request: check for method existence in #selector #2084

Open
2 tasks done
sammy-SC opened this issue Mar 4, 2018 · 3 comments
Open
2 tasks done

Rule Request: check for method existence in #selector #2084

sammy-SC opened this issue Mar 4, 2018 · 3 comments

Comments

@sammy-SC
Copy link
Contributor

sammy-SC commented Mar 4, 2018

New Issue Checklist

Rule Request

If this is a new rule request, please ignore all sections below this one, format
this issue's title as Rule Request: [Rule Name] and describe:

Describe:
#selector() doesn't warn you if you reference method from different class. In class A you could be adding target to button button.addTarget(self, action: #selector(B.didTap), for: .touchUpInside) and as long as B.didTap is accessible this code compiles but on tap of the button crashes the app. I would keep this rule simple and it would only check for existence of method in case target is being added to self.

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

I don't have link to an existing discussion.

  1. Provide several examples of what would and wouldn't trigger violations.

What would trigger:

final class A: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let button = UIButton()
    button.addTarget(self,
                                   action: #selector(B.didTap),
                                   for: .touchUpInside)
  }
}

what wouldn't trigger:

final class A: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let button = UIButton()
    button.addTarget(self,
                                   action: #selector(A.didTap),
                                   for: .touchUpInside)
  }
}
  1. Should the rule be configurable, if so what parameters should be configurable?

This rule should not be configurable.

  1. Should the rule be opt-in or enabled by default? Why?
    See README.md for guidelines on when to mark a
    rule as opt-in.

I believe this rule should be enabled by default.

@marcelofabri
Copy link
Collaborator

Should this only work with UIControl APIs? What about other classes that use target-action (e.g. gesture recognizers)?

@sammy-SC
Copy link
Contributor Author

@marcelofabri you are right, it should work with any class that uses target-action.

However I realised one problem with implementation, what if the method in action is in subclass? That way the method is invisible to SwiftLint but won't crash the application. Is there a way to work around this?

@marcelofabri
Copy link
Collaborator

@sammy-SC I don’t think that’s a major use case. The rule should be opt-in anyways, so I’m fine with some false positives.

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

No branches or pull requests

2 participants