realm / realm/SwiftLint

False positive: explicit_self with Swift property wrappers

Open
#3,075 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

When using Swift property wrappers, auto-correcting the explicit_self rule yields adding a self. in the wrong location.

Before:

print($property)

After autocorrect:

print($self.property)

Expected:

print(self.$property)

Additionally, it identifies a false positive in a valid situation:

print(self.$property)

And attempts to autocorrect to:

print(self.$self.property)

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint analyze --compiler-log-path /Users/mrebello/Downloads/output.txt --config /Users/mrebello/Downloads/SwiftlintTest/.swiftlint.yml --path /Users/mrebello/Downloads/SwiftlintTest/SwiftlintTest/AppDelegate.swift --autocorrect
Environment
  • SwiftLint version (run swiftlint version to be sure)? 0.38.2
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? Homebrew
  • Paste your configuration file:
analyzer_rules:
  - explicit_self
  • Are you using nested configurations? No
  • Which Xcode version are you using (check xcodebuild -version)? 11.3.1
  • Do you have a sample that shows the issue?
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
        -> Bool
    {
        SomeClass().doSomething()
        return true
    }
}

@propertyWrapper
struct CustomWrapper {
    private var value: String

    init(wrappedValue: String) {
        self.value = wrappedValue
    }

    var wrappedValue: String {
        get { return self.value + "1" }
        set { self.value = newValue }
    }

    var projectedValue: String {
        return self.wrappedValue
    }
}

final class SomeClass {
    @CustomWrapper
    private var property = "foo"

    func doSomething() {
        // Incorrectly autocorrects to print($self.property)
        print($property)
        // Incorrectly autocorrects to print(self.$self.property)
        print(self.$property)
    }
}

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 explicit_self analyzer and the AppDelegate.swift sample, using the reported SwiftLint command and configuration to reproduce both autocorrection cases. Compare the results with the expected print(self.$property) behavior; done means no false positive and no insertion of self. after the property-wrapper projection marker.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.