realm / SwiftLint

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

Colon removes escape character in argument list #2981

Closed iwheelbuy closed 3 years ago

iwheelbuy commented 4 years ago

New Issue Checklist

Describe the bug

I have some swift code which is autogenerated. It contains

`default`: `default` ?? self.`default`

Xcode suggests:

Keyword `default` does not need to be escaped in argument list Replace `default` with default

I'd love to replace it, but since the file is autogenerated it will become `default` very soon. But the SwiftLink autocorrects the file in a wrong way and I get

`defaul: `default` ?? self.`default`
Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint autocorrect

Correcting ... (XXX/YYY)
ZZZ.swift:4835:104 Corrected Colon
Done correcting YYY files!

Environment

disabled_rules:
excluded:
- Carthage
- Pods
public func with(apartment: String? = nil, country: Object.Country? = nil, `default`: Bool? = nil, firstName: String? = nil, house: String? = nil, id: Int? = nil, lastName: String? = nil, middleName: String? = nil, name: String? = nil, phones: [Object.UserPhone]? = nil, settlement: Object.Settlement? = nil, shortName: String? = nil, street: String? = nil, zipCode: String? = nil) -> UserAddress {
    return UserAddress(apartment: apartment ?? self.apartment, country: country ?? self.country, `default`: `default` ?? self.`default`, firstName: firstName ?? self.firstName, house: house ?? self.house, id: id ?? self.id, lastName: lastName ?? self.lastName, middleName: middleName ?? self.middleName, name: name ?? self.name, phones: phones ?? self.phones, settlement: settlement ?? self.settlement, shortName: shortName ?? self.shortName, street: street ?? self.street, zipCode: zipCode ?? self.zipCode)
}
ZZZ.swift:4835:104: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
iwheelbuy commented 4 years ago

Also I got typealias __States<T> = [__State[T]> where T: Hashable from typealias __States<T> = Array<__State<T>> where T: Hashable and OptionalInt?>.none from Optional<Optional<Int>>.none

marcelofabri commented 4 years ago

Also I got typealias __States<T> = [__State[T]> where T: Hashable from typealias __States<T> = Array<__State<T>> where T: Hashable and OptionalInt?>.none from Optional<Optional<Int>>.none

This is probably a different rule/bug. Can you please open a new issue for it?

marcelofabri commented 4 years ago

This seems to be caused by a SourceKit issue:

public func with(`default`: Bool? = nil) -> UserAddress {
    return UserAddress(`default`: `default` ?? self.`default`)
}
{
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.diagnostics" : [
    {
      "key.column" : 21,
      "key.description" : "keyword 'default' does not need to be escaped in argument list",
      "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
      "key.filepath" : "\/Users\/marcelofabri\/projetos\/SwiftLint\/file.swift",
      "key.fixits" : [
        {
          "key.length" : 1,
          "key.offset" : 78,
          "key.sourcetext" : ""
        },
        {
          "key.length" : 1,
          "key.offset" : 86,
          "key.sourcetext" : ""
        }
      ],
      "key.line" : 2,
      "key.severity" : "source.diagnostic.severity.warning"
    }
  ],
  "key.length" : 120,
  "key.offset" : 0,
  "key.substructure" : [
    {
      "key.accessibility" : "source.lang.swift.accessibility.public",
      "key.attributes" : [
        {
          "key.attribute" : "source.decl.attribute.public",
          "key.length" : 6,
          "key.offset" : 0
        }
      ],
      "key.bodylength" : 61,
      "key.bodyoffset" : 57,
      "key.kind" : "source.lang.swift.decl.function.free",
      "key.length" : 112,
      "key.name" : "with(default:)",
      "key.namelength" : 28,
      "key.nameoffset" : 12,
      "key.offset" : 7,
      "key.substructure" : [
        {
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.length" : 22,
          "key.name" : "default",
          "key.namelength" : 7,
          "key.nameoffset" : 17,
          "key.offset" : 17,
          "key.typename" : "Bool?"
        },
        {
          "key.bodylength" : 38,
          "key.bodyoffset" : 78,
          "key.kind" : "source.lang.swift.expr.call",
          "key.length" : 51,
          "key.name" : "UserAddress",
          "key.namelength" : 11,
          "key.nameoffset" : 66,
          "key.offset" : 66,
          "key.substructure" : [
            {
              "key.bodylength" : 27,
              "key.bodyoffset" : 89,
              "key.kind" : "source.lang.swift.expr.argument",
              "key.length" : 38,
              "key.name" : "`defaul", // Wrong!
              "key.namelength" : 7, // Wrong!
              "key.nameoffset" : 78,
              "key.offset" : 78
            }
          ]
        }
      ],
      "key.typename" : "UserAddress"
    }
  ]
}

Created SR-11987 to track this.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

marcelofabri commented 3 years ago

Still an issue on Swift 5.3

jpsim commented 3 years ago

It'd be helpful if others weighed in on SR-11987 so the Swift team can be made aware of this issue.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!