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

False positive in DiscardedNotificationCenterObserverRule #1525

Closed
sammy-SC opened this issue May 16, 2017 · 3 comments
Closed

False positive in DiscardedNotificationCenterObserverRule #1525

sammy-SC opened this issue May 16, 2017 · 3 comments
Labels
enhancement Ideas for improvements of existing features and rules.

Comments

@sammy-SC
Copy link
Contributor

sammy-SC commented May 16, 2017

Following code is labelled with warning even thought the opaque observer is getting preserved.

return nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })\n
@marcelofabri marcelofabri added the enhancement Ideas for improvements of existing features and rules. label May 17, 2017
@marcelofabri
Copy link
Collaborator

This could be hard to fix, since there's no guarantee that the places that call this method will retain the observer object.

@sammy-SC
Copy link
Contributor Author

@marcelofabri I believe it will be sufficient to check whether the function returning observer doesn't have @discardableResult prefix. Swift compiler will do the rest for us giving Result of call ... warnings.

@marcelofabri
Copy link
Collaborator

That's a good idea! It should be possible, since we get the attribute from SourceKit:

file.swift

@discardableResult
func foo() -> Int{
    return 0
}

$ sourcekitten structure --file file.swift

{
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.substructure" : [
    {
      "key.offset" : 19,
      "key.nameoffset" : 24,
      "key.bodyoffset" : 37,
      "key.accessibility" : "source.lang.swift.accessibility.internal",
      "key.length" : 33,
      "key.name" : "foo()",
      "key.kind" : "source.lang.swift.decl.function.free",
      "key.bodylength" : 14,
      "key.attributes" : [
        {
          "key.attribute" : "source.decl.attribute.discardableResult"
        }
      ],
      "key.namelength" : 5
    }
  ],
  "key.offset" : 0,
  "key.length" : 53
}

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