SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Rule Request: [explicit_init] - avoid using `let cl: Class = .init()`

Open jendaz opened this issue 4 years ago • 3 comments

New Issue Checklist

New rule request

Please describe the rule idea, format this issue's title as Rule Request: [Rule Name] and describe:

Improve rule explicit_init to force user to use let cl = Class() instead of let cl: Class = .init()

  1. Why should this rule be added? Share links to existing discussion about what the community thinks about this.

Code is more readable when you initialize with class name instead of .init() only

  1. Provide several examples of what would and wouldn't trigger violations.

NO:

let view: UIView = .init(frame: .init(origin: .init(x: 0, y: 0), size: .init(width: 50, height: 50)))

YES:

let view = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 50, height: 50)))
  1. Should the rule be configurable, if so what parameters should be configurable?

I guess no.

  1. Should the rule be opt-in or enabled by default? Why? See README.md for guidelines on when to mark a rule as opt-in.

I guess it should be enabled by default.

jendaz avatar Nov 13 '19 21:11 jendaz

I've created a simple custom rule for that:

init_with_class_name:
        name: "Init With Class Name"
        regex: "((=|,|\\[|:|return)\\s*\\.init\\()"
        message: "There should be init function with class or structure name. EG: let cl = Class() instead of let cl: Class = .init()"
        severity: warning

jendaz avatar Nov 13 '19 21:11 jendaz

@jendaz can you please fill the template? it really helps whoever decides to work on this!

marcelofabri avatar Nov 13 '19 21:11 marcelofabri

I've created a simple custom rule for that:

init_with_class_name:
        name: "Init With Class Name"
        regex: "((=|,|\\[|:|return)\\s*\\.init\\()"
        message: "There should be init function with class or structure name. EG: let cl = Class() instead of let cl: Class = .init()"
        severity: warning

This pattern doesn't handle this following case

optionalValue ?? .init(...)

This should be a more accurate & efficient rule:

  init_with_class_name:
    name: "Init With Class Name"
    message: "Prefer let object = Class() instead of let object: Class = .init()"
    included: ".*.swift"
    regex: '(?<!self|super)\.init\('
    match_kinds:
      - identifier
      - keyword
    severity: warning

also, do we have an update on this issue?

honghaoz avatar Jul 27 '22 21:07 honghaoz

I'd love to see this officially supported 👍🏻

brzzdev avatar Aug 09 '23 20:08 brzzdev

I have actually implemented this rule. I don't think I kept my branches (I built it as a demo), but it was very easy, at least to catch all .init cases.

I'm curious as to why you think

let view: UIView = .init(frame: .init(origin: .init(x: 0, y: 0), size: .init(width: 50, height: 50)))

should not trigger.

and how about?

let x: [SomeObject] = [
    .init(someArg: someArg)
    .init(someArg: someOtherArg)
]

mildm8nnered avatar Aug 10 '23 09:08 mildm8nnered

I assumed they meant the

NO

case wouldn't trigger and

YES

would

and I would personally say that array example should trigger. Maybe a level of strictness could be configurable?

brzzdev avatar Aug 10 '23 09:08 brzzdev

Work in progress ^^^^

mildm8nnered avatar Aug 11 '23 17:08 mildm8nnered

Awesome! Is there a timeline for a new release?

brzzdev avatar Sep 06 '23 21:09 brzzdev