Skip to content

Commit

Permalink
Enforce any on existential types (#5273)
Browse files Browse the repository at this point in the history
This makes syntactically clear which types are rather expensive.
  • Loading branch information
SimplyDanny authored Oct 12, 2023
1 parent 6438c77 commit 58928b7
Show file tree
Hide file tree
Showing 126 changed files with 268 additions and 230 deletions.
2 changes: 1 addition & 1 deletion .sourcery/BuiltInRules.stencil
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

/// The rule list containing all available rules built into SwiftLint.
public let builtInRules: [Rule.Type] = [
public let builtInRules: [any Rule.Type] = [
{% for rule in types.structs where rule.name|hasSuffix:"Rule" %} {{ rule.name }}.self{% if not forloop.last %},{% endif %}
{% endfor %}]
2 changes: 1 addition & 1 deletion .sourcery/ReportersList.stencil
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/// The reporters list containing all the reporters built into SwiftLint.
public let reportersList: [Reporter.Type] = [
public let reportersList: [any Reporter.Type] = [
{% for reporter in types.structs where reporter.name|hasSuffix:"Reporter" %}
{{ reporter.name }}.self{% if not forloop.last %},{% endif %}
{% endfor %}
Expand Down
9 changes: 9 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ load(
"swift_compiler_plugin"
)

copts = ["-enable-upcoming-feature", "ExistentialAny"]

# Targets

swift_library(
Expand All @@ -17,6 +19,7 @@ swift_library(
"@SwiftSyntax//:SwiftCompilerPlugin_opt",
"@SwiftSyntax//:SwiftSyntaxMacros_opt",
],
copts = copts,
)

swift_compiler_plugin(
Expand All @@ -26,6 +29,7 @@ swift_compiler_plugin(
"@SwiftSyntax//:SwiftCompilerPlugin_opt",
"@SwiftSyntax//:SwiftSyntaxMacros_opt",
],
copts = copts,
)

swift_library(
Expand All @@ -49,6 +53,7 @@ swift_library(
plugins = [
":SwiftLintCoreMacros",
],
copts = copts,
)

swift_library(
Expand All @@ -59,6 +64,7 @@ swift_library(
deps = [
":SwiftLintCore",
],
copts = copts,
)

swift_library(
Expand Down Expand Up @@ -86,6 +92,7 @@ swift_library(
":SwiftLintCore",
":SwiftLintExtraRules",
],
copts = copts,
)

swift_library(
Expand All @@ -99,6 +106,7 @@ swift_library(
"@sourcekitten_com_github_apple_swift_argument_parser//:ArgumentParser",
"@swiftlint_com_github_scottrhoyt_swifty_text_table//:SwiftyTextTable",
],
copts = copts,
)

swift_binary(
Expand All @@ -107,6 +115,7 @@ swift_binary(
deps = [
":swiftlint.library",
],
copts = copts,
)

apple_universal_binary(
Expand Down
34 changes: 24 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import CompilerPluginSupport
import PackageDescription

let swiftFeatures: [SwiftSetting] = [
.enableUpcomingFeature("ExistentialAny")
]

let package = Package(
name: "SwiftLint",
platforms: [.macOS(.v12)],
Expand Down Expand Up @@ -35,13 +39,15 @@ let package = Package(
"CollectionConcurrencyKit",
"SwiftLintFramework",
"SwiftyTextTable",
]
],
swiftSettings: swiftFeatures
),
.testTarget(
name: "CLITests",
dependencies: [
"swiftlint"
]
],
swiftSettings: swiftFeatures
),
.target(
name: "SwiftLintCore",
Expand All @@ -57,11 +63,13 @@ let package = Package(
.product(name: "SwiftyTextTable", package: "SwiftyTextTable"),
.product(name: "Yams", package: "Yams"),
"SwiftLintCoreMacros"
]
],
swiftSettings: swiftFeatures
),
.target(
name: "SwiftLintBuiltInRules",
dependencies: ["SwiftLintCore"]
dependencies: ["SwiftLintCore"],
swiftSettings: swiftFeatures
),
.target(
name: "SwiftLintExtraRules",
Expand All @@ -76,7 +84,8 @@ let package = Package(
// Workaround for https://github.com/apple/swift-package-manager/issues/6940:
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"CollectionConcurrencyKit"
]
],
swiftSettings: swiftFeatures
),
.target(name: "DyldWarningWorkaround"),
.target(
Expand All @@ -95,21 +104,24 @@ let package = Package(
],
exclude: [
"Resources",
]
],
swiftSettings: swiftFeatures
),
.testTarget(
name: "GeneratedTests",
dependencies: [
"SwiftLintFramework",
"SwiftLintTestHelpers"
]
],
swiftSettings: swiftFeatures
),
.testTarget(
name: "IntegrationTests",
dependencies: [
"SwiftLintFramework",
"SwiftLintTestHelpers"
]
],
swiftSettings: swiftFeatures
),
.testTarget(
name: "ExtraRulesTests",
Expand All @@ -129,14 +141,16 @@ let package = Package(
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
],
path: "Source/SwiftLintCoreMacros"
path: "Source/SwiftLintCoreMacros",
swiftSettings: swiftFeatures
),
.testTarget(
name: "MacroTests",
dependencies: [
"SwiftLintCoreMacros",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
],
swiftSettings: swiftFeatures
),
]
)
2 changes: 1 addition & 1 deletion Source/SwiftLintBuiltInRules/Models/BuiltInRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// DO NOT EDIT

/// The rule list containing all available rules built into SwiftLint.
public let builtInRules: [Rule.Type] = [
public let builtInRules: [any Rule.Type] = [
AccessibilityLabelForImageRule.self,
AccessibilityTraitForButtonRule.self,
AnonymousArgumentInMultilineClosureRule.self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct ConvenienceTypeRule: OptInRule, ConfigurationProviderRule {

private extension ConvenienceTypeRule {
final class Visitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] { [ProtocolDeclSyntax.self] }
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { [ProtocolDeclSyntax.self] }

override func visitPost(_ node: StructDeclSyntax) {
if hasViolation(
Expand Down Expand Up @@ -158,7 +158,7 @@ private extension ConvenienceTypeRule {
}

private class ConvenienceTypeCheckVisitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] { .all }
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { .all }

private(set) var canBeConvenienceType = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct ExplicitEnumRawValueRule: OptInRule, ConfigurationProviderRule {

private extension ExplicitEnumRawValueRule {
final class Visitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] { [ProtocolDeclSyntax.self] }
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { [ProtocolDeclSyntax.self] }

override func visitPost(_ node: EnumCaseElementSyntax) {
if node.rawValue == nil, node.enclosingEnum()?.supportsRawValues == true {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct ExplicitInitRule: SwiftSyntaxCorrectableRule, ConfigurationProviderRule,
Visitor(viewMode: .sourceAccurate, includeBareInit: configuration.includeBareInit)
}

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(locationConverter: file.locationConverter, disabledRegions: disabledRegions(file: file))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct ExplicitTopLevelACLRule: OptInRule, ConfigurationProviderRule {

private extension ExplicitTopLevelACLRule {
final class Visitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] { .all }
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { .all }

override func visitPost(_ node: ClassDeclSyntax) {
if hasViolation(modifiers: node.modifiers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private extension ExplicitTypeInterfaceRule {
final class Visitor: ViolationsSyntaxVisitor {
let configuration: ExplicitTypeInterfaceConfiguration

override var skippableDeclarations: [DeclSyntaxProtocol.Type] { [ProtocolDeclSyntax.self] }
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { [ProtocolDeclSyntax.self] }

init(configuration: ExplicitTypeInterfaceConfiguration) {
self.configuration = configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct JoinedDefaultParameterRule: SwiftSyntaxCorrectableRule, ConfigurationProv
]
)

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct LegacyCGGeometryFunctionsRule: SwiftSyntaxCorrectableRule, ConfigurationP
LegacyFunctionRuleHelper.Visitor(legacyFunctions: Self.legacyFunctions)
}

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
LegacyFunctionRuleHelper.Rewriter(
legacyFunctions: Self.legacyFunctions,
locationConverter: file.locationConverter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct LegacyConstantRule: SwiftSyntaxCorrectableRule, ConfigurationProviderRule
corrections: LegacyConstantRuleExamples.corrections
)

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct LegacyConstructorRule: SwiftSyntaxCorrectableRule, ConfigurationProviderR
"NSEdgeInsetsMake": "NSEdgeInsets",
"UIOffsetMake": "UIOffset"]

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct LegacyNSGeometryFunctionsRule: SwiftSyntaxCorrectableRule, ConfigurationP
LegacyFunctionRuleHelper.Visitor(legacyFunctions: Self.legacyFunctions)
}

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
LegacyFunctionRuleHelper.Rewriter(
legacyFunctions: Self.legacyFunctions,
locationConverter: file.locationConverter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct NoExtensionAccessModifierRule: OptInRule, ConfigurationProviderRule {

private extension NoExtensionAccessModifierRule {
final class Visitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] { .all }
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { .all }

override func visitPost(_ node: ExtensionDeclSyntax) {
let modifiers = node.modifiers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private extension NoMagicNumbersRule {
collectViolation(forNode: node)
}

private func collectViolation(forNode node: ExprSyntaxProtocol) {
private func collectViolation(forNode node: some ExprSyntaxProtocol) {
if node.isMemberOfATestClass(testParentClasses) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct PreferZeroOverExplicitInitRule: SwiftSyntaxCorrectableRule, OptInRule, Co
]
)

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct PrivateOverFilePrivateRule: ConfigurationProviderRule, SwiftSyntaxCorrect
Visitor(validateExtensions: configuration.validateExtensions)
}

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
validateExtensions: configuration.validateExtensions,
locationConverter: file.locationConverter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct RedundantNilCoalescingRule: OptInRule, SwiftSyntaxCorrectableRule, Config
]
)

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct RedundantOptionalInitializationRule: SwiftSyntaxCorrectableRule, Configur
""")
]

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct RedundantSetAccessControlRule: ConfigurationProviderRule {

private extension RedundantSetAccessControlRule {
final class Visitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] {
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] {
[FunctionDeclSyntax.self]
}

Expand Down Expand Up @@ -120,7 +120,7 @@ private extension SyntaxProtocol {

private extension DeclSyntax {
var modifiers: DeclModifierListSyntax? {
self.asProtocol(WithModifiersSyntax.self)?.modifiers
self.asProtocol((any WithModifiersSyntax).self)?.modifiers
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct ShorthandOptionalBindingRule: OptInRule, SwiftSyntaxCorrectableRule, Conf
deprecatedAliases: ["if_let_shadowing"]
)

func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
func makeRewriter(file: SwiftLintFile) -> (some ViolationsSyntaxRewriter)? {
Rewriter(
locationConverter: file.locationConverter,
disabledRegions: disabledRegions(file: file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct StaticOperatorRule: ConfigurationProviderRule, OptInRule {

private extension StaticOperatorRule {
final class Visitor: ViolationsSyntaxVisitor {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] { .all }
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { .all }

override func visitPost(_ node: FunctionDeclSyntax) {
if node.isFreeFunction, node.isOperator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private extension StrictFilePrivateRule {
violations.append(node.positionAfterSkippingLeadingTrivia)
}

private func implementedTypesInDecl(of node: SyntaxProtocol?) -> [String] {
private func implementedTypesInDecl(of node: (some SyntaxProtocol)?) -> [String] {
guard let node else {
queuedFatalError("Given node is nil. That should not happen.")
}
Expand Down Expand Up @@ -216,7 +216,7 @@ private final class ProtocolCollector: ViolationsSyntaxVisitor {
private(set) var protocols = [String: [ProtocolRequirementType]]()
private var currentProtocolName: String = ""

override var skippableDeclarations: [DeclSyntaxProtocol.Type] { .allExcept(ProtocolDeclSyntax.self) }
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { .allExcept(ProtocolDeclSyntax.self) }

override func visit(_ node: ProtocolDeclSyntax) -> SyntaxVisitorContinueKind {
currentProtocolName = node.name.text
Expand Down
Loading

0 comments on commit 58928b7

Please sign in to comment.