realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.64k stars 2.22k forks source link

prefer_key_path: false error and auto correct for static members #5831

Open Nef10 opened 5 days ago

Nef10 commented 5 days ago

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

opt_in_rules:
  - prefer_key_path

Are you using nested configurations? No

SimplyDanny commented 1 day ago

This is hopefully going to be resolved by an accepted Swift Evolution proposal.

At the moment, there is not much SwiftLint can do as it doesn't know if the element refers to a class or instance property only spotting how it is accessed.