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 explicit_type_interface compatible with redundant_type_annotation #2312

Closed
Jeehut opened this issue Jul 23, 2018 · 7 comments
Closed

Make explicit_type_interface compatible with redundant_type_annotation #2312

Jeehut opened this issue Jul 23, 2018 · 7 comments
Labels
enhancement Ideas for improvements of existing features and rules.

Comments

@Jeehut
Copy link
Collaborator

Jeehut commented Jul 23, 2018

Currently when the two rules explicit_type_interface and redundant_type_annotation are active, the most common way of writing a Singleton doesn't work any more. The following violates the rule explicit_type_interface:

final class Logger {
    // MARK: - Stored Type Properties
    static let shared = Logger() // <-- Explicit Type Interface Violation: Properties should have a type interface (explicit_type_interface)

    // MARK: - Instance Properties
    func setup() {
         // some code
    }
}

When fixing it by explicitly stating the type, the code violates the rule redundant_type_annotation:

final class Logger {
    // MARK: - Stored Type Properties
    static let shared = Logger() // <-- Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)

    // MARK: - Instance Properties
    func setup() {
         // some code
    }
}

While this could be fixed both ways, I think the rule explicit_type_interface is to be fixed here. Since the goal of it is to make clear what the type is and the type is the next thing right after the = sign, I think this can be seen as a false positive. One could also make it an option, but since Singletons are very common and there's no real added value (correct me if I'm wrong) in stating the type explicitly again if the next thing to be seen is the exact same thing (that's exactly the point of the rule redundant_type_annotation), I think we should make it non-violating by default.

@marcelofabri
Copy link
Collaborator

IMO those rules are completely incompatible with each other by definition. Why do want to enable both? 🤔

@Jeehut
Copy link
Collaborator Author

Jeehut commented Jul 23, 2018

IMO redundant_type_annotation is only to prevent listing the type twice (< 2) and explicit_type_interface on the other hand is here to make sure the type is listed at least once (>= 1). Both together ensure that the type is listed exactly once (== 1) – that's the use case.

@marcelofabri
Copy link
Collaborator

IMO explicit_type_interface is for checking that every declaration has an explicit type (before the assignment), which can be used for improving legibility and also compiler performance.

@Jeehut
Copy link
Collaborator Author

Jeehut commented Jul 23, 2018

Exactly. And I don't need it (for legibility) if the type is redundant, nor does the compiler, AFAIK.

@ornithocoder
Copy link
Contributor

The compiler still needs it for performance. It doesn't know it's redundant.

@Jeehut
Copy link
Collaborator Author

Jeehut commented Jul 24, 2018

Yeah, for sure. I just thought it's like explicit types and initializers for implicit type checking are both O(1) but more complicated type checking can get to O(n) and beyond. But that's just a gut feeling, I have no idea if it's actually the case.

But since you both seem not to be very understanding of combining those rules, I'll rather make this a configuration option. This way people who want to use both can opt-in to do so.

@marcelofabri marcelofabri added the enhancement Ideas for improvements of existing features and rules. label Jul 24, 2018
@kubalani
Copy link

This is reintroduced with 0.43 version for Bool warnings.

https://github.com/realm/SwiftLint/pull/3438/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Ideas for improvements of existing features and rules.
Projects
None yet
Development

No branches or pull requests

4 participants