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

Make comma_inheritance opt-in #4047

Merged

Conversation

sjmadsen
Copy link
Contributor

This addresses issue #4027.

The Swift language grammar doesn't allow a comma in place of an ampersand in all cases. Specifically, if a protocol's associated type is required to conform to a composition of protocols and one of those protocols in turn requires conformance to one of the same protocols, the compiler will generate an error: "redundant conformance constraint". For example:

protocol Transformable {
    associatedtype Unit: Equatable & CaseIterable & UnitConvertible

    ...
}

protocol UnitConvertible where Self: Equatable {
    ...
}

When & is replaced with ,, this produces Redundant conformance constraint: 'Self.Unit' : 'Equatable'.

My first thought was to disable autocorrect, but this penalizes those that want to use the rule without solving the problem for problematic code bases. It feels better to make the rule opt-in, so that developers who want to enforce the rule can still take advantage of autocorrection, while code bases where it is an issue can either leave it disabled or opt-in and selectively disable where necessary.

The Swift language grammar doesn't allow a comma in place of an ampersand in all cases. Specifically, if a protocol's associated type is required to conform to a composition of protocols and one of those protocols in turn requires conformance to one of the same protocols, the compiler will generate an error: "redundant conformance constraint". For example:

    protocol Transformable {
        associatedtype Unit: Equatable & CaseIterable & UnitConvertible

        ...
    }

    protocol UnitConvertible where Self: Equatable {
        ...
    }

If comma_inheritance is enabled by default, autocorrection can introduce a new compile error. Making the rule opt-in allows code bases where it isn't a problem to opt-in and continue to use autocorrect, while code bases where it is a problem can leave it disabled.
@SwiftLintBot
Copy link

SwiftLintBot commented Jul 26, 2022

12 Messages
📖 Linting Aerial with this PR took 0.38s vs 0.35s on master (8% slower)
📖 Linting Alamofire with this PR took 0.4s vs 0.4s on master (0% slower)
📖 Linting Firefox with this PR took 1.83s vs 1.84s on master (0% faster)
📖 Linting Kickstarter with this PR took 2.72s vs 2.73s on master (0% faster)
📖 Linting Moya with this PR took 0.17s vs 0.17s on master (0% slower)
📖 Linting Nimble with this PR took 0.17s vs 0.17s on master (0% slower)
📖 Linting Quick with this PR took 0.08s vs 0.08s on master (0% slower)
📖 Linting Realm with this PR took 1.34s vs 1.37s on master (2% faster)
📖 Linting SourceKitten with this PR took 0.15s vs 0.15s on master (0% slower)
📖 Linting Sourcery with this PR took 0.54s vs 0.54s on master (0% slower)
📖 Linting Swift with this PR took 1.02s vs 1.02s on master (0% slower)
📖 Linting WordPress with this PR took 3.02s vs 3.02s on master (0% slower)

Generated by 🚫 Danger

CHANGELOG.md Outdated Show resolved Hide resolved
@sjmadsen sjmadsen requested a review from SimplyDanny July 27, 2022 14:45
@SimplyDanny SimplyDanny merged commit 4d8abec into realm:master Jul 27, 2022
@SimplyDanny
Copy link
Collaborator

Thank you!

@jpsim
Copy link
Collaborator

jpsim commented Jul 27, 2022

Shouldn't the right fix here be to avoid reporting violations for associated types?

@sjmadsen sjmadsen deleted the disable-comma_inheritance-autocorrect branch July 27, 2022 15:23
@SimplyDanny
Copy link
Collaborator

Shouldn't the right fix here be to avoid reporting violations for associated types?

There is no difference between associated types and protocol definitions. In the example

protocol UnitConvertible where Self: Equatable {}

protocol Transformable: Equatable, UnitConvertible {   // ⚠️
    associatedtype Unit: Equatable, UnitConvertible    // ⚠️
}

we get the same message from the compiler at both definitions. But: The messages are only warnings, as I see now. So I wonder why the SwiftLint rule and its fix does anything wrong here. Isn't it helpful to see that there are redundant conformances?

@sjmadsen
Copy link
Contributor Author

Now that you say that, I'm pretty sure I have "warnings as errors" turned on. I still don't think it's great for SwiftLint's default configuration to potentially introduce compiler warnings into a codebase when --fix is run.

@SimplyDanny
Copy link
Collaborator

Well, I think introducing this kind of warning is fine. It is not caused by SwiftLint doing anything wrong but is just made visible to the compiler.

What is your view on this, @jpsim?

@jpsim
Copy link
Collaborator

jpsim commented Aug 29, 2022

Yeah there's no hard policy, the obvious ideal would be for SwiftLint to never have any false positives, any false negatives and never correct violations to cause previously compiling code to not compile.

However, we're limited by operating on the pre-typechecked AST in lint rules, so we should strike a balance between offering broadly useful rules that as much as possible do the right thing.

If the false-positive rate is too high and can't easily be reduced, then marking a rule as opt-in seems like a reasonable compromise to me.

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 this pull request may close these issues.

4 participants