Rule Request: `explicit_default_argument`
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing SwiftLint’s existing rule implementations and tests to find the appropriate entry point for a new rule. Define how calls matching default arguments should be detected, settle whether initializer and function exclusions are configurable, and add coverage showing that redundant arguments are reported while differing arguments are not.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100