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

Update Discarded Notification Center Observer Violation false positive trigger on array append #2685

Merged
merged 5 commits into from Mar 27, 2019

Conversation

ghost
Copy link

@ghost ghost commented Mar 23, 2019

- This fixes an issue where the notification center observer is appended to an array, which triggers the violation. Fixes #2684.
Copy link
Collaborator

@Jeehut Jeehut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for providing a fix for this. But as I've written in my comments, I don't think we should merge this as is but rather provide a broader solution to the said problem while we're at it. Would you mind considering my suggestions?

CHANGELOG.md Outdated Show resolved Hide resolved
@ghost
Copy link
Author

ghost commented Mar 27, 2019

Okay, I will update the PR to make it more generic.

- Updated rule to not trigger if notification observer is passed to any function as argument (#2684)
@SwiftLintBot
Copy link

SwiftLintBot commented Mar 27, 2019

12 Messages
📖 Linting Aerial with this PR took 1.96s vs 1.81s on master (8% slower)
📖 Linting Alamofire with this PR took 4.57s vs 4.04s on master (13% slower)
📖 Linting Firefox with this PR took 14.09s vs 12.31s on master (14% slower)
📖 Linting Kickstarter with this PR took 21.85s vs 20.39s on master (7% slower)
📖 Linting Moya with this PR took 1.86s vs 1.85s on master (0% slower)
📖 Linting Nimble with this PR took 1.78s vs 1.74s on master (2% slower)
📖 Linting Quick with this PR took 0.56s vs 0.56s on master (0% slower)
📖 Linting Realm with this PR took 3.58s vs 3.4s on master (5% slower)
📖 Linting SourceKitten with this PR took 1.2s vs 1.13s on master (6% slower)
📖 Linting Sourcery with this PR took 4.1s vs 4.07s on master (0% slower)
📖 Linting Swift with this PR took 26.86s vs 26.54s on master (1% slower)
📖 Linting WordPress with this PR took 21.57s vs 21.49s on master (0% slower)

Generated by 🚫 Danger

Copy link
Collaborator

@Jeehut Jeehut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see that more triggering and non-triggering examples were added with different ways of using the notification as parameter. Specifically, there's no array literal or dictionary literal example.

CHANGELOG.md Outdated Show resolved Hide resolved
@ghost
Copy link
Author

ghost commented Mar 27, 2019

I have added non-triggering example for array append and passing as function call argument. I will add an example on dictionary. The triggering example is the same.

@Jeehut
Copy link
Collaborator

Jeehut commented Mar 27, 2019

Looks good to me now. Thank you very much! Merging. 🎉

@Jeehut Jeehut merged commit 020d0d1 into realm:master Mar 27, 2019
@ghost
Copy link
Author

ghost commented Mar 27, 2019

Welcome :)

@@ -51,6 +61,11 @@ public struct DiscardedNotificationCenterObserverRule: ASTRule, ConfigurationPro
return []
}

if let lastMatch = regex("\\b[^\\(]+").matches(in: file.contents, options: [], range: range).last?.range,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this cause false negatives? e.g.

print(nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { }))

* `discarded_notification_center_observer` rule now checks if the observer is
added to any collection or passed to a function before triggering the violation.
[jsloop42](https://github.com/jsloop42)
[#2684](https://github.com/realm/SwiftLint/issues/2684)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should update this to mention #1398 which is older

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.

Discarded Notification Center Observer Violation false trigger
4 participants