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

Rule Request: explicit_default_argument #5416

Open
2 tasks done
KacperCzapp opened this issue Jan 10, 2024 · 1 comment
Open
2 tasks done

Rule Request: explicit_default_argument #5416

KacperCzapp opened this issue Jan 10, 2024 · 1 comment

Comments

@KacperCzapp
Copy link

KacperCzapp commented Jan 10, 2024

New Issue Checklist

New rule request

I would like to propose a new rule explicit_default_argument which would trigger a violation if an argument passed to a function or initializer matches the default value provided by the function (or init).

Given the following code:

struct Foo {
    let bar: Bool
    let baz: String

    init(bar: Boo, baz: String = "") { ... }
    func doSomething(arg: Bool = true, arg2: String = "", arg3: Float = 0.0) { ... }
}

When calling:

let foo = Foo(bar: false, baz: "")
foo.doSomething(arg: true, arg2: "", arg3: 0.0)

Would trigger violations in both lines as they are called with the same values as their defaults. Calling Foo(bar: false, baz: "baz") would not trigger the rule.

This allows for cleaner, more concise code on the call site, especially if there are many default parameters.

let foo = Foo(bar: false)
foo.doSomething()

Should the rule be configurable, if so what parameters should be configurable?

If any, I suggest adding two params, for excluding initializers and excluding functions, both set to false by default.

Should the rule be opt-in or enabled by default? Why?

My suggestion is to set the rule as opt-in. It's a style enhancement, not a functional one.

@SimplyDanny
Copy link
Collaborator

This requires to implement an Analyzer rule, because the rule needs to access the method/initializer declaration from the call-side to figure out the default parameter values. This is impossible on a syntax-level only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants