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 for avoiding redundant extensions resolve #5359 #5388

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

#### Enhancements

* Add new `redundant_extension` rule that detect redundant extensions.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add new `redundant_extension` rule that detect redundant extensions.
* 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.
Extensions are an exception; more than one is allowed.
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 @@ -166,6 +166,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 @@ -986,6 +986,12 @@ class RedundantDiscardableLetRuleGeneratedTests: SwiftLintTestCase {
}
}

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

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