realm / realm/SwiftLint

Rule Request: `explicit_default_argument`

Open
#5,416 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

rule-request
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.