From 85c4a3a03dc6eeec1e7ca5bfc7400a86fd93f345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Fri, 4 Aug 2023 00:03:26 +0200 Subject: [PATCH] Revert "Allow to initialize `Example`s by string literals (#5154)" (#5155) This reverts commit 56461c3ab4df9d8f78a6f05d2ebeb32b3cae0238. --- .../DiscouragedObjectLiteralRule.swift | 16 +- .../Idiomatic/ExplicitTopLevelACLRule.swift | 28 +-- .../Rules/Idiomatic/ForceCastRule.swift | 4 +- .../Rules/Idiomatic/GenericTypeNameRule.swift | 52 ++--- .../Rules/Idiomatic/IsDisjointRule.swift | 12 +- .../Rules/Idiomatic/LegacyRandomRule.swift | 12 +- .../NoExtensionAccessModifierRule.swift | 14 +- .../Idiomatic/NoGroupingExtensionRule.swift | 14 +- .../PatternMatchingKeywordsRule.swift | 38 ++-- .../Rules/Idiomatic/PreferNimbleRule.swift | 16 +- .../XCTSpecificMatcherRuleExamples.swift | 184 +++++++++--------- .../Lint/ClassDelegateProtocolRule.swift | 30 +-- .../Rules/Lint/CompilerProtocolInitRule.swift | 12 +- .../Lint/DeploymentTargetRuleExamples.swift | 60 +++--- .../Lint/DiscouragedDirectInitRule.swift | 42 ++-- .../Rules/Lint/DynamicInlineRule.swift | 16 +- .../Rules/Lint/ExpiringTodoRule.swift | 28 +-- .../Lint/InvalidSwiftLintCommandRule.swift | 38 ++-- .../Rules/Lint/PrivateActionRule.swift | 34 ++-- .../Rules/Lint/TodoRule.swift | 20 +- .../Metrics/LargeTupleRuleExamples.swift | 98 +++++----- .../ContainsOverFilterCountRule.swift | 18 +- .../ContainsOverFilterIsEmptyRule.swift | 18 +- .../ContainsOverFirstNotNilRule.swift | 16 +- .../ContainsOverRangeNilComparisonRule.swift | 17 +- .../EmptyCollectionLiteralRule.swift | 24 +-- .../Rules/Performance/EmptyCountRule.swift | 38 ++-- .../FlatMapOverMapReduceRule.swift | 6 +- .../Rules/Performance/ReduceBooleanRule.swift | 24 +-- .../OperatorFunctionWhitespaceRule.swift | 18 +- .../Rules/Style/TrailingClosureRule.swift | 26 +-- Source/SwiftLintCore/Models/Example.swift | 7 - 32 files changed, 487 insertions(+), 493 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift index 77c45fdf8..4c95abba2 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift @@ -9,16 +9,16 @@ struct DiscouragedObjectLiteralRule: SwiftSyntaxRule, OptInRule, ConfigurationPr description: "Prefer initializers over object literals", kind: .idiomatic, nonTriggeringExamples: [ - "let image = UIImage(named: aVariable)", - "let image = UIImage(named: \"interpolated \\(variable)\")", - "let color = UIColor(red: value, green: value, blue: value, alpha: 1)", - "let image = NSImage(named: aVariable)", - "let image = NSImage(named: \"interpolated \\(variable)\")", - "let color = NSColor(red: value, green: value, blue: value, alpha: 1)" + Example("let image = UIImage(named: aVariable)"), + Example("let image = UIImage(named: \"interpolated \\(variable)\")"), + Example("let color = UIColor(red: value, green: value, blue: value, alpha: 1)"), + Example("let image = NSImage(named: aVariable)"), + Example("let image = NSImage(named: \"interpolated \\(variable)\")"), + Example("let color = NSColor(red: value, green: value, blue: value, alpha: 1)") ], triggeringExamples: [ - "let image = ↓#imageLiteral(resourceName: \"image.jpg\")", - "let color = ↓#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)" + Example("let image = ↓#imageLiteral(resourceName: \"image.jpg\")"), + Example("let color = ↓#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitTopLevelACLRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitTopLevelACLRule.swift index eb0c8ab47..d502ce06a 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitTopLevelACLRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitTopLevelACLRule.swift @@ -9,22 +9,22 @@ struct ExplicitTopLevelACLRule: SwiftSyntaxRule, OptInRule, ConfigurationProvide description: "Top-level declarations should specify Access Control Level keywords explicitly", kind: .idiomatic, nonTriggeringExamples: [ - "internal enum A {}\n", - "public final class B {}\n", - "private struct C {}\n", - "internal enum A {\n enum B {}\n}", - "internal final class Foo {}", - "internal\nclass Foo {}", - "internal func a() {}\n", - "extension A: Equatable {}", - "extension A {}" + Example("internal enum A {}\n"), + Example("public final class B {}\n"), + Example("private struct C {}\n"), + Example("internal enum A {\n enum B {}\n}"), + Example("internal final class Foo {}"), + Example("internal\nclass Foo {}"), + Example("internal func a() {}\n"), + Example("extension A: Equatable {}"), + Example("extension A {}") ], triggeringExamples: [ - "↓enum A {}\n", - "final ↓class B {}\n", - "↓struct C {}\n", - "↓func a() {}\n", - "internal let a = 0\n↓func b() {}\n" + Example("↓enum A {}\n"), + Example("final ↓class B {}\n"), + Example("↓struct C {}\n"), + Example("↓func a() {}\n"), + Example("internal let a = 0\n↓func b() {}\n") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ForceCastRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ForceCastRule.swift index f227a9548..357bd5130 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ForceCastRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ForceCastRule.swift @@ -9,9 +9,9 @@ struct ForceCastRule: ConfigurationProviderRule, SwiftSyntaxRule { description: "Force casts should be avoided", kind: .idiomatic, nonTriggeringExamples: [ - "NSNumber() as? Int\n" + Example("NSNumber() as? Int\n") ], - triggeringExamples: [ "NSNumber() ↓as! Int\n" ] + triggeringExamples: [ Example("NSNumber() ↓as! Int\n") ] ) func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor { diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/GenericTypeNameRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/GenericTypeNameRule.swift index 8ebbe3694..167215da7 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/GenericTypeNameRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/GenericTypeNameRule.swift @@ -14,36 +14,36 @@ struct GenericTypeNameRule: SwiftSyntaxRule, ConfigurationProviderRule { "uppercase character and span between 1 and 20 characters in length.", kind: .idiomatic, nonTriggeringExamples: [ - "func foo() {}\n", - "func foo() -> T {}\n", - "func foo(param: U) -> T {}\n", - "func foo(param: U) -> T {}\n", - "struct Foo {}\n", - "class Foo {}\n", - "enum Foo {}\n", - "func run(_ options: NoOptions>) {}\n", - "func foo(_ options: Set) {}\n", - "func < (lhs: T?, rhs: T?) -> Bool\n", - "func configureWith(data: Either)\n", - "typealias StringDictionary = Dictionary\n", - "typealias BackwardTriple = (T3, T2, T1)\n", - "typealias DictionaryOfStrings = Dictionary\n" + Example("func foo() {}\n"), + Example("func foo() -> T {}\n"), + Example("func foo(param: U) -> T {}\n"), + Example("func foo(param: U) -> T {}\n"), + Example("struct Foo {}\n"), + Example("class Foo {}\n"), + Example("enum Foo {}\n"), + Example("func run(_ options: NoOptions>) {}\n"), + Example("func foo(_ options: Set) {}\n"), + Example("func < (lhs: T?, rhs: T?) -> Bool\n"), + Example("func configureWith(data: Either)\n"), + Example("typealias StringDictionary = Dictionary\n"), + Example("typealias BackwardTriple = (T3, T2, T1)\n"), + Example("typealias DictionaryOfStrings = Dictionary\n") ], triggeringExamples: [ - "func foo<↓T_Foo>() {}\n", - "func foo(param: U_Foo) -> T {}\n", - "func foo<↓\(String(repeating: "T", count: 21))>() {}\n", - "func foo<↓type>() {}\n", - "typealias StringDictionary<↓T_Foo> = Dictionary\n", - "typealias BackwardTriple = (T3, T2_Bar, T1)\n", - "typealias DictionaryOfStrings<↓T_Foo: Hashable> = Dictionary\n" + Example("func foo<↓T_Foo>() {}\n"), + Example("func foo(param: U_Foo) -> T {}\n"), + Example("func foo<↓\(String(repeating: "T", count: 21))>() {}\n"), + Example("func foo<↓type>() {}\n"), + Example("typealias StringDictionary<↓T_Foo> = Dictionary\n"), + Example("typealias BackwardTriple = (T3, T2_Bar, T1)\n"), + Example("typealias DictionaryOfStrings<↓T_Foo: Hashable> = Dictionary\n") ] + ["class", "struct", "enum"].flatMap { type -> [Example] in return [ - "\(type) Foo<↓T_Foo> {}\n", - "\(type) Foo {}\n", - "\(type) Foo<↓T_Foo, ↓U_Foo> {}\n", - "\(type) Foo<↓\(String(repeating: "T", count: 21))> {}\n", - "\(type) Foo<↓type> {}\n" + Example("\(type) Foo<↓T_Foo> {}\n"), + Example("\(type) Foo {}\n"), + Example("\(type) Foo<↓T_Foo, ↓U_Foo> {}\n"), + Example("\(type) Foo<↓\(String(repeating: "T", count: 21))> {}\n"), + Example("\(type) Foo<↓type> {}\n") ] } ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/IsDisjointRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/IsDisjointRule.swift index 8ab0935fd..351f4f7d5 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/IsDisjointRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/IsDisjointRule.swift @@ -9,14 +9,14 @@ struct IsDisjointRule: SwiftSyntaxRule, ConfigurationProviderRule { description: "Prefer using `Set.isDisjoint(with:)` over `Set.intersection(_:).isEmpty`", kind: .idiomatic, nonTriggeringExamples: [ - "_ = Set(syntaxKinds).isDisjoint(with: commentAndStringKindsSet)", - "let isObjc = !objcAttributes.isDisjoint(with: dictionary.enclosedSwiftAttributes)", - "_ = Set(syntaxKinds).intersection(commentAndStringKindsSet)", - "_ = !objcAttributes.intersection(dictionary.enclosedSwiftAttributes)" + Example("_ = Set(syntaxKinds).isDisjoint(with: commentAndStringKindsSet)"), + Example("let isObjc = !objcAttributes.isDisjoint(with: dictionary.enclosedSwiftAttributes)"), + Example("_ = Set(syntaxKinds).intersection(commentAndStringKindsSet)"), + Example("_ = !objcAttributes.intersection(dictionary.enclosedSwiftAttributes)") ], triggeringExamples: [ - "_ = Set(syntaxKinds).↓intersection(commentAndStringKindsSet).isEmpty", - "let isObjc = !objcAttributes.↓intersection(dictionary.enclosedSwiftAttributes).isEmpty" + Example("_ = Set(syntaxKinds).↓intersection(commentAndStringKindsSet).isEmpty"), + Example("let isObjc = !objcAttributes.↓intersection(dictionary.enclosedSwiftAttributes).isEmpty") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyRandomRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyRandomRule.swift index 1147d7a34..2dc5c3182 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyRandomRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/LegacyRandomRule.swift @@ -9,14 +9,14 @@ struct LegacyRandomRule: SwiftSyntaxRule, ConfigurationProviderRule { description: "Prefer using `type.random(in:)` over legacy functions", kind: .idiomatic, nonTriggeringExamples: [ - "Int.random(in: 0..<10)\n", - "Double.random(in: 8.6...111.34)\n", - "Float.random(in: 0 ..< 1)\n" + Example("Int.random(in: 0..<10)\n"), + Example("Double.random(in: 8.6...111.34)\n"), + Example("Float.random(in: 0 ..< 1)\n") ], triggeringExamples: [ - "↓arc4random()\n", - "↓arc4random_uniform(83)\n", - "↓drand48()\n" + Example("↓arc4random()\n"), + Example("↓arc4random_uniform(83)\n"), + Example("↓drand48()\n") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoExtensionAccessModifierRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoExtensionAccessModifierRule.swift index 0070a2f4e..c8b2cd57b 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoExtensionAccessModifierRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoExtensionAccessModifierRule.swift @@ -9,15 +9,15 @@ struct NoExtensionAccessModifierRule: SwiftSyntaxRule, OptInRule, ConfigurationP description: "Prefer not to use extension access modifiers", kind: .idiomatic, nonTriggeringExamples: [ - "extension String {}", - "\n\n extension String {}" + Example("extension String {}"), + Example("\n\n extension String {}") ], triggeringExamples: [ - "↓private extension String {}", - "↓public \n extension String {}", - "↓open extension String {}", - "↓internal extension String {}", - "↓fileprivate extension String {}" + Example("↓private extension String {}"), + Example("↓public \n extension String {}"), + Example("↓open extension String {}"), + Example("↓internal extension String {}"), + Example("↓fileprivate extension String {}") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoGroupingExtensionRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoGroupingExtensionRule.swift index 5764674f2..36fc72ab7 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoGroupingExtensionRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoGroupingExtensionRule.swift @@ -9,15 +9,15 @@ struct NoGroupingExtensionRule: OptInRule, ConfigurationProviderRule { description: "Extensions shouldn't be used to group code within the same source file", kind: .idiomatic, nonTriggeringExamples: [ - "protocol Food {}\nextension Food {}\n", - "class Apples {}\nextension Oranges {}\n", - "class Box {}\nextension Box where T: Vegetable {}\n" + Example("protocol Food {}\nextension Food {}\n"), + Example("class Apples {}\nextension Oranges {}\n"), + Example("class Box {}\nextension Box where T: Vegetable {}\n") ], triggeringExamples: [ - "enum Fruit {}\n↓extension Fruit {}\n", - "↓extension Tea: Error {}\nstruct Tea {}\n", - "class Ham { class Spam {}}\n↓extension Ham.Spam {}\n", - "extension External { struct Gotcha {}}\n↓extension External.Gotcha {}\n" + Example("enum Fruit {}\n↓extension Fruit {}\n"), + Example("↓extension Tea: Error {}\nstruct Tea {}\n"), + Example("class Ham { class Spam {}}\n↓extension Ham.Spam {}\n"), + Example("extension External { struct Gotcha {}}\n↓extension External.Gotcha {}\n") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PatternMatchingKeywordsRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PatternMatchingKeywordsRule.swift index f24532fcc..dd9676424 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PatternMatchingKeywordsRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PatternMatchingKeywordsRule.swift @@ -9,27 +9,27 @@ struct PatternMatchingKeywordsRule: SwiftSyntaxRule, ConfigurationProviderRule, description: "Combine multiple pattern matching bindings by moving keywords out of tuples", kind: .idiomatic, nonTriggeringExamples: [ - "default", - "case 1", - "case bar", - "case let (x, y)", - "case .foo(let x)", - "case let .foo(x, y)", - "case .foo(let x), .bar(let x)", - "case .foo(let x, var y)", - "case var (x, y)", - "case .foo(var x)", - "case var .foo(x, y)" + Example("default"), + Example("case 1"), + Example("case bar"), + Example("case let (x, y)"), + Example("case .foo(let x)"), + Example("case let .foo(x, y)"), + Example("case .foo(let x), .bar(let x)"), + Example("case .foo(let x, var y)"), + Example("case var (x, y)"), + Example("case .foo(var x)"), + Example("case var .foo(x, y)") ].map(wrapInSwitch), triggeringExamples: [ - "case (↓let x, ↓let y)", - "case (↓let x, ↓let y, .foo)", - "case (↓let x, ↓let y, _)", - "case .foo(↓let x, ↓let y)", - "case (.yamlParsing(↓let x), .yamlParsing(↓let y))", - "case (↓var x, ↓var y)", - "case .foo(↓var x, ↓var y)", - "case (.yamlParsing(↓var x), .yamlParsing(↓var y))" + Example("case (↓let x, ↓let y)"), + Example("case (↓let x, ↓let y, .foo)"), + Example("case (↓let x, ↓let y, _)"), + Example("case .foo(↓let x, ↓let y)"), + Example("case (.yamlParsing(↓let x), .yamlParsing(↓let y))"), + Example("case (↓var x, ↓var y)"), + Example("case .foo(↓var x, ↓var y)"), + Example("case (.yamlParsing(↓var x), .yamlParsing(↓var y))") ].map(wrapInSwitch) ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferNimbleRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferNimbleRule.swift index 527438302..eb7379ce3 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferNimbleRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferNimbleRule.swift @@ -9,16 +9,16 @@ struct PreferNimbleRule: SwiftSyntaxRule, OptInRule, ConfigurationProviderRule { description: "Prefer Nimble matchers over XCTAssert functions", kind: .idiomatic, nonTriggeringExamples: [ - "expect(foo) == 1", - "expect(foo).to(equal(1))" + Example("expect(foo) == 1"), + Example("expect(foo).to(equal(1))") ], triggeringExamples: [ - "↓XCTAssertTrue(foo)", - "↓XCTAssertEqual(foo, 2)", - "↓XCTAssertNotEqual(foo, 2)", - "↓XCTAssertNil(foo)", - "↓XCTAssert(foo)", - "↓XCTAssertGreaterThan(foo, 10)" + Example("↓XCTAssertTrue(foo)"), + Example("↓XCTAssertEqual(foo, 2)"), + Example("↓XCTAssertNotEqual(foo, 2)"), + Example("↓XCTAssertNil(foo)"), + Example("↓XCTAssert(foo)"), + Example("↓XCTAssertGreaterThan(foo, 10)") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTSpecificMatcherRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTSpecificMatcherRuleExamples.swift index 1b5f7ac81..accabe8f7 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTSpecificMatcherRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTSpecificMatcherRuleExamples.swift @@ -1,56 +1,56 @@ internal struct XCTSpecificMatcherRuleExamples { - static let nonTriggeringExamples: [Example] = [ + static let nonTriggeringExamples = [ // True/False - "XCTAssert(foo)", - "XCTAssertFalse(foo)", - "XCTAssertTrue(foo)", + Example("XCTAssert(foo"), + Example("XCTAssertFalse(foo)"), + Example("XCTAssertTrue(foo)"), // Nil/Not nil - "XCTAssertNil(foo)", - "XCTAssertNotNil(foo)", + Example("XCTAssertNil(foo)"), + Example("XCTAssertNotNil(foo)"), // Equal/Not equal - "XCTAssertEqual(foo, 2)", - "XCTAssertNotEqual(foo, \"false\")", + Example("XCTAssertEqual(foo, 2)"), + Example("XCTAssertNotEqual(foo, \"false\")"), // Arrays with key words - "XCTAssertEqual(foo, [1, 2, 3, true])", - "XCTAssertEqual(foo, [1, 2, 3, false])", - "XCTAssertEqual(foo, [1, 2, 3, nil])", - "XCTAssertEqual(foo, [true, nil, true, nil])", - "XCTAssertEqual([1, 2, 3, true], foo)", - "XCTAssertEqual([1, 2, 3, false], foo)", - "XCTAssertEqual([1, 2, 3, nil], foo)", - "XCTAssertEqual([true, nil, true, nil], foo)", + Example("XCTAssertEqual(foo, [1, 2, 3, true])"), + Example("XCTAssertEqual(foo, [1, 2, 3, false])"), + Example("XCTAssertEqual(foo, [1, 2, 3, nil])"), + Example("XCTAssertEqual(foo, [true, nil, true, nil])"), + Example("XCTAssertEqual([1, 2, 3, true], foo)"), + Example("XCTAssertEqual([1, 2, 3, false], foo)"), + Example("XCTAssertEqual([1, 2, 3, nil], foo)"), + Example("XCTAssertEqual([true, nil, true, nil], foo)"), // Inverted logic - "XCTAssertEqual(2, foo)", - "XCTAssertNotEqual(\"false\"), foo)", - "XCTAssertEqual(false, foo?.bar)", - "XCTAssertEqual(true, foo?.bar)", + Example("XCTAssertEqual(2, foo)"), + Example("XCTAssertNotEqual(\"false\"), foo)"), + Example("XCTAssertEqual(false, foo?.bar)"), + Example("XCTAssertEqual(true, foo?.bar)"), // Blank spaces - "XCTAssert( foo )", - "XCTAssertFalse( foo )", - "XCTAssertTrue( foo )", - "XCTAssertNil( foo )", - "XCTAssertNotNil( foo )", - "XCTAssertEqual( foo , 2 )", - "XCTAssertNotEqual( foo, \"false\")", + Example("XCTAssert( foo )"), + Example("XCTAssertFalse( foo )"), + Example("XCTAssertTrue( foo )"), + Example("XCTAssertNil( foo )"), + Example("XCTAssertNotNil( foo )"), + Example("XCTAssertEqual( foo , 2 )"), + Example("XCTAssertNotEqual( foo, \"false\")"), // Optionals - "XCTAssertEqual(foo?.bar, false)", - "XCTAssertEqual(foo?.bar, true)", - "XCTAssertNil(foo?.bar)", - "XCTAssertNotNil(foo?.bar)", - "XCTAssertEqual(foo?.bar, 2)", - "XCTAssertNotEqual(foo?.bar, \"false\")", + Example("XCTAssertEqual(foo?.bar, false)"), + Example("XCTAssertEqual(foo?.bar, true)"), + Example("XCTAssertNil(foo?.bar)"), + Example("XCTAssertNotNil(foo?.bar)"), + Example("XCTAssertEqual(foo?.bar, 2)"), + Example("XCTAssertNotEqual(foo?.bar, \"false\")"), // Function calls and enums - "XCTAssertEqual(foo?.bar, toto())", - "XCTAssertEqual(foo?.bar, .toto(.zoo))", - "XCTAssertEqual(toto(), foo?.bar)", - "XCTAssertEqual(.toto(.zoo), foo?.bar)", + Example("XCTAssertEqual(foo?.bar, toto())"), + Example("XCTAssertEqual(foo?.bar, .toto(.zoo))"), + Example("XCTAssertEqual(toto(), foo?.bar)"), + Example("XCTAssertEqual(.toto(.zoo), foo?.bar)"), // Configurations Disabled Example("XCTAssertEqual(foo, true)", @@ -61,81 +61,81 @@ internal struct XCTSpecificMatcherRuleExamples { excludeFromDocumentation: true), // Skip if one operand might be a type or a tuple - "XCTAssert(foo.self == bar)", - "XCTAssertTrue(type(of: foo) != Int.self)", - "XCTAssertTrue(a == (1, 3, 5)" + Example("XCTAssert(foo.self == bar)"), + Example("XCTAssertTrue(type(of: foo) != Int.self)"), + Example("XCTAssertTrue(a == (1, 3, 5)") ] - static let triggeringExamples: [Example] = [ + static let triggeringExamples = [ // Without message - "↓XCTAssertEqual(foo, true)", - "↓XCTAssertEqual(foo, false)", - "↓XCTAssertEqual(foo, nil)", - "↓XCTAssertNotEqual(foo, true)", - "↓XCTAssertNotEqual(foo, false)", - "↓XCTAssertNotEqual(foo, nil)", + Example("↓XCTAssertEqual(foo, true)"), + Example("↓XCTAssertEqual(foo, false)"), + Example("↓XCTAssertEqual(foo, nil)"), + Example("↓XCTAssertNotEqual(foo, true)"), + Example("↓XCTAssertNotEqual(foo, false)"), + Example("↓XCTAssertNotEqual(foo, nil)"), // Inverted logic (just in case...) - "↓XCTAssertEqual(true, foo)", - "↓XCTAssertEqual(false, foo)", - "↓XCTAssertEqual(nil, foo)", - "↓XCTAssertNotEqual(true, foo)", - "↓XCTAssertNotEqual(false, foo)", - "↓XCTAssertNotEqual(nil, foo)", + Example("↓XCTAssertEqual(true, foo)"), + Example("↓XCTAssertEqual(false, foo)"), + Example("↓XCTAssertEqual(nil, foo)"), + Example("↓XCTAssertNotEqual(true, foo)"), + Example("↓XCTAssertNotEqual(false, foo)"), + Example("↓XCTAssertNotEqual(nil, foo)"), // With message - "↓XCTAssertEqual(foo, true, \"toto\")", - "↓XCTAssertEqual(foo, false, \"toto\")", - "↓XCTAssertEqual(foo, nil, \"toto\")", - "↓XCTAssertNotEqual(foo, true, \"toto\")", - "↓XCTAssertNotEqual(foo, false, \"toto\")", - "↓XCTAssertNotEqual(foo, nil, \"toto\")", - "↓XCTAssertEqual(true, foo, \"toto\")", - "↓XCTAssertEqual(false, foo, \"toto\")", - "↓XCTAssertEqual(nil, foo, \"toto\")", - "↓XCTAssertNotEqual(true, foo, \"toto\")", - "↓XCTAssertNotEqual(false, foo, \"toto\")", - "↓XCTAssertNotEqual(nil, foo, \"toto\")", + Example("↓XCTAssertEqual(foo, true, \"toto\")"), + Example("↓XCTAssertEqual(foo, false, \"toto\")"), + Example("↓XCTAssertEqual(foo, nil, \"toto\")"), + Example("↓XCTAssertNotEqual(foo, true, \"toto\")"), + Example("↓XCTAssertNotEqual(foo, false, \"toto\")"), + Example("↓XCTAssertNotEqual(foo, nil, \"toto\")"), + Example("↓XCTAssertEqual(true, foo, \"toto\")"), + Example("↓XCTAssertEqual(false, foo, \"toto\")"), + Example("↓XCTAssertEqual(nil, foo, \"toto\")"), + Example("↓XCTAssertNotEqual(true, foo, \"toto\")"), + Example("↓XCTAssertNotEqual(false, foo, \"toto\")"), + Example("↓XCTAssertNotEqual(nil, foo, \"toto\")"), // Blank spaces - "↓XCTAssertEqual(foo,true)", - "↓XCTAssertEqual( foo , false )", - "↓XCTAssertEqual( foo , nil )", + Example("↓XCTAssertEqual(foo,true)"), + Example("↓XCTAssertEqual( foo , false )"), + Example("↓XCTAssertEqual( foo , nil )"), // Arrays - "↓XCTAssertEqual(true, [1, 2, 3, true].hasNumbers())", - "↓XCTAssertEqual([1, 2, 3, true].hasNumbers(), true)", + Example("↓XCTAssertEqual(true, [1, 2, 3, true].hasNumbers())"), + Example("↓XCTAssertEqual([1, 2, 3, true].hasNumbers(), true)"), // Optionals - "↓XCTAssertEqual(foo?.bar, nil)", - "↓XCTAssertNotEqual(foo?.bar, nil)", + Example("↓XCTAssertEqual(foo?.bar, nil)"), + Example("↓XCTAssertNotEqual(foo?.bar, nil)"), // Weird cases - "↓XCTAssertEqual(nil, true)", - "↓XCTAssertEqual(nil, false)", - "↓XCTAssertEqual(true, nil)", - "↓XCTAssertEqual(false, nil)", - "↓XCTAssertEqual(nil, nil)", - "↓XCTAssertEqual(true, true)", - "↓XCTAssertEqual(false, false)", + Example("↓XCTAssertEqual(nil, true)"), + Example("↓XCTAssertEqual(nil, false)"), + Example("↓XCTAssertEqual(true, nil)"), + Example("↓XCTAssertEqual(false, nil)"), + Example("↓XCTAssertEqual(nil, nil)"), + Example("↓XCTAssertEqual(true, true)"), + Example("↓XCTAssertEqual(false, false)"), // Equality with `==` - "↓XCTAssert(foo == bar)", - "↓XCTAssertTrue( foo == bar )", - "↓XCTAssertFalse(1 == foo)", - "↓XCTAssert(foo == bar, \"toto\")", + Example("↓XCTAssert(foo == bar)"), + Example("↓XCTAssertTrue( foo == bar )"), + Example("↓XCTAssertFalse(1 == foo)"), + Example("↓XCTAssert(foo == bar, \"toto\")"), // Inequality with `!=` - "↓XCTAssert(foo != bar)", - "↓XCTAssertTrue( foo != bar )", - "↓XCTAssertFalse(1 != foo)", - "↓XCTAssert(foo != bar, \"toto\")", + Example("↓XCTAssert(foo != bar)"), + Example("↓XCTAssertTrue( foo != bar )"), + Example("↓XCTAssertFalse(1 != foo)"), + Example("↓XCTAssert(foo != bar, \"toto\")"), // Comparison with `nil` - "↓XCTAssert( foo == nil)", - "↓XCTAssert(nil == foo", - "↓XCTAssertTrue( foo != nil)", - "↓XCTAssertFalse(nil != foo", - "↓XCTAssert(foo == nil, \"toto\")" + Example("↓XCTAssert( foo == nil)"), + Example("↓XCTAssert(nil == foo"), + Example("↓XCTAssertTrue( foo != nil)"), + Example("↓XCTAssertFalse(nil != foo"), + Example("↓XCTAssert(foo == nil, \"toto\")") ] } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/ClassDelegateProtocolRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/ClassDelegateProtocolRule.swift index 94303e4a8..e20fd09f6 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/ClassDelegateProtocolRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/ClassDelegateProtocolRule.swift @@ -9,23 +9,23 @@ struct ClassDelegateProtocolRule: SwiftSyntaxRule, ConfigurationProviderRule { description: "Delegate protocols should be class-only so they can be weakly referenced", kind: .lint, nonTriggeringExamples: [ - "protocol FooDelegate: class {}\n", - "protocol FooDelegate: class, BarDelegate {}\n", - "protocol Foo {}\n", - "class FooDelegate {}\n", - "@objc protocol FooDelegate {}\n", - "@objc(MyFooDelegate)\n protocol FooDelegate {}\n", - "protocol FooDelegate: BarDelegate {}\n", - "protocol FooDelegate: AnyObject {}\n", - "protocol FooDelegate: NSObjectProtocol {}\n", - "protocol FooDelegate where Self: BarDelegate {}\n", - "protocol FooDelegate where Self: AnyObject {}\n", - "protocol FooDelegate where Self: NSObjectProtocol {}\n" + Example("protocol FooDelegate: class {}\n"), + Example("protocol FooDelegate: class, BarDelegate {}\n"), + Example("protocol Foo {}\n"), + Example("class FooDelegate {}\n"), + Example("@objc protocol FooDelegate {}\n"), + Example("@objc(MyFooDelegate)\n protocol FooDelegate {}\n"), + Example("protocol FooDelegate: BarDelegate {}\n"), + Example("protocol FooDelegate: AnyObject {}\n"), + Example("protocol FooDelegate: NSObjectProtocol {}\n"), + Example("protocol FooDelegate where Self: BarDelegate {}\n"), + Example("protocol FooDelegate where Self: AnyObject {}\n"), + Example("protocol FooDelegate where Self: NSObjectProtocol {}\n") ], triggeringExamples: [ - "↓protocol FooDelegate {}\n", - "↓protocol FooDelegate: Bar {}\n", - "↓protocol FooDelegate where Self: StringProtocol {}\n" + Example("↓protocol FooDelegate {}\n"), + Example("↓protocol FooDelegate: Bar {}\n"), + Example("↓protocol FooDelegate where Self: StringProtocol {}\n") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/CompilerProtocolInitRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/CompilerProtocolInitRule.swift index 7afcae5a8..a8fd946b4 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/CompilerProtocolInitRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/CompilerProtocolInitRule.swift @@ -10,14 +10,14 @@ struct CompilerProtocolInitRule: SwiftSyntaxRule, ConfigurationProviderRule { "shouldn't be called directly.", kind: .lint, nonTriggeringExamples: [ - "let set: Set = [1, 2]\n", - "let set = Set(array)\n" + Example("let set: Set = [1, 2]\n"), + Example("let set = Set(array)\n") ], triggeringExamples: [ - "let set = ↓Set(arrayLiteral: 1, 2)\n", - "let set = ↓Set (arrayLiteral: 1, 2)\n", - "let set = ↓Set.init(arrayLiteral: 1, 2)\n", - "let set = ↓Set.init(arrayLiteral : 1, 2)\n" + Example("let set = ↓Set(arrayLiteral: 1, 2)\n"), + Example("let set = ↓Set (arrayLiteral: 1, 2)\n"), + Example("let set = ↓Set.init(arrayLiteral: 1, 2)\n"), + Example("let set = ↓Set.init(arrayLiteral : 1, 2)\n") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/DeploymentTargetRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/DeploymentTargetRuleExamples.swift index ff598005f..a22c0c8c3 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/DeploymentTargetRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/DeploymentTargetRuleExamples.swift @@ -1,37 +1,37 @@ internal enum DeploymentTargetRuleExamples { static let nonTriggeringExamples: [Example] = [ - "@available(iOS 12.0, *)\nclass A {}", - "@available(iOSApplicationExtension 13.0, *)\nclass A {}", - "@available(watchOS 4.0, *)\nclass A {}", - "@available(watchOSApplicationExtension 4.0, *)\nclass A {}", - "@available(swift 3.0.2)\nclass A {}", - "class A {}", - "if #available(iOS 10.0, *) {}", - "if #available(iOS 10, *) {}", - "guard #available(iOS 12.0, *) else { return }", - "#if #unavailable(iOS 15.0) {}", - "#guard #unavailable(iOS 15.0) {} else { return }" + Example("@available(iOS 12.0, *)\nclass A {}"), + Example("@available(iOSApplicationExtension 13.0, *)\nclass A {}"), + Example("@available(watchOS 4.0, *)\nclass A {}"), + Example("@available(watchOSApplicationExtension 4.0, *)\nclass A {}"), + Example("@available(swift 3.0.2)\nclass A {}"), + Example("class A {}"), + Example("if #available(iOS 10.0, *) {}"), + Example("if #available(iOS 10, *) {}"), + Example("guard #available(iOS 12.0, *) else { return }"), + Example("#if #unavailable(iOS 15.0) {}"), + Example("#guard #unavailable(iOS 15.0) {} else { return }") ] static let triggeringExamples: [Example] = [ - "↓@available(iOS 6.0, *)\nclass A {}", - "↓@available(iOSApplicationExtension 6.0, *)\nclass A {}", - "↓@available(iOS 7.0, *)\nclass A {}", - "↓@available(iOS 6, *)\nclass A {}", - "↓@available(iOS 6.0, macOS 10.12, *)\n class A {}", - "↓@available(macOS 10.12, iOS 6.0, *)\n class A {}", - "↓@available(macOS 10.7, *)\nclass A {}", - "↓@available(macOSApplicationExtension 10.7, *)\nclass A {}", - "↓@available(OSX 10.7, *)\nclass A {}", - "↓@available(watchOS 0.9, *)\nclass A {}", - "↓@available(watchOSApplicationExtension 0.9, *)\nclass A {}", - "↓@available(tvOS 8, *)\nclass A {}", - "↓@available(tvOSApplicationExtension 8, *)\nclass A {}", - "if ↓#available(iOS 6.0, *) {}", - "if ↓#available(iOS 6, *) {}", - "guard ↓#available(iOS 6.0, *) else { return }", - "if ↓#unavailable(iOS 7.0) {}", - "if ↓#unavailable(iOS 6.9) {}", - "guard ↓#unavailable(iOS 7.0) {} else { return }" + Example("↓@available(iOS 6.0, *)\nclass A {}"), + Example("↓@available(iOSApplicationExtension 6.0, *)\nclass A {}"), + Example("↓@available(iOS 7.0, *)\nclass A {}"), + Example("↓@available(iOS 6, *)\nclass A {}"), + Example("↓@available(iOS 6.0, macOS 10.12, *)\n class A {}"), + Example("↓@available(macOS 10.12, iOS 6.0, *)\n class A {}"), + Example("↓@available(macOS 10.7, *)\nclass A {}"), + Example("↓@available(macOSApplicationExtension 10.7, *)\nclass A {}"), + Example("↓@available(OSX 10.7, *)\nclass A {}"), + Example("↓@available(watchOS 0.9, *)\nclass A {}"), + Example("↓@available(watchOSApplicationExtension 0.9, *)\nclass A {}"), + Example("↓@available(tvOS 8, *)\nclass A {}"), + Example("↓@available(tvOSApplicationExtension 8, *)\nclass A {}"), + Example("if ↓#available(iOS 6.0, *) {}"), + Example("if ↓#available(iOS 6, *) {}"), + Example("guard ↓#available(iOS 6.0, *) else { return }"), + Example("if ↓#unavailable(iOS 7.0) {}"), + Example("if ↓#unavailable(iOS 6.9) {}"), + Example("guard ↓#unavailable(iOS 7.0) {} else { return }") ] } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/DiscouragedDirectInitRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/DiscouragedDirectInitRule.swift index 219e3e7e6..df5e6114f 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/DiscouragedDirectInitRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/DiscouragedDirectInitRule.swift @@ -9,29 +9,29 @@ struct DiscouragedDirectInitRule: SwiftSyntaxRule, ConfigurationProviderRule { description: "Discouraged direct initialization of types that can be harmful", kind: .lint, nonTriggeringExamples: [ - "let foo = UIDevice.current", - "let foo = Bundle.main", - "let foo = Bundle(path: \"bar\")", - "let foo = Bundle(identifier: \"bar\")", - "let foo = Bundle.init(path: \"bar\")", - "let foo = Bundle.init(identifier: \"bar\")", - "let foo = NSError(domain: \"bar\", code: 0)", - "let foo = NSError.init(domain: \"bar\", code: 0)", - "func testNSError()" + Example("let foo = UIDevice.current"), + Example("let foo = Bundle.main"), + Example("let foo = Bundle(path: \"bar\")"), + Example("let foo = Bundle(identifier: \"bar\")"), + Example("let foo = Bundle.init(path: \"bar\")"), + Example("let foo = Bundle.init(identifier: \"bar\")"), + Example("let foo = NSError(domain: \"bar\", code: 0)"), + Example("let foo = NSError.init(domain: \"bar\", code: 0)"), + Example("func testNSError()") ], triggeringExamples: [ - "↓UIDevice()", - "↓Bundle()", - "let foo = ↓UIDevice()", - "let foo = ↓Bundle()", - "let foo = ↓NSError()", - "let foo = bar(bundle: ↓Bundle(), device: ↓UIDevice(), error: ↓NSError())", - "↓UIDevice.init()", - "↓Bundle.init()", - "↓NSError.init()", - "let foo = ↓UIDevice.init()", - "let foo = ↓Bundle.init()", - "let foo = bar(bundle: ↓Bundle.init(), device: ↓UIDevice.init(), error: ↓NSError.init())" + Example("↓UIDevice()"), + Example("↓Bundle()"), + Example("let foo = ↓UIDevice()"), + Example("let foo = ↓Bundle()"), + Example("let foo = ↓NSError()"), + Example("let foo = bar(bundle: ↓Bundle(), device: ↓UIDevice(), error: ↓NSError())"), + Example("↓UIDevice.init()"), + Example("↓Bundle.init()"), + Example("↓NSError.init()"), + Example("let foo = ↓UIDevice.init()"), + Example("let foo = ↓Bundle.init()"), + Example("let foo = bar(bundle: ↓Bundle.init(), device: ↓UIDevice.init(), error: ↓NSError.init())") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/DynamicInlineRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/DynamicInlineRule.swift index a7dd9fc5c..bbd5c35ae 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/DynamicInlineRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/DynamicInlineRule.swift @@ -9,16 +9,16 @@ struct DynamicInlineRule: SwiftSyntaxRule, ConfigurationProviderRule { description: "Avoid using 'dynamic' and '@inline(__always)' together", kind: .lint, nonTriggeringExamples: [ - "class C {\ndynamic func f() {}}", - "class C {\n@inline(__always) func f() {}}", - "class C {\n@inline(never) dynamic func f() {}}" + Example("class C {\ndynamic func f() {}}"), + Example("class C {\n@inline(__always) func f() {}}"), + Example("class C {\n@inline(never) dynamic func f() {}}") ], triggeringExamples: [ - "class C {\n@inline(__always) dynamic ↓func f() {}\n}", - "class C {\n@inline(__always) public dynamic ↓func f() {}\n}", - "class C {\n@inline(__always) dynamic internal ↓func f() {}\n}", - "class C {\n@inline(__always)\ndynamic ↓func f() {}\n}", - "class C {\n@inline(__always)\ndynamic\n↓func f() {}\n}" + Example("class C {\n@inline(__always) dynamic ↓func f() {}\n}"), + Example("class C {\n@inline(__always) public dynamic ↓func f() {}\n}"), + Example("class C {\n@inline(__always) dynamic internal ↓func f() {}\n}"), + Example("class C {\n@inline(__always)\ndynamic ↓func f() {}\n}"), + Example("class C {\n@inline(__always)\ndynamic\n↓func f() {}\n}") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/ExpiringTodoRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/ExpiringTodoRule.swift index d1e754ff4..5cc4773c7 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/ExpiringTodoRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/ExpiringTodoRule.swift @@ -25,22 +25,22 @@ struct ExpiringTodoRule: ConfigurationProviderRule, OptInRule { description: "TODOs and FIXMEs should be resolved prior to their expiry date.", kind: .lint, nonTriggeringExamples: [ - "// notaTODO:\n", - "// notaFIXME:\n", - "// TODO: [12/31/9999]\n", - "// TODO(note)\n", - "// FIXME(note)\n", - "/* FIXME: */\n", - "/* TODO: */\n", - "/** FIXME: */\n", - "/** TODO: */\n" + Example("// notaTODO:\n"), + Example("// notaFIXME:\n"), + Example("// TODO: [12/31/9999]\n"), + Example("// TODO(note)\n"), + Example("// FIXME(note)\n"), + Example("/* FIXME: */\n"), + Example("/* TODO: */\n"), + Example("/** FIXME: */\n"), + Example("/** TODO: */\n") ], triggeringExamples: [ - "// TODO: [↓10/14/2019]\n", - "// FIXME: [↓10/14/2019]\n", - "// FIXME: [↓1/14/2019]\n", - "// FIXME: [↓10/14/2019]\n", - "// TODO: [↓9999/14/10]\n" + Example("// TODO: [↓10/14/2019]\n"), + Example("// FIXME: [↓10/14/2019]\n"), + Example("// FIXME: [↓1/14/2019]\n"), + Example("// FIXME: [↓10/14/2019]\n"), + Example("// TODO: [↓9999/14/10]\n") ].skipWrappingInCommentTests() ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/InvalidSwiftLintCommandRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/InvalidSwiftLintCommandRule.swift index 6936f7527..058f35934 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/InvalidSwiftLintCommandRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/InvalidSwiftLintCommandRule.swift @@ -7,27 +7,27 @@ struct InvalidSwiftLintCommandRule: ConfigurationProviderRule { description: "swiftlint command does not have a valid action or modifier", kind: .lint, nonTriggeringExamples: [ - "// swiftlint:disable unused_import", - "// swiftlint:enable unused_import", - "// swiftlint:disable:next unused_import", - "// swiftlint:disable:previous unused_import", - "// swiftlint:disable:this unused_import" + Example("// swiftlint:disable unused_import"), + Example("// swiftlint:enable unused_import"), + Example("// swiftlint:disable:next unused_import"), + Example("// swiftlint:disable:previous unused_import"), + Example("// swiftlint:disable:this unused_import") ], triggeringExamples: [ - "// swiftlint:", - "// swiftlint: ", - "// swiftlint::", - "// swiftlint:: ", - "// swiftlint:disable", - "// swiftlint:dissable unused_import", - "// swiftlint:enaaaable unused_import", - "// swiftlint:disable:nxt unused_import", - "// swiftlint:enable:prevus unused_import", - "// swiftlint:enable:ths unused_import", - "// swiftlint:enable", - "// swiftlint:enable:", - "// swiftlint:enable: ", - "// swiftlint:disable: unused_import" + Example("// swiftlint:"), + Example("// swiftlint: "), + Example("// swiftlint::"), + Example("// swiftlint:: "), + Example("// swiftlint:disable"), + Example("// swiftlint:dissable unused_import"), + Example("// swiftlint:enaaaable unused_import"), + Example("// swiftlint:disable:nxt unused_import"), + Example("// swiftlint:enable:prevus unused_import"), + Example("// swiftlint:enable:ths unused_import"), + Example("// swiftlint:enable"), + Example("// swiftlint:enable:"), + Example("// swiftlint:enable: "), + Example("// swiftlint:disable: unused_import") ].skipWrappingInCommentTests() ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/PrivateActionRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/PrivateActionRule.swift index 2341330f7..b02a542fd 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/PrivateActionRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/PrivateActionRule.swift @@ -9,25 +9,25 @@ struct PrivateActionRule: SwiftSyntaxRule, OptInRule, ConfigurationProviderRule description: "IBActions should be private", kind: .lint, nonTriggeringExamples: [ - "class Foo {\n\t@IBAction private func barButtonTapped(_ sender: UIButton) {}\n}\n", - "struct Foo {\n\t@IBAction private func barButtonTapped(_ sender: UIButton) {}\n}\n", - "class Foo {\n\t@IBAction fileprivate func barButtonTapped(_ sender: UIButton) {}\n}\n", - "struct Foo {\n\t@IBAction fileprivate func barButtonTapped(_ sender: UIButton) {}\n}\n", - "private extension Foo {\n\t@IBAction func barButtonTapped(_ sender: UIButton) {}\n}\n", - "fileprivate extension Foo {\n\t@IBAction func barButtonTapped(_ sender: UIButton) {}\n}\n" + Example("class Foo {\n\t@IBAction private func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("struct Foo {\n\t@IBAction private func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("class Foo {\n\t@IBAction fileprivate func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("struct Foo {\n\t@IBAction fileprivate func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("private extension Foo {\n\t@IBAction func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("fileprivate extension Foo {\n\t@IBAction func barButtonTapped(_ sender: UIButton) {}\n}\n") ], triggeringExamples: [ - "class Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "struct Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "class Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "struct Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "class Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "struct Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "extension Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "extension Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "public extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n", - "internal extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n" + Example("class Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("struct Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("class Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("struct Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("class Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("struct Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("extension Foo {\n\t@IBAction public ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("extension Foo {\n\t@IBAction internal ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("public extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n"), + Example("internal extension Foo {\n\t@IBAction ↓func barButtonTapped(_ sender: UIButton) {}\n}\n") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/TodoRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/TodoRule.swift index 968f9a015..63bab297d 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/TodoRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/TodoRule.swift @@ -10,18 +10,18 @@ struct TodoRule: SwiftSyntaxRule, ConfigurationProviderRule { description: "TODOs and FIXMEs should be resolved.", kind: .lint, nonTriggeringExamples: [ - "// notaTODO:\n", - "// notaFIXME:\n" + Example("// notaTODO:\n"), + Example("// notaFIXME:\n") ], triggeringExamples: [ - "// ↓TODO:\n", - "// ↓FIXME:\n", - "// ↓TODO(note)\n", - "// ↓FIXME(note)\n", - "/* ↓FIXME: */\n", - "/* ↓TODO: */\n", - "/** ↓FIXME: */\n", - "/** ↓TODO: */\n" + Example("// ↓TODO:\n"), + Example("// ↓FIXME:\n"), + Example("// ↓TODO(note)\n"), + Example("// ↓FIXME(note)\n"), + Example("/* ↓FIXME: */\n"), + Example("/* ↓TODO: */\n"), + Example("/** ↓FIXME: */\n"), + Example("/** ↓TODO: */\n") ].skipWrappingInCommentTests() ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Metrics/LargeTupleRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Metrics/LargeTupleRuleExamples.swift index ed9e84c2f..b5ca54747 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Metrics/LargeTupleRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Metrics/LargeTupleRuleExamples.swift @@ -1,56 +1,56 @@ struct LargeTupleRuleExamples { static let nonTriggeringExamples: [Example] = [ - "let foo: (Int, Int)\n", - "let foo: (start: Int, end: Int)\n", - "let foo: (Int, (Int, String))\n", - "func foo() -> (Int, Int)\n", - "func foo() -> (Int, Int) {}\n", - "func foo(bar: String) -> (Int, Int)\n", - "func foo(bar: String) -> (Int, Int) {}\n", - "func foo() throws -> (Int, Int)\n", - "func foo() throws -> (Int, Int) {}\n", - "let foo: (Int, Int, Int) -> Void\n", - "let foo: (Int, Int, Int) throws -> Void\n", - "func foo(bar: (Int, String, Float) -> Void)\n", - "func foo(bar: (Int, String, Float) throws -> Void)\n", - "var completionHandler: ((_ data: Data?, _ resp: URLResponse?, _ e: NSError?) -> Void)!\n", - "func getDictionaryAndInt() -> (Dictionary, Int)?\n", - "func getGenericTypeAndInt() -> (Type, Int)?\n", - "func foo() async -> (Int, Int)\n", - "func foo() async -> (Int, Int) {}\n", - "func foo(bar: String) async -> (Int, Int)\n", - "func foo(bar: String) async -> (Int, Int) {}\n", - "func foo() async throws -> (Int, Int)\n", - "func foo() async throws -> (Int, Int) {}\n", - "let foo: (Int, Int, Int) async -> Void\n", - "let foo: (Int, Int, Int) async throws -> Void\n", - "func foo(bar: (Int, String, Float) async -> Void)\n", - "func foo(bar: (Int, String, Float) async throws -> Void)\n", - "func getDictionaryAndInt() async -> (Dictionary, Int)?\n", - "func getGenericTypeAndInt() async -> (Type, Int)?\n" + Example("let foo: (Int, Int)\n"), + Example("let foo: (start: Int, end: Int)\n"), + Example("let foo: (Int, (Int, String))\n"), + Example("func foo() -> (Int, Int)\n"), + Example("func foo() -> (Int, Int) {}\n"), + Example("func foo(bar: String) -> (Int, Int)\n"), + Example("func foo(bar: String) -> (Int, Int) {}\n"), + Example("func foo() throws -> (Int, Int)\n"), + Example("func foo() throws -> (Int, Int) {}\n"), + Example("let foo: (Int, Int, Int) -> Void\n"), + Example("let foo: (Int, Int, Int) throws -> Void\n"), + Example("func foo(bar: (Int, String, Float) -> Void)\n"), + Example("func foo(bar: (Int, String, Float) throws -> Void)\n"), + Example("var completionHandler: ((_ data: Data?, _ resp: URLResponse?, _ e: NSError?) -> Void)!\n"), + Example("func getDictionaryAndInt() -> (Dictionary, Int)?\n"), + Example("func getGenericTypeAndInt() -> (Type, Int)?\n"), + Example("func foo() async -> (Int, Int)\n"), + Example("func foo() async -> (Int, Int) {}\n"), + Example("func foo(bar: String) async -> (Int, Int)\n"), + Example("func foo(bar: String) async -> (Int, Int) {}\n"), + Example("func foo() async throws -> (Int, Int)\n"), + Example("func foo() async throws -> (Int, Int) {}\n"), + Example("let foo: (Int, Int, Int) async -> Void\n"), + Example("let foo: (Int, Int, Int) async throws -> Void\n"), + Example("func foo(bar: (Int, String, Float) async -> Void)\n"), + Example("func foo(bar: (Int, String, Float) async throws -> Void)\n"), + Example("func getDictionaryAndInt() async -> (Dictionary, Int)?\n"), + Example("func getGenericTypeAndInt() async -> (Type, Int)?\n") ] static let triggeringExamples: [Example] = [ - "let foo: ↓(Int, Int, Int)\n", - "let foo: ↓(start: Int, end: Int, value: String)\n", - "let foo: (Int, ↓(Int, Int, Int))\n", - "func foo(bar: ↓(Int, Int, Int))\n", - "func foo() -> ↓(Int, Int, Int)\n", - "func foo() -> ↓(Int, Int, Int) {}\n", - "func foo(bar: String) -> ↓(Int, Int, Int)\n", - "func foo(bar: String) -> ↓(Int, Int, Int) {}\n", - "func foo() throws -> ↓(Int, Int, Int)\n", - "func foo() throws -> ↓(Int, Int, Int) {}\n", - "func foo() throws -> ↓(Int, ↓(String, String, String), Int) {}\n", - "func getDictionaryAndInt() -> (Dictionary, Int)?\n", - "func foo(bar: ↓(Int, Int, Int)) async\n", - "func foo() async -> ↓(Int, Int, Int)\n", - "func foo() async -> ↓(Int, Int, Int) {}\n", - "func foo(bar: String) async -> ↓(Int, Int, Int)\n", - "func foo(bar: String) async -> ↓(Int, Int, Int) {}\n", - "func foo() async throws -> ↓(Int, Int, Int)\n", - "func foo() async throws -> ↓(Int, Int, Int) {}\n", - "func foo() async throws -> ↓(Int, ↓(String, String, String), Int) {}\n", - "func getDictionaryAndInt() async -> (Dictionary, Int)?\n" + Example("let foo: ↓(Int, Int, Int)\n"), + Example("let foo: ↓(start: Int, end: Int, value: String)\n"), + Example("let foo: (Int, ↓(Int, Int, Int))\n"), + Example("func foo(bar: ↓(Int, Int, Int))\n"), + Example("func foo() -> ↓(Int, Int, Int)\n"), + Example("func foo() -> ↓(Int, Int, Int) {}\n"), + Example("func foo(bar: String) -> ↓(Int, Int, Int)\n"), + Example("func foo(bar: String) -> ↓(Int, Int, Int) {}\n"), + Example("func foo() throws -> ↓(Int, Int, Int)\n"), + Example("func foo() throws -> ↓(Int, Int, Int) {}\n"), + Example("func foo() throws -> ↓(Int, ↓(String, String, String), Int) {}\n"), + Example("func getDictionaryAndInt() -> (Dictionary, Int)?\n"), + Example("func foo(bar: ↓(Int, Int, Int)) async\n"), + Example("func foo() async -> ↓(Int, Int, Int)\n"), + Example("func foo() async -> ↓(Int, Int, Int) {}\n"), + Example("func foo(bar: String) async -> ↓(Int, Int, Int)\n"), + Example("func foo(bar: String) async -> ↓(Int, Int, Int) {}\n"), + Example("func foo() async throws -> ↓(Int, Int, Int)\n"), + Example("func foo() async throws -> ↓(Int, Int, Int) {}\n"), + Example("func foo() async throws -> ↓(Int, ↓(String, String, String), Int) {}\n"), + Example("func getDictionaryAndInt() async -> (Dictionary, Int)?\n") ] } diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFilterCountRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFilterCountRule.swift index 7654dc125..af6620383 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFilterCountRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFilterCountRule.swift @@ -10,20 +10,20 @@ struct ContainsOverFilterCountRule: SwiftSyntaxRule, OptInRule, ConfigurationPro kind: .performance, nonTriggeringExamples: [">", "==", "!="].flatMap { operation in return [ - "let result = myList.filter(where: { $0 % 2 == 0 }).count \(operation) 1\n", - "let result = myList.filter { $0 % 2 == 0 }.count \(operation) 1\n", - "let result = myList.filter(where: { $0 % 2 == 0 }).count \(operation) 01\n" + Example("let result = myList.filter(where: { $0 % 2 == 0 }).count \(operation) 1\n"), + Example("let result = myList.filter { $0 % 2 == 0 }.count \(operation) 1\n"), + Example("let result = myList.filter(where: { $0 % 2 == 0 }).count \(operation) 01\n") ] } + [ - "let result = myList.contains(where: { $0 % 2 == 0 })\n", - "let result = !myList.contains(where: { $0 % 2 == 0 })\n", - "let result = myList.contains(10)\n" + Example("let result = myList.contains(where: { $0 % 2 == 0 })\n"), + Example("let result = !myList.contains(where: { $0 % 2 == 0 })\n"), + Example("let result = myList.contains(10)\n") ], triggeringExamples: [">", "==", "!="].flatMap { operation in return [ - "let result = ↓myList.filter(where: { $0 % 2 == 0 }).count \(operation) 0\n", - "let result = ↓myList.filter { $0 % 2 == 0 }.count \(operation) 0\n", - "let result = ↓myList.filter(where: someFunction).count \(operation) 0\n" + Example("let result = ↓myList.filter(where: { $0 % 2 == 0 }).count \(operation) 0\n"), + Example("let result = ↓myList.filter { $0 % 2 == 0 }.count \(operation) 0\n"), + Example("let result = ↓myList.filter(where: someFunction).count \(operation) 0\n") ] } ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFilterIsEmptyRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFilterIsEmptyRule.swift index 479a9c6a3..3baf4e504 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFilterIsEmptyRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFilterIsEmptyRule.swift @@ -10,19 +10,19 @@ struct ContainsOverFilterIsEmptyRule: SwiftSyntaxRule, OptInRule, ConfigurationP kind: .performance, nonTriggeringExamples: [">", "==", "!="].flatMap { operation in return [ - "let result = myList.filter(where: { $0 % 2 == 0 }).count \(operation) 1\n", - "let result = myList.filter { $0 % 2 == 0 }.count \(operation) 1\n" + Example("let result = myList.filter(where: { $0 % 2 == 0 }).count \(operation) 1\n"), + Example("let result = myList.filter { $0 % 2 == 0 }.count \(operation) 1\n") ] } + [ - "let result = myList.contains(where: { $0 % 2 == 0 })\n", - "let result = !myList.contains(where: { $0 % 2 == 0 })\n", - "let result = myList.contains(10)\n" + Example("let result = myList.contains(where: { $0 % 2 == 0 })\n"), + Example("let result = !myList.contains(where: { $0 % 2 == 0 })\n"), + Example("let result = myList.contains(10)\n") ], triggeringExamples: [ - "let result = ↓myList.filter(where: { $0 % 2 == 0 }).isEmpty\n", - "let result = !↓myList.filter(where: { $0 % 2 == 0 }).isEmpty\n", - "let result = ↓myList.filter { $0 % 2 == 0 }.isEmpty\n", - "let result = ↓myList.filter(where: someFunction).isEmpty\n" + Example("let result = ↓myList.filter(where: { $0 % 2 == 0 }).isEmpty\n"), + Example("let result = !↓myList.filter(where: { $0 % 2 == 0 }).isEmpty\n"), + Example("let result = ↓myList.filter { $0 % 2 == 0 }.isEmpty\n"), + Example("let result = ↓myList.filter(where: someFunction).isEmpty\n") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFirstNotNilRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFirstNotNilRule.swift index 50a2fdd6a..160ef87ef 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFirstNotNilRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverFirstNotNilRule.swift @@ -10,19 +10,19 @@ struct ContainsOverFirstNotNilRule: SwiftSyntaxRule, OptInRule, ConfigurationPro kind: .performance, nonTriggeringExamples: ["first", "firstIndex"].flatMap { method in return [ - "let \(method) = myList.\(method)(where: { $0 % 2 == 0 })\n", - "let \(method) = myList.\(method) { $0 % 2 == 0 }\n" + Example("let \(method) = myList.\(method)(where: { $0 % 2 == 0 })\n"), + Example("let \(method) = myList.\(method) { $0 % 2 == 0 }\n") ] }, triggeringExamples: ["first", "firstIndex"].flatMap { method in return ["!=", "=="].flatMap { comparison in return [ - "↓myList.\(method) { $0 % 2 == 0 } \(comparison) nil\n", - "↓myList.\(method)(where: { $0 % 2 == 0 }) \(comparison) nil\n", - "↓myList.map { $0 + 1 }.\(method)(where: { $0 % 2 == 0 }) \(comparison) nil\n", - "↓myList.\(method)(where: someFunction) \(comparison) nil\n", - "↓myList.map { $0 + 1 }.\(method) { $0 % 2 == 0 } \(comparison) nil\n", - "(↓myList.\(method) { $0 % 2 == 0 }) \(comparison) nil\n" + Example("↓myList.\(method) { $0 % 2 == 0 } \(comparison) nil\n"), + Example("↓myList.\(method)(where: { $0 % 2 == 0 }) \(comparison) nil\n"), + Example("↓myList.map { $0 + 1 }.\(method)(where: { $0 % 2 == 0 }) \(comparison) nil\n"), + Example("↓myList.\(method)(where: someFunction) \(comparison) nil\n"), + Example("↓myList.map { $0 + 1 }.\(method) { $0 % 2 == 0 } \(comparison) nil\n"), + Example("(↓myList.\(method) { $0 % 2 == 0 }) \(comparison) nil\n") ] } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverRangeNilComparisonRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverRangeNilComparisonRule.swift index e023f8dec..53d9b83b4 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverRangeNilComparisonRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/ContainsOverRangeNilComparisonRule.swift @@ -9,15 +9,16 @@ struct ContainsOverRangeNilComparisonRule: SwiftSyntaxRule, OptInRule, Configura description: "Prefer `contains` over `range(of:) != nil` and `range(of:) == nil`", kind: .performance, nonTriggeringExamples: [ - "let range = myString.range(of: \"Test\")", - "myString.contains(\"Test\")", - "!myString.contains(\"Test\")", - "resourceString.range(of: rule.regex, options: .regularExpression) != nil" + Example("let range = myString.range(of: \"Test\")"), + Example("myString.contains(\"Test\")"), + Example("!myString.contains(\"Test\")"), + Example("resourceString.range(of: rule.regex, options: .regularExpression) != nil") ], - triggeringExamples: [ - "↓myString.range(of: \"Test\") != nil", - "↓myString.range(of: \"Test\") == nil" - ] + triggeringExamples: ["!=", "=="].flatMap { comparison in + return [ + Example("↓myString.range(of: \"Test\") \(comparison) nil") + ] + } ) func preprocess(file: SwiftLintFile) -> SourceFileSyntax? { diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCollectionLiteralRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCollectionLiteralRule.swift index 1c92f9766..b64388b82 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCollectionLiteralRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCollectionLiteralRule.swift @@ -9,20 +9,20 @@ struct EmptyCollectionLiteralRule: SwiftSyntaxRule, ConfigurationProviderRule, O description: "Prefer checking `isEmpty` over comparing collection to an empty array or dictionary literal", kind: .performance, nonTriggeringExamples: [ - "myArray = []", - "myArray.isEmpty", - "!myArray.isEmpty", - "myDict = [:]" + Example("myArray = []"), + Example("myArray.isEmpty"), + Example("!myArray.isEmpty"), + Example("myDict = [:]") ], triggeringExamples: [ - "myArray↓ == []", - "myArray↓ != []", - "myArray↓ == [ ]", - "myDict↓ == [:]", - "myDict↓ != [:]", - "myDict↓ == [: ]", - "myDict↓ == [ :]", - "myDict↓ == [ : ]" + Example("myArray↓ == []"), + Example("myArray↓ != []"), + Example("myArray↓ == [ ]"), + Example("myDict↓ == [:]"), + Example("myDict↓ != [:]"), + Example("myDict↓ == [: ]"), + Example("myDict↓ == [ :]"), + Example("myDict↓ == [ : ]") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift index e6902a29b..e12b582e4 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift @@ -9,27 +9,27 @@ struct EmptyCountRule: ConfigurationProviderRule, OptInRule, SwiftSyntaxRule { description: "Prefer checking `isEmpty` over comparing `count` to zero", kind: .performance, nonTriggeringExamples: [ - "var count = 0\n", - "[Int]().isEmpty\n", - "[Int]().count > 1\n", - "[Int]().count == 1\n", - "[Int]().count == 0xff\n", - "[Int]().count == 0b01\n", - "[Int]().count == 0o07\n", - "discount == 0\n", - "order.discount == 0\n" + Example("var count = 0\n"), + Example("[Int]().isEmpty\n"), + Example("[Int]().count > 1\n"), + Example("[Int]().count == 1\n"), + Example("[Int]().count == 0xff\n"), + Example("[Int]().count == 0b01\n"), + Example("[Int]().count == 0o07\n"), + Example("discount == 0\n"), + Example("order.discount == 0\n") ], triggeringExamples: [ - "[Int]().↓count == 0\n", - "0 == [Int]().↓count\n", - "[Int]().↓count==0\n", - "[Int]().↓count > 0\n", - "[Int]().↓count != 0\n", - "[Int]().↓count == 0x0\n", - "[Int]().↓count == 0x00_00\n", - "[Int]().↓count == 0b00\n", - "[Int]().↓count == 0o00\n", - "↓count == 0\n" + Example("[Int]().↓count == 0\n"), + Example("0 == [Int]().↓count\n"), + Example("[Int]().↓count==0\n"), + Example("[Int]().↓count > 0\n"), + Example("[Int]().↓count != 0\n"), + Example("[Int]().↓count == 0x0\n"), + Example("[Int]().↓count == 0x00_00\n"), + Example("[Int]().↓count == 0b00\n"), + Example("[Int]().↓count == 0o00\n"), + Example("↓count == 0\n") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/FlatMapOverMapReduceRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/FlatMapOverMapReduceRule.swift index 31c7dc946..db397d8e5 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/FlatMapOverMapReduceRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/FlatMapOverMapReduceRule.swift @@ -9,11 +9,11 @@ struct FlatMapOverMapReduceRule: SwiftSyntaxRule, OptInRule, ConfigurationProvid description: "Prefer `flatMap` over `map` followed by `reduce([], +)`", kind: .performance, nonTriggeringExamples: [ - "let foo = bar.map { $0.count }.reduce(0, +)", - "let foo = bar.flatMap { $0.array }" + Example("let foo = bar.map { $0.count }.reduce(0, +)"), + Example("let foo = bar.flatMap { $0.array }") ], triggeringExamples: [ - "let foo = ↓bar.map { $0.array }.reduce([], +)" + Example("let foo = ↓bar.map { $0.array }.reduce([], +)") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/ReduceBooleanRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/ReduceBooleanRule.swift index bbcfeaef7..44ff9478a 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/ReduceBooleanRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/ReduceBooleanRule.swift @@ -9,20 +9,20 @@ struct ReduceBooleanRule: SwiftSyntaxRule, ConfigurationProviderRule { description: "Prefer using `.allSatisfy()` or `.contains()` over `reduce(true)` or `reduce(false)`.", kind: .performance, nonTriggeringExamples: [ - "nums.reduce(0) { $0.0 + $0.1 }", - "nums.reduce(0.0) { $0.0 + $0.1 }", - "nums.reduce(initial: true) { $0.0 && $0.1 == 3 }" + Example("nums.reduce(0) { $0.0 + $0.1 }"), + Example("nums.reduce(0.0) { $0.0 + $0.1 }"), + Example("nums.reduce(initial: true) { $0.0 && $0.1 == 3 }") ], triggeringExamples: [ - "let allNines = nums.↓reduce(true) { $0.0 && $0.1 == 9 }", - "let anyNines = nums.↓reduce(false) { $0.0 || $0.1 == 9 }", - "let allValid = validators.↓reduce(true) { $0 && $1(input) }", - "let anyValid = validators.↓reduce(false) { $0 || $1(input) }", - "let allNines = nums.↓reduce(true, { $0.0 && $0.1 == 9 })", - "let anyNines = nums.↓reduce(false, { $0.0 || $0.1 == 9 })", - "let allValid = validators.↓reduce(true, { $0 && $1(input) })", - "let anyValid = validators.↓reduce(false, { $0 || $1(input) })", - "nums.reduce(into: true) { (r: inout Bool, s) in r = r && (s == 3) }" + Example("let allNines = nums.↓reduce(true) { $0.0 && $0.1 == 9 }"), + Example("let anyNines = nums.↓reduce(false) { $0.0 || $0.1 == 9 }"), + Example("let allValid = validators.↓reduce(true) { $0 && $1(input) }"), + Example("let anyValid = validators.↓reduce(false) { $0 || $1(input) }"), + Example("let allNines = nums.↓reduce(true, { $0.0 && $0.1 == 9 })"), + Example("let anyNines = nums.↓reduce(false, { $0.0 || $0.1 == 9 })"), + Example("let allValid = validators.↓reduce(true, { $0 && $1(input) })"), + Example("let anyValid = validators.↓reduce(false, { $0 || $1(input) })"), + Example("nums.reduce(into: true) { (r: inout Bool, s) in r = r && (s == 3) }") ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/OperatorFunctionWhitespaceRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/OperatorFunctionWhitespaceRule.swift index 03e64dfae..b56ef1232 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/OperatorFunctionWhitespaceRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/OperatorFunctionWhitespaceRule.swift @@ -9,17 +9,17 @@ struct OperatorFunctionWhitespaceRule: ConfigurationProviderRule, SwiftSyntaxRul description: "Operators should be surrounded by a single whitespace when defining them", kind: .style, nonTriggeringExamples: [ - "func <| (lhs: Int, rhs: Int) -> Int {}\n", - "func <|< (lhs: A, rhs: A) -> A {}\n", - "func abc(lhs: Int, rhs: Int) -> Int {}\n" + Example("func <| (lhs: Int, rhs: Int) -> Int {}\n"), + Example("func <|< (lhs: A, rhs: A) -> A {}\n"), + Example("func abc(lhs: Int, rhs: Int) -> Int {}\n") ], triggeringExamples: [ - "↓func <|(lhs: Int, rhs: Int) -> Int {}\n", // no spaces after - "↓func <|<(lhs: A, rhs: A) -> A {}\n", // no spaces after - "↓func <| (lhs: Int, rhs: Int) -> Int {}\n", // 2 spaces after - "↓func <|< (lhs: A, rhs: A) -> A {}\n", // 2 spaces after - "↓func <| (lhs: Int, rhs: Int) -> Int {}\n", // 2 spaces before - "↓func <|< (lhs: A, rhs: A) -> A {}\n" // 2 spaces before + Example("↓func <|(lhs: Int, rhs: Int) -> Int {}\n"), // no spaces after + Example("↓func <|<(lhs: A, rhs: A) -> A {}\n"), // no spaces after + Example("↓func <| (lhs: Int, rhs: Int) -> Int {}\n"), // 2 spaces after + Example("↓func <|< (lhs: A, rhs: A) -> A {}\n"), // 2 spaces after + Example("↓func <| (lhs: Int, rhs: Int) -> Int {}\n"), // 2 spaces before + Example("↓func <|< (lhs: A, rhs: A) -> A {}\n") // 2 spaces before ] ) diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/TrailingClosureRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/TrailingClosureRule.swift index 0859c2ab5..68fcb1518 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/TrailingClosureRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/TrailingClosureRule.swift @@ -10,21 +10,21 @@ struct TrailingClosureRule: OptInRule, ConfigurationProviderRule { description: "Trailing closure syntax should be used whenever possible", kind: .style, nonTriggeringExamples: [ - "foo.map { $0 + 1 }\n", - "foo.bar()\n", - "foo.reduce(0) { $0 + 1 }\n", - "if let foo = bar.map({ $0 + 1 }) { }\n", - "foo.something(param1: { $0 }, param2: { $0 + 1 })\n", - "offsets.sorted { $0.offset < $1.offset }\n", - "foo.something({ return 1 }())", - "foo.something({ return $0 }(1))", - "foo.something(0, { return 1 }())" + Example("foo.map { $0 + 1 }\n"), + Example("foo.bar()\n"), + Example("foo.reduce(0) { $0 + 1 }\n"), + Example("if let foo = bar.map({ $0 + 1 }) { }\n"), + Example("foo.something(param1: { $0 }, param2: { $0 + 1 })\n"), + Example("offsets.sorted { $0.offset < $1.offset }\n"), + Example("foo.something({ return 1 }())"), + Example("foo.something({ return $0 }(1))"), + Example("foo.something(0, { return 1 }())") ], triggeringExamples: [ - "↓foo.map({ $0 + 1 })\n", - "↓foo.reduce(0, combine: { $0 + 1 })\n", - "↓offsets.sorted(by: { $0.offset < $1.offset })\n", - "↓foo.something(0, { $0 + 1 })\n" + Example("↓foo.map({ $0 + 1 })\n"), + Example("↓foo.reduce(0, combine: { $0 + 1 })\n"), + Example("↓offsets.sorted(by: { $0.offset < $1.offset })\n"), + Example("↓foo.something(0, { $0 + 1 })\n") ] ) diff --git a/Source/SwiftLintCore/Models/Example.swift b/Source/SwiftLintCore/Models/Example.swift index b5f3cdd1d..4d19d6a4e 100644 --- a/Source/SwiftLintCore/Models/Example.swift +++ b/Source/SwiftLintCore/Models/Example.swift @@ -147,13 +147,6 @@ extension Example: Comparable { } } -/// Allows to initialize an ``Example`` as `let example: Example = "..."`. -extension Example: ExpressibleByStringInterpolation { - public init(stringLiteral value: String) { - self.init(value) - } -} - public extension Array where Element == Example { /// Make these examples skip wrapping in comment tests. func skipWrappingInCommentTests() -> Self {