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

- rule added i.e. redundant_extension #5359 #5397

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ disabled_rules:
- prefer_nimble
- prefer_self_in_static_references
- prefixed_toplevel_constant
- redundant_extension
- redundant_self_in_closure
- required_deinit
- self_binding
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

#### Enhancements

* Add new `redundant_extension` rule that detects redundant extensions.
An extension is considered redundant if it does not define any
members, but only conformances.
[Muhammad Zeeshan](https://github.com/mzeeshanid)
[#5359](https://github.com/realm/SwiftLint/issues/5359)

* Prevent from compiling `SwiftLint` target when only using `SwiftLintPlugin` on macOS.
[Julien Baillon](https://github.com/julien-baillon)
[#5372](https://github.com/realm/SwiftLint/issues/5372)
Expand All @@ -18,6 +24,11 @@
rule to ignore switch statements written in a single line.
[tonell-m](https://github.com/tonell-m)
[#5373](https://github.com/realm/SwiftLint/issues/5373)
* Add new `redundant_extension` rule that detects redundant extensions.
An extension is considered redundant if it does not define any
members, but only conformances.
[Muhammad Zeeshan](https://github.com/mzeeshanid)
[#5359](https://github.com/realm/SwiftLint/issues/5359)

* Add new `one_declaration_per_file` rule that allows only a
single class/struct/enum/protocol declaration per file.
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintBuiltInRules/Models/BuiltInRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public let builtInRules: [any Rule.Type] = [
ReduceBooleanRule.self,
ReduceIntoRule.self,
RedundantDiscardableLetRule.self,
RedundantExtensionRule.self,
RedundantNilCoalescingRule.self,
RedundantObjcAttributeRule.self,
RedundantOptionalInitializationRule.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import SwiftSyntax

@SwiftSyntaxRule
struct RedundantExtensionRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static let description = RuleDescription(
identifier: "redundant_extension",
name: "Redundant Extension",
description: "Avoid redundant extensions",
kind: .idiomatic,
nonTriggeringExamples: [
Example("""
extension Foo {
func something() {}
}
"""),
Example("""
extension Foo {
var a: Int { 1 }
}
"""),
Example("""
extension Foo {
final class Bar {}
}
"""),
Example("""
extension Foo {
struct Bar {}
}
""")
],
triggeringExamples: [
Example("""
↓extension Bar {}
""")
]
)
}

private extension RedundantExtensionRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: ExtensionDeclSyntax) {
if node.memberBlock.members.isEmpty {
violations.append(node.extensionKeyword.positionAfterSkippingLeadingTrivia)
}
}
}
}
6 changes: 6 additions & 0 deletions Tests/GeneratedTests/GeneratedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,12 @@ class RedundantDiscardableLetRuleGeneratedTests: SwiftLintTestCase {
}
}

class RedundantExtensionRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(RedundantExtensionRule.description)
}
}

class RedundantNilCoalescingRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(RedundantNilCoalescingRule.description)
Expand Down