realm / realm/SwiftLint

prefer_key_path: false error and auto correct for static members

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

Nobody has claimed this yet.

acceptable-false-positive bug
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
Bug Description

A clear and concise description of what the bug is. Ideally, provide a small (but compilable) example code snippet that
can be used to reproduce the issue.


struct MyStruct {
  static var staticMember = "test"
}

let foo: [MyStruct.Type] = [MyStruct.self]
let bar = foo.map { $0.staticMember }

Mention the command or other SwiftLint integration method that caused the issue. Include stack traces or command output.

$ swiftlint lint

Produces test.swift:6:19: warning: Prefer Key Path Violation: Use a key path argument instead of a closure with property access (prefer_key_path)

$ SwiftLint lint --fix

Changes it into

struct MyStruct {
  static var staticMember = "test"
}

let foo: [MyStruct.Type] = [MyStruct.self]
let bar = foo.map(\.staticMember)

Which is not valid swift code: error: key path cannot refer to static member 'staticMember'

Environment
  • SwiftLint version: 0.57.0
  • Xcode version: 16.0
  • Installation method used: Homebrew
  • Configuration file:
opt_in_rules:
  - prefer_key_path

Are you using nested configurations? No

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 reproducing the prefer_key_path diagnostic with the provided static-member example using swiftlint lint, then check the --fix behavior. Done means the valid example is not reported as a violation and autocorrection does not produce the invalid \.staticMember key path.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.