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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#2628](https://github.com/realm/SwiftLint/issues/2628)

* `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


## 0.31.0: Busy Laundromat

#### Breaking
Expand Down
26 changes: 26 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -3078,6 +3078,32 @@ func foo() -> Any {

```

```swift
var obs: [Any?] = []
obs.append(nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { }))

```

```swift
var obs: [String: Any?] = []
obs["foo"] = nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })

```

```swift
var obs: [Any?] = []
obs.append(nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { }))

```

```swift
func foo(_ notif: Any) {
obs.append(notif)
}
foo(nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { }))

```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ public struct DiscardedNotificationCenterObserverRule: ASTRule, ConfigurationPro
"let foo = nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })\n",
"func foo() -> Any {\n" +
" return nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })\n" +
"}\n"
"}\n",
"var obs: [Any?] = []\n" +
"obs.append(nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { }))\n",
"var obs: [String: Any?] = []\n" +
"obs[\"foo\"] = nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { })\n",
"var obs: [Any?] = []\n" +
"obs.append(nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { }))\n",
"func foo(_ notif: Any) {\n" +
" obs.append(notif)\n" +
"}\n" +
"foo(nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: { }))\n"
],
triggeringExamples: [
"↓nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil) { }\n",
Expand Down Expand Up @@ -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: { }))

lastMatch.location == range.length - lastMatch.length - 1 {
return []
}

if let lastMatch = regex("\\s?=\\s*").matches(in: file.contents, options: [], range: range).last?.range,
lastMatch.location == range.length - lastMatch.length {
return []
Expand Down