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
jsloop42 opened this issue Mar 23, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@jsloop42
Copy link
Contributor

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!) }
    }
}

jsloop42 added a commit to jsloop42/SwiftLint that referenced this issue Mar 23, 2019

Update discarded notification center observer
- This fixes an issue where the notification center observer is appended to an array, which triggers the violation. Fixes realm#2684.

jsloop42 added a commit to jsloop42/SwiftLint that referenced this issue Mar 23, 2019

jsloop42 added a commit to jsloop42/SwiftLint that referenced this issue Mar 27, 2019

Update rule to match any function
- Updated rule to not trigger if notification observer is passed to any function as argument (realm#2684)

jsloop42 added a commit to jsloop42/SwiftLint that referenced this issue Mar 27, 2019

jsloop42 added a commit to jsloop42/SwiftLint that referenced this issue Mar 27, 2019

@Dschee Dschee closed this in #2685 Mar 27, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.