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 UnusedDeclarationRule #2814

Merged
merged 4 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
128 changes: 70 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,75 @@ 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 Item {}
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 +23954,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
16 changes: 0 additions & 16 deletions Source/SwiftLintFramework/Extensions/Dictionary+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ extension Dictionary where Key: ExpressibleByStringLiteral {
var typeName: String? {
return self["key.typename"] as? String
}
/// Column where the token's declaration begins.
var docColumn: Int? {
return (self["key.doc.column"] as? Int64).flatMap({ Int($0) })
}
/// Line where the token's declaration begins.
var docLine: Int? {
return (self["key.doc.line"] as? Int64).flatMap({ Int($0) })
}
/// Parsed scope start.
var docType: Int? {
return (self["key.doc.type"] as? Int64).flatMap({ Int($0) })
}
/// Parsed scope start end.
var usr: Int? {
return (self["key.usr"] as? Int64).flatMap({ Int($0) })
}
/// Documentation length.
var docLength: Int? {
return (self["key.doclength"] as? Int64).flatMap({ Int($0) })
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
Loading