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

Add include_bare_init option to explicit_init rule #5203

Conversation

mildm8nnered
Copy link
Collaborator

@mildm8nnered mildm8nnered commented Sep 2, 2023

Adds include_bare_init option to the explicit_init rule.

This will produce violations where .init is called using type inference to determine the type.

For example:

let foo: Foo = ↓.init()
let foo: [Foo] = [↓.init(), ↓.init()]
foo(↓.init())

This option is off by default (as many code bases use the .init construct), and these violations are not correctable, as SwiftLint cannot infer the intended type.

Also adds an include_explicit_init option, on by default, which allows the original behavior of explicit_init to be suppressed, so that users can tune the rule's behavior to suit their particular use case.

Replaces #5170, and resolves #2960

@mildm8nnered mildm8nnered changed the title Mildm8nnered explicit init bare init option Add include_bare_init option to explicit_init rule Sep 2, 2023
@SwiftLintBot
Copy link

SwiftLintBot commented Sep 2, 2023

17 Messages
📖 Linting Aerial with this PR took 1.16s vs 1.17s on main (0% faster)
📖 Linting Alamofire with this PR took 1.54s vs 1.55s on main (0% faster)
📖 Linting Brave with this PR took 8.7s vs 8.72s on main (0% faster)
📖 Linting DuckDuckGo with this PR took 4.26s vs 4.29s on main (0% faster)
📖 Linting Firefox with this PR took 10.08s vs 10.13s on main (0% faster)
📖 Linting Kickstarter with this PR took 10.95s vs 10.93s on main (0% slower)
📖 Linting Moya with this PR took 0.6s vs 0.61s on main (1% faster)
📖 Linting NetNewsWire with this PR took 3.28s vs 3.29s on main (0% faster)
📖 Linting Nimble with this PR took 0.79s vs 0.79s on main (0% slower)
📖 Linting PocketCasts with this PR took 8.6s vs 8.59s on main (0% slower)
📖 Linting Quick with this PR took 0.39s vs 0.39s on main (0% slower)
📖 Linting Realm with this PR took 12.09s vs 12.09s on main (0% slower)
📖 Linting Sourcery with this PR took 2.45s vs 2.45s on main (0% slower)
📖 Linting Swift with this PR took 5.18s vs 5.19s on main (0% faster)
📖 Linting VLC with this PR took 1.46s vs 1.47s on main (0% faster)
📖 Linting Wire with this PR took 8.97s vs 9.0s on main (0% faster)
📖 Linting WordPress with this PR took 12.98s vs 13.02s on main (0% faster)

Generated by 🚫 Danger

@mildm8nnered mildm8nnered force-pushed the mildm8nnered-explicit-init-bare-init-option branch from bb3bd36 to 5dc6cb5 Compare September 2, 2023 11:49
@mildm8nnered mildm8nnered marked this pull request as ready for review September 2, 2023 12:34
private(set) var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "include_explicit_init")
private(set) var includeExplicitInit = true
@ConfigurationElement(key: "include_bare_init")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift calls expression like .init(...) "implicit member expression". Not sure we should use the term in an ExplicitInitRule, though. So "bare" might be okay. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm not super-happy with "bare". "inferred" sounds a little bit better, and is at least in keeping with existing terms of art, but "inferred_init" doesn't feel quite right, as it's not the "init" that is inferred ... totally open to suggestions if anyone has something better.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling as well. include_implict_init_members maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe bare is ok - "implicit member expression" feels a little obscure

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name would not matter so much if there was a description added to each option. The infrastructure is there already. I may prepare a PR in the upcoming days to open it up and add a few examples. More can then be attached over time.

Long story short, let's keep "bare". 😉

@mildm8nnered mildm8nnered force-pushed the mildm8nnered-explicit-init-bare-init-option branch from 5dc6cb5 to 919d399 Compare September 3, 2023 10:47
CHANGELOG.md Outdated Show resolved Hide resolved
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-explicit-init-bare-init-option branch from ed14259 to 6f2d8ad Compare September 4, 2023 11:30
@SimplyDanny SimplyDanny merged commit 0c9827b into realm:main Sep 4, 2023
5 checks passed
@mildm8nnered mildm8nnered deleted the mildm8nnered-explicit-init-bare-init-option branch September 26, 2023 22:24
KS1019 pushed a commit to KS1019/SwiftLint that referenced this pull request Oct 2, 2023
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.

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