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

Discarded Notification Center Observer Violation false trigger #2684

Closed
2 tasks done
ghost opened this issue Mar 23, 2019 · 0 comments · Fixed by #2685
Closed
2 tasks done

Discarded Notification Center Observer Violation false trigger #2684

ghost opened this issue Mar 23, 2019 · 0 comments · Fixed by #2685

Comments

@ghost
Copy link

ghost commented Mar 23, 2019

New Issue Checklist

  • Updated SwiftLint to the latest version
  • I searched for existing GitHub issues

Describe the bug

Discarded Notification Center Observer Violation is triggered when I use the below code pattern where the notification observer result is appended to an array instead of assigning it to a variable.

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint App/NotificationController.swift 
Loading configuration from '.swiftlint.yml'
Linting Swift files at paths App/NotificationController.swift
Linting 'NotificationController.swift' (1/1)
/Users/jsloop/dev/App/App/NotificationController.swift:34:27: warning: Discarded Notification Center Observer Violation: When registering for a notification using a block, the opaque observer that is returned should be stored so it can be removed later. (discarded_notification_center_observer)
/Users/jsloop/dev/App/App/NotificationController.swift:36:27: warning: Discarded Notification Center Observer Violation: When registering for a notification using a block, the opaque observer that is returned should be stored so it can be removed later. (discarded_notification_center_observer)
Done linting! Found 2 violations, 0 serious in 1 file.

Environment

  • SwiftLint version: 0.31.0
  • Installation method used: Hombrew
  • Paste your configuration file:
excluded:
- Carthage
- Pods
- SwiftLint/Common/3rdPartyLib

disabled_rules:
- trailing_whitespace
- identifier_name
- type_body_length
- large_tuple
- cyclomatic_complexity

opt_in_rules:
- unneeded_parentheses_in_closure_argument

force_cast: warning

line_length:
  warning: 160

file_length:
  warning: 1000
  
function_body_length:
  warning: 100
  error: 200

reporter: "xcode"
  • Which Xcode version are you using: 10.1
  • Do you have a sample that shows the issue?
// This triggers a violation:
class NotificationController {
    private var notifs: [Any?] = []

    init() {
        notifs.append(NotificationCenter.default.addObserver(forName: Notification.Name("n1"),
                                                             object: nil, queue: nil, using: {_ in}))
        notifs.append(NotificationCenter.default.addObserver(forName: Notification.Name("n2"), 
                                                            object: nil, queue: nil, using: {_ in}))
    }
}

If I assign the return to a variable, the violation is not triggered.

// This *does not* trigger a violation:
class NotificationController {
    private var notifs: [Any?] = []

    init() {
        let n1 = NotificationCenter.default.addObserver(forName: Notification.Name("n1"),
                                                        object: nil, queue: nil, using: {_ in})
        let n2 = NotificationCenter.default.addObserver(forName: Notification.Name("n2"), 
                                                        object: nil, queue: nil, using: {_ in})
        _ = [n1, n2].map { notifs.append($0!) }
    }
}
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 a pull request may close this issue.

0 participants