SwiftLint: False positive `implicit_getter` violation when mentioning word "set" in a comment in custom getter of a read-write var

New Issue Checklist

Describe the bug

Mentioning word “set” (without parentheses) in a comment in a get of a read-write var with both get and set defined triggers SwiftLint to think that this is a read-only property.

image
Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint --path Test.swift --no-cache --enable-all-rules
Loading configuration from '.swiftlint.yml'
Linting Swift files at paths Test.swift
Linting 'Test.swift' (1/1)
/Users/yas/code/caremobile/Test.swift:3:9: warning: Implicit Getter Violation: Computed read-only properties should avoid using the get keyword. (implicit_getter)
Done linting! Found 1 violation, 0 serious in 1 file.

Environment

  • SwiftLint 0.39.1
  • Installation method used: Homebrew
  • Paste your configuration file:
excluded:
  - Pods

opt_in_rules:
  - array_init
  - contains_over_first_not_nil
  - collection_alignment
  - contains_over_filter_count
  - empty_count
  - empty_collection_literal
  - overridden_super_call
  - sorted_first_last
  - yoda_condition

disabled_rules:
  - cyclomatic_complexity
  - file_length
  - force_cast
  - force_try
  - function_body_length
  - function_parameter_count
  - identifier_name
  - large_tuple
  - line_length
  - multiple_closures_with_trailing_closure
  - nesting # consider
  - notification_center_detachment
  - operator_whitespace
  - todo # fix
  - type_body_length
  - type_name # consider
  - weak_delegate # fix

statement_position:
  statement_mode: uncuddled_else
  • Are you using nested configurations?No

  • Which Xcode version are you using (check xcodebuild -version)? Xcode 11.4 Build version 11E146

  • Do you have a sample that shows the issue? Yes:

Test.swift

extension Test {
    var foo: Bool {
        get {
            bar?.boolValue ?? true // Comment mentioning word set which triggers violation
        }
        set {
            bar = NSNumber(value: newValue as Bool)
        }
    }
}

If I don’t mention word “set” in the comment then it seem to work fine. I don’t recall seeing this issues prior to updating to Xcode 11.4.

Hope it helps.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17

Commits related to this issue

Most upvoted comments

thanks for everyone reporting different minimal examples! this has been very helpful to fix the issue quickly

I found another false-positive. When the set is before the get you get warning. If it is the other way round then everything is all right. I believe another rule could be created to formalize the order of get-set.

extension Float {
    var clamped: Float {
        set {
            self = min(1, max(0, newValue))
        }
        get {
            min(1, max(0, self))
        }
    }
}

Command:

swiftlint

Output:

Linting Swift files at paths 
Linting 'lint.swift' (1/1)
/Users/kaspar/Downloads/swiftlint-issue/lint.swift:6:9: warning: Implicit Getter Violation: Computed read-only properties should avoid using the get keyword. (implicit_getter)
Done linting! Found 1 violation, 0 serious in 1 file.

@jpsim my issues are fixed on the last master

Looking forward to the fixing release. My code below still triggers this false positive on 0.39.1:

import Foundation

extension UserDefaults {
    var didShowWelcomeWindow: Bool {
        set {
            self.set(newValue, forKey: "didShowWelcomeWindow")
        }
        get {
            return bool(forKey: "didShowWelcomeWindow")
        }
    }
}

Update: it resolves if I move the getter above the setter:

var didShowWelcomeWindow: Bool {
    get {
        return bool(forKey: "didShowWelcomeWindow”)
    }
    set {
        self.set(newValue, forKey: "didShowWelcomeWindow”)
    }    
}