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

explicit_init - creating an instance from a meta-type #984

Closed
evgeniyd opened this issue Dec 15, 2016 · 6 comments
Closed

explicit_init - creating an instance from a meta-type #984

evgeniyd opened this issue Dec 15, 2016 · 6 comments
Labels
enhancement Ideas for improvements of existing features and rules. wontfix Issues that became stale and were auto-closed by a bot.

Comments

@evgeniyd
Copy link

Creating an instance from meta-types via explicit init() is a common idiom. Currently, swiftlint highlights instructions like this when explicit_init rule is opted-in.

struct SomeStruct {
    var a: Int = 0
}

class SomeClass {
    var a: Int = 0
    
    required init() {
        a = 1
    }
}

let metaType = SomeClass.self // SomeClass.Type
let instance = metaType.init()

let structMetaType =  SomeStruct.self // SomeStruct.Type
let structInstance = structMetaType.init()
@marcelofabri
Copy link
Collaborator

Are you sure this example triggers? I couldn't reproduce on SwiftLint 0.13.2:

$ swiftlint lint --path file.swift          
Loading configuration from '.swiftlint.yml'
Linting Swift files at path file.swift
Linting 'file.swift' (1/1)
file.swift:2:2: error: Variable Name Violation: Variable name should be between 3 and 40 characters long: 'a' (variable_name)
file.swift:6:2: error: Variable Name Violation: Variable name should be between 3 and 40 characters long: 'a' (variable_name)
Done linting! Found 2 violations, 2 serious in 1 file.

@evgeniyd
Copy link
Author

@marcelofabri my fault, I didn't do linting, just shown the idiom.

Here is the snipped that does produce the warning:

import UIKit.UIView

public enum KeyImage {
    case standard
    case customView(type: UIView.Type)
}

extension KeyImage {
    public func view() -> UIView {
        switch self {
        case .standard:
            return StandardView()
        case .customView(let ViewType):
            return ViewType.init()
        }
    }
}

class StandardView: UIView { }
class MyView: UIView { }

KeyImage.standard.view()
KeyImage.customView(type: MyView.self).view()

Output:

$ swiftlint lint --path file.swift
Loading configuration from '.swiftlint.yml'
Linting Swift files at path file.swift
Linting 'file.swift' (1/1)
file.swift:14:28: warning: Explicit Init Violation: Explicitly calling .init() should be avoided. (explicit_init)
Done linting! Found 1 violation, 0 serious in 1 file.

@marcelofabri
Copy link
Collaborator

marcelofabri commented Dec 15, 2016

The rule currently uses a regex to check if it's a type or metatype, so changing ViewType to viewType fixes the issue.

It would be better to check if it was indeed a type or variable, but we currently don't have this information AFAIK and both a type (UIView) and a variable (viewType) have the same SyntaxKind: source.lang.swift.syntaxtype.identifier.

@marcelofabri marcelofabri added the enhancement Ideas for improvements of existing features and rules. label Dec 15, 2016
@evgeniyd
Copy link
Author

@marcelofabri Thank you for explanation. I thought it is possible to infer the type, as swiftlint hooked into Clang and SourceKit.

Unfortunately, lowercasing the var name here loses the semantics I'd like to preserve. Nevertheless, this particular example, could be fixed in this way, I have other cases, where I was in need to go with // swiftlint:disable:this explicit_init.

@jpsim
Copy link
Collaborator

jpsim commented Dec 15, 2016

I thought it is possible to infer the type, as swiftlint hooked into Clang and SourceKit.

Possible yes, but not implemented. Would require a new "compiled" mode that SwiftLint could run in such as the one described in #829 (comment).

@stale
Copy link

stale bot commented Nov 8, 2020

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!

@stale stale bot added the wontfix Issues that became stale and were auto-closed by a bot. label Nov 8, 2020
@stale stale bot closed this as completed Nov 15, 2020
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. wontfix Issues that became stale and were auto-closed by a bot.
Projects
None yet
Development

No branches or pull requests

3 participants