Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
Nef10 opened this issue Oct 20, 2024 · 1 comment
Open
2 tasks done

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

Nef10 opened this issue Oct 20, 2024 · 1 comment
Labels
acceptable-false-positive False positives caused by rules that are unavoidable due to missing type information. bug Unexpected and reproducible misbehavior.

Comments

@Nef10
Copy link

Nef10 commented Oct 20, 2024

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

@SimplyDanny
Copy link
Collaborator

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.

@SimplyDanny SimplyDanny added bug Unexpected and reproducible misbehavior. acceptable-false-positive False positives caused by rules that are unavoidable due to missing type information. labels Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acceptable-false-positive False positives caused by rules that are unavoidable due to missing type information. bug Unexpected and reproducible misbehavior.
Projects
None yet
Development

No branches or pull requests

2 participants