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

Add ImplicitGetterRule #862

Merged
merged 3 commits into from
Nov 25, 2016
Merged

Add ImplicitGetterRule #862

merged 3 commits into from
Nov 25, 2016

Conversation

marcelofabri
Copy link
Collaborator

Partially fixes #57

It seems that SourceKit doesn't provide information on subscript, which is weird because there is a FunctionSubscript case on SyntaxKind.

struct Thing {

    subscript(index: Int) -> Int {
      return 2
    }
}
sourcekitten structure --file file.swift
{
  "key.substructure" : [
    {
      "key.kind" : "source.lang.swift.decl.struct",
      "key.offset" : 0,
      "key.nameoffset" : 7,
      "key.namelength" : 5,
      "key.bodyoffset" : 14,
      "key.bodylength" : 58,
      "key.accessibility" : "source.lang.swift.accessibility.internal",
      "key.substructure" : [
        {
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 30,
          "key.nameoffset" : 0,
          "key.namelength" : 0,
          "key.length" : 10,
          "key.typename" : "Int",
          "key.name" : "index"
        }
      ],
      "key.name" : "Thing",
      "key.length" : 73
    }
  ],
  "key.offset" : 0,
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.length" : 74
}

@codecov-io
Copy link

codecov-io commented Oct 30, 2016

Current coverage is 85.84% (diff: 97.18%)

No coverage report found for master at 1b82909.

Powered by Codecov. Last update 1b82909...92569e6

@norio-nomura
Copy link
Collaborator

False positive pattern with local scoped type:

class Foo {
  var foo: Int {
    struct Bar {
      var bar: Int {
        get { return 1 } // violation produced on this line
        set { _ = newValue }
      }
    }
    return Bar().bar
  }
}

@marcelofabri
Copy link
Collaborator Author

Nice catch! Do you have any tips on how to deal with this?

@norio-nomura
Copy link
Collaborator

If I need to write this rule, I guess I would do:

  1. Search keyword get from SyntaxMap
  2. Search deepest structure containing the get, like as Structure.kindsFor(_:)
  3. Check condition of the structure for violation

The key of avoiding false positive is 2.
I guess 1 would be faster than using ASTRule.

}

private func variableDeclarationsFor(byteOffset: Int, structure: Structure) ->
[[String : SourceKitRepresentable]] {
Copy link
Collaborator Author

@marcelofabri marcelofabri Nov 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of breaking line here, but I couldn't think of other ways to respect the 100 length limit 😬

@marcelofabri
Copy link
Collaborator Author

Sorry for taking to take a look at this, but I've implemented your suggestion on 772e702, @norio-nomura. Thanks again 🙌

@jpsim
Copy link
Collaborator

jpsim commented Nov 25, 2016

Awesome work! 🎉

@jpsim jpsim merged commit 1151c18 into realm:master Nov 25, 2016
@marcelofabri marcelofabri deleted the implicit-getters branch November 25, 2016 22:15
@frank61003
Copy link

My project can run in my old Macbook Pro but show the error in my new Mac mini. I check my Xcode(11.4.1) and swift version(4.2) are all the same. how to solve this issue? I use cocoa pod to install swiftlint and the version is 0.27. (pod 'SwiftLint', '0.27'). I check the document many times but still can't find the answer.

https://stackoverflow.com/questions/61947496/how-to-slove-implicit-getter-violation-computed-read-only-subscripts-should-avo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate implicit getters
5 participants