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

class_delegate_protocol should accept AnyObject #1261

Closed
jshier opened this issue Jan 30, 2017 · 9 comments
Closed

class_delegate_protocol should accept AnyObject #1261

jshier opened this issue Jan 30, 2017 · 9 comments
Labels
enhancement Ideas for improvements of existing features and rules.

Comments

@jshier
Copy link
Contributor

jshier commented Jan 30, 2017

Using AnyObject instead of class gives the protocol the same reference requirement and is future proof against the discussed removal of the class keyword. It shouldn't trigger the warning.

@marcelofabri
Copy link
Collaborator

I didn't know you could use AnyObject 😲

@jshier Would you like to send a PR?

@marcelofabri marcelofabri added the enhancement Ideas for improvements of existing features and rules. label Jan 30, 2017
@jshier
Copy link
Contributor Author

jshier commented Jan 30, 2017

@marcelofabri I've never worked with this codebase before, so what would you recommend I do? I've found the rule (I think it should be renamed to reference_delegate_protocol but that might be too invasive) but I'm completely unfamiliar with the rule implementation, so I can just guess how to implement the change. Is there some documentation about writing rules I should read?

@jshier
Copy link
Contributor Author

jshier commented Jan 30, 2017

Thinking about this more, it seems like the rule also needs to account for inheriting from a protocol that requires reference semantics. Currently it doesn't check if the protocol inherits at all, so this seems like it would be a major change.

@marcelofabri
Copy link
Collaborator

I think it should be renamed to reference_delegate_protocol but that might be too invasive.

I don't think we should rename it by now.

It seems like the rule also needs to account for inheriting from a protocol that requires reference semantics. Currently it doesn't check if the protocol inherits at all, so this seems like it would be a major change.

Actually, it does check the inherited protocol: https://github.com/realm/SwiftLint/blob/master/Source/SwiftLintFramework/Rules/ClassDelegateProtocolRule.swift#L56. But it only checks for *Delegate to avoid false positives on UIKit's delegate protocols.
We currently have no way to check if another protocol requires reference semantics, so we've went with this by now. It's probably just a matter of looking for AnyClass in the list. We probably should whitelist NSObjectProtocol as well.

@jshier
Copy link
Contributor Author

jshier commented Jan 31, 2017

Without being able to detect whether the inherited protocol inherits from one that guarantees reference semantics (or even just the class keyword, since I don't know if that's considered inheritance), I think this warning will have predictable false positives, so I question its utility.

That said, I can look into making a set of accepted base types that guarantee reference semantics, but it likely won't be exhaustive, especially since inheriting from any Apple protocol does the same thing. So, I can do the base types but that's hardly exhaustive.

@marcelofabri
Copy link
Collaborator

I'm not too worried about false positives, since the rule only validates *Delegate protocols and allows inheritance from other *Delegate protocol. You can always add the class keyword to mute the warning (or use the comment-style way).

My guess is that 95%+ of the cases where a Delegate inherits from another protocol, this protocol either:

  1. is another Delegate (possibly from UIKit)
  2. is NSObjectProtocol because of Objective-C semantics
  3. is AnyClass

@jshier
Copy link
Contributor Author

jshier commented Jan 31, 2017

I've been able to make the changes (essentially just changing the class protocol regex: "\\b(class|AnyObject|NSObjectProtocol)\\b"). However, using make install and then running the installer generated doesn't show any change in output. How would you suggest I debug this?

@jshier
Copy link
Contributor Author

jshier commented Feb 1, 2017

Opened #1265 to fix this.

@marcelofabri
Copy link
Collaborator

Fixed in #1279

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Ideas for improvements of existing features and rules.
Projects
None yet
Development

No branches or pull requests

2 participants