Skip to content

Commit

Permalink
Fix false positives in unused_declaration for protocol extensions (re…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim authored Dec 30, 2019
1 parent 9ecefbd commit 99dd894
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
[JP Simard](https://github.com/jpsim)
[#2877](https://github.com/realm/SwiftLint/issues/2877)

* Fix false positives from the `unused_declaration` rule involving
functions in protocol extensions.
[JP Simard](https://github.com/jpsim)

## 0.38.0: Toroidal Agitation

#### Breaking
Expand Down
22 changes: 22 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -24610,6 +24610,28 @@ let kConstant = 0
_ = kConstant
```

```swift
enum Change<T> {
case insert(T)
case delete(T)
}

extension Sequence {
func deletes<T>() -> [T] where Element == Change<T> {
return compactMap { operation in
if case .delete(let value) = operation {
return value
} else {
return nil
}
}
}
}

let changes = [Change.insert(0), .delete(0)]
changes.deletes()
```

```swift
struct Item {}
struct ResponseModel: Codable {
Expand Down
26 changes: 25 additions & 1 deletion Source/SwiftLintFramework/Rules/Lint/UnusedDeclarationRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ public struct UnusedDeclarationRule: AutomaticTestableRule, ConfigurationProvide
_ = kConstant
""",
"""
enum Change<T> {
case insert(T)
case delete(T)
}
extension Sequence {
func deletes<T>() -> [T] where Element == Change<T> {
return compactMap { operation in
if case .delete(let value) = operation {
return value
} else {
return nil
}
}
}
}
let changes = [Change.insert(0), .delete(0)]
changes.deletes()
""",
"""
struct Item {}
struct ResponseModel: Codable {
let items: [Item]
Expand Down Expand Up @@ -219,7 +240,10 @@ private extension SwiftLintFile {
private static func referencedUSR(cursorInfo: SourceKittenDictionary) -> String? {
if let usr = cursorInfo.usr,
let kind = cursorInfo.kind,
kind.contains("source.lang.swift.ref") {
kind.starts(with: "source.lang.swift.ref") {
if let synthesizedLocation = usr.range(of: "::SYNTHESIZED::")?.lowerBound {
return String(usr.prefix(upTo: synthesizedLocation))
}
return usr
}

Expand Down

0 comments on commit 99dd894

Please sign in to comment.