Swiftlint: Class Delegate Protocol rule conflicting with Xcode 9.1 warnings

Created on 8 Nov 2017  路  10Comments  路  Source: realm/SwiftLint

New Issue Checklist

Bug Report

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint
Protocols.swift:16:1: warning: Test Violation: Delegate protocols should be class-only so they can be weakly referenced. (class_delegate_protocol)

Environment

  • SwiftLint version (run swiftlint version to be sure)?

0.23.1

  • Installation method used (Homebrew, CocoaPods, building from source, etc)?

Brew

  • Paste your configuration file:

No

  • Which Xcode version are you using (check xcode-select -p)?

9.1

  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
// This triggers a violation:
protocol A: class {}

protocol BDelegate: A {}

Description

In the example above this triggers a violation as expected. The suggested way to fix this would be to change the code to:

// This does not trigger a violation
protocol A: class {}

protocol BDelegate: class, A {}

However this invokes a new warning in Xcode 9.1:

warning: redundant layout constraint 'Self' : 'AnyObject'
protocol BDelegate: class, A {

So it's not possible to satisfy both the Swift requirement and the Swiftlint rule unless the dependent protocol is renamed to *Delegate. This seems more restrictive than this rule was originally intended to be.

1856 is similar but appears to have addressed a slightly different issue.

wontfix

Most helpful comment

I'm not 100% sure about this, but I feel the warning is triggered without protocol inheritance.

// This triggers a violation "Delegate protocols should be class-only so they can be weakly referenced."
protocol MyClassDelegate: class {}
class MyClass: NSObject {
    weak var delegate: MyClassDelegate?
}

This might have been fixed recently, but still shows up for Danger running swiftlint (checking the version now).

All 10 comments

I have the same issue with Xcode version 9.1. I had to disable the rule in order to stop getting warnings that didn't appear on Xcode 9.0.

Hope to get a solution.

Same for me, only happens on Xcode 9.1.

Same for me too, only happens for Xcode 9.1.

PRs welcome.

The only solution I could come up with would be adding an extra configuration to explicitly allow certain protocols inheritance. This is not ideal, but at least would avoid the warning.

I'm not 100% sure about this, but I feel the warning is triggered without protocol inheritance.

// This triggers a violation "Delegate protocols should be class-only so they can be weakly referenced."
protocol MyClassDelegate: class {}
class MyClass: NSObject {
    weak var delegate: MyClassDelegate?
}

This might have been fixed recently, but still shows up for Danger running swiftlint (checking the version now).

@eneko That didn't trigger a warning for me.

@eneko Your case also triggers warnings for me when run from Danger on CI but not locally. I'm using the latest versions of Danger and SwiftLint on CI and locally.

Ok, so it might have been fixed recently, I would assume the danger-swiftlint gem has not been updated yet. Thanks!

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

Was this page helpful?
0 / 5 - 0 ratings