-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Conversation
ghost
commented
Mar 23, 2019
- This fixes an issue where the notification center observer is appended to an array, which triggers the violation. Fixes Discarded Notification Center Observer Violation false trigger #2684.
- This fixes an issue where the notification center observer is appended to an array, which triggers the violation. Fixes #2684.
There was a problem hiding this 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?
Source/SwiftLintFramework/Rules/Lint/DiscardedNotificationCenterObserverRule.swift
Outdated
Show resolved
Hide resolved
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)
Generated by 🚫 Danger |
There was a problem hiding this 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.
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. |
Looks good to me now. Thank you very much! Merging. 🎉 |
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, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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