realm / realm/SwiftLint

Rule Request: Unnecessary closure used in stored property default value with single expression

Open
#3,486 0 comments 2 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

(I feel like this one is a bit challenging to search for, so my apologies if there's already an issue or if it's already implemented)

New rule request

Possible rule identifier: unnecessary_property_closure (open to suggestions)

When a stored property's default value requires some customization/setup, it's often useful to use a closure as follows:

let closeButton: UIButton = {
    let button = UIButton()
    button.setImage(UIImage(named: "close"), for: .normal)
    return button
}()

But when that setup is done using a single expression, the closure is unnecessary:

// Unnecessary closure - should trigger a warning
let closeButton: UIButton = {
    return UIButton() 
}()

// More concise
let closeButton = UIButton()

This new rule should warn when using such an unnecessary closure with a single expression. I don't feel strongly one way or another about whether this should be enabled by default. This rule could probably support autocorrect as well.

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 comparing the two stored-property examples in the issue and define the exact single-expression closure cases the rule should report. Decide whether the rule is enabled by default and whether autocorrection is in scope; done means the requested unnecessary closure is detected without flagging the multi-statement setup example.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.