Skip to content

Commit

Permalink
Add UnusedDeclarationRule
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed Jul 18, 2019
1 parent 438905b commit 6381063
Show file tree
Hide file tree
Showing 14 changed files with 425 additions and 269 deletions.
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ included:
excluded:
- Tests/SwiftLintFrameworkTests/Resources
analyzer_rules:
- unused_declaration
- unused_import
- unused_private_declaration
opt_in_rules:
- anyobject_protocol
- array_init
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
[Elliott Williams](https://github.com/elliottwilliams)
[#2431](https://github.com/realm/SwiftLint/issues/2431)

* Add a new `unused_declaration` analyzer rule to lint for unused declarations.
By default, detects unused `fileprivate`, `private` and `internal`
declarations. Configure the rule with `include_public_and_open: true` to
also detect unused `public` and `open` declarations.
[JP Simard](https://github.com/jpsim)

* Completely remove the `unused_private_declaration` rule. Please use
`unused_declaration` instead.
[JP Simard](https://github.com/jpsim)

#### Enhancements

* None.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test_tsan:
DYLD_INSERT_LIBRARIES=$(TSAN_LIB) $(TSAN_XCTEST) $(TSAN_TEST_BUNDLE)

write_xcodebuild_log: bootstrap
xcodebuild -workspace SwiftLint.xcworkspace -scheme swiftlint > xcodebuild.log
xcodebuild -workspace SwiftLint.xcworkspace -scheme swiftlint clean build-for-testing > xcodebuild.log

analyze: write_xcodebuild_log
swift run -c release swiftlint analyze --strict --compiler-log-path xcodebuild.log
Expand Down
127 changes: 69 additions & 58 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@
* [Unused Capture List](#unused-capture-list)
* [Unused Closure Parameter](#unused-closure-parameter)
* [Unused Control Flow Label](#unused-control-flow-label)
* [Unused Declaration](#unused-declaration)
* [Unused Enumerated](#unused-enumerated)
* [Unused Import](#unused-import)
* [Unused Optional Binding](#unused-optional-binding)
* [Unused Private Declaration](#unused-private-declaration)
* [Unused Setter Value](#unused-setter-value)
* [Valid IBInspectable](#valid-ibinspectable)
* [Vertical Parameter Alignment](#vertical-parameter-alignment)
Expand Down Expand Up @@ -23612,6 +23612,74 @@ default: break



## Unused Declaration

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
--- | --- | --- | --- | --- | ---
`unused_declaration` | Disabled | No | lint | Yes | 3.0.0

Declarations should be referenced at least once within all files linted.

### Examples

<details>
<summary>Non Triggering Examples</summary>

```swift
let kConstant = 0
_ = kConstant
```

```swift
struct Item {}
struct ResponseModel: Codable {
let items: [Item]

enum CodingKeys: String, CodingKey {
case items = "ResponseItems"
}
}

_ = ResponseModel(items: [Item()]).items
```

```swift
class ResponseModel {
@objc func foo() {
}
}
_ = ResponseModel()
```

</details>
<details>
<summary>Triggering Examples</summary>

```swift
let ↓kConstant = 0
```

```swift
struct ↓ResponseModel: Codable {
let ↓items: [Item]

enum ↓CodingKeys: String {
case items = "ResponseItems"
}
}
```

```swift
class ↓ResponseModel {
func ↓foo() {
}
}
```

</details>



## Unused Enumerated

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
Expand Down Expand Up @@ -23885,63 +23953,6 @@ if case .some(let ↓_) = self {}



## Unused Private Declaration

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
--- | --- | --- | --- | --- | ---
`unused_private_declaration` | Disabled | No | lint | Yes | 3.0.0

Private declarations should be referenced in that file.

### Examples

<details>
<summary>Non Triggering Examples</summary>

```swift
private let kConstant = 0
_ = kConstant
```

```swift
struct ResponseModel: Codable {
let items: [Item]

private enum CodingKeys: String, CodingKey {
case items = "ResponseItems"
}
}
```

```swift
class ResponseModel {
@objc private func foo() {
}
}
```

</details>
<details>
<summary>Triggering Examples</summary>

```swift
private let ↓kConstant = 0
```

```swift
struct ResponseModel: Codable {
let items: [Item]

private enum ↓CodingKeys: String {
case items = "ResponseItems"
}
}
```

</details>



## Unused Setter Value

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ private let regexCacheLock = NSLock()
private struct RegexCacheKey: Hashable {
// Disable unused private declaration rule here because even though we don't use these properties
// directly, we rely on them for their hashable and equatable behavior.
// swiftlint:disable unused_private_declaration
// swiftlint:disable unused_declaration
let pattern: String
let options: NSRegularExpression.Options
// swiftlint:enable unused_private_declaration
// swiftlint:enable unused_declaration
}

extension NSRegularExpression.Options: Hashable {
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ public let masterRuleList = RuleList(rules: [
UnusedCaptureListRule.self,
UnusedClosureParameterRule.self,
UnusedControlFlowLabelRule.self,
UnusedDeclarationRule.self,
UnusedEnumeratedRule.self,
UnusedImportRule.self,
UnusedOptionalBindingRule.self,
UnusedPrivateDeclarationRule.self,
UnusedSetterValueRule.self,
ValidIBInspectableRule.self,
VerticalParameterAlignmentOnCallRule.self,
Expand Down
6 changes: 0 additions & 6 deletions Source/SwiftLintFramework/Protocols/RuleConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ public protocol RuleConfiguration {
func isEqualTo(_ ruleConfiguration: RuleConfiguration) -> Bool
}

extension RuleConfiguration {
internal var cacheDescription: String {
return (self as? CacheDescriptionProvider)?.cacheDescription ?? consoleDescription
}
}

public extension RuleConfiguration where Self: Equatable {
func isEqualTo(_ ruleConfiguration: RuleConfiguration) -> Bool {
return self == ruleConfiguration as? Self
Expand Down
Loading

0 comments on commit 6381063

Please sign in to comment.