realm / realm/SwiftLint

Add support for catching dead recursive code with unused_declaration

Open
#3,187 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Currently if you have code like this:

/tmp/foo.swift:

struct Foo {
    var baz = true

    func bar() {
        if self.baz {
            self.bar()
        }
    }
}

print(Foo())

And you run the unused_declaration rule on it with this config:

.swiftlint.yml:

included:
  - /tmp/foo.swift
analyzer_rules:
  - unused_declaration

commands.json:

[
    {
        "arguments": [
            "swiftc",
            "/tmp/Foo.swift",
            "-sdk",
            "/Applications/Xcode-11.3.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
        ],
        "file": "/tmp/Foo.swift"
    }
]

And you run swiftlint:

% swiftlint analyze --compile-commands $PWD/commands.json
Loading configuration from '.swiftlint.yml'
Analyzing Swift files at paths
Collecting 'Foo.swift' (1/1)
Analyzing 'Foo.swift' (1/1)
Done analyzing! Found 0 violations, 0 serious in 1 file.

bar is not diagnosed as being unused because it calls itself. It would be awesome if references included in the same definition they're referenced by were not included when determining if something is unused.

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 the unused_declaration analyzer and reproduce the example using swiftlint analyze --compile-commands commands.json with the shown .swiftlint.yml and Swift source. Done means the self-recursive bar method is diagnosed as unused without counting its reference from within its own definition.

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.