realm / realm/SwiftLint

Rule Request: Prefer returning `Self` in value types instead of the value type's name

Open
#2,944 0 comments 5 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

This rule is similar to https://github.com/realm/SwiftLint/issues/2921 but I don't think it's a duplicate.

New rule request

Please describe the rule idea, format
this issue's title as Rule Request: [Rule Name] and describe:

  1. Why should this rule be added?

I can't find the exact Swift proposal but in Swift 5.1 (I think) you can return Self in value types instead of the value type's name.

e.g.

struct Foo {
    static func someFunc() -> Self { ... }
}
  1. Provide several examples of what would and wouldn't trigger violations.

would trigger:

struct Foo {
    static func someFunc() -> Foo { ... }
}
struct Foo {
    func someFunc() -> Foo { ... }
}
struct Foo {
    var someComputedProperty: Foo { ... }
}

wouldn't trigger:

struct Foo {
    func someFunc() -> Self { ... }
}
class Foo {
    func someFunc() -> Foo { ... }
}
  1. Should the rule be configurable, if so what parameters should be configurable?

I don't think so

  1. Should the rule be opt-in or enabled by default? Why?
    See README.md for guidelines on when to mark a rule as opt-in.

Safer to opt-in I guess.

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 with README.md's opt-in-rules guidance and compare the related issue #2921. Use the Swift examples in this issue to define which value-type return annotations should trigger and which class or Self cases should not. Done means an opt-in rule with coverage for these examples.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.