From 743853c28b628bebe5e0e723e73cda55729221ad Mon Sep 17 00:00:00 2001 From: JP Simard Date: Tue, 11 Feb 2020 13:03:07 -0800 Subject: [PATCH] Revert "Add `prohibited_nan_comparison` opt-in rule (#3089)" This reverts commit fdd16a68531d98872ced39904950d337daef7086. --- CHANGELOG.md | 5 - .../Models/MasterRuleList.swift | 1 - .../Lint/PhohibitedNaNComparisonRule.swift | 99 ------------------- SwiftLint.xcodeproj/project.pbxproj | 4 - Tests/LinuxMain.swift | 7 -- .../AutomaticRuleTests.generated.swift | 6 -- 6 files changed, 122 deletions(-) delete mode 100644 Source/SwiftLintFramework/Rules/Lint/PhohibitedNaNComparisonRule.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 322b0d9095..3be8a63923 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,11 +75,6 @@ [Zsolt Kovács](https://github.com/lordzsolt) [#827](https://github.com/realm/SwiftLint/issues/827) -* Add `prohibited_nan_comparison` opt-in rule to validate using `isNaN` - instead of comparing values to the `.nan` constant. - [Marcelo Fabri](https://github.com/marcelofabri) - [#2086](https://github.com/realm/SwiftLint/issues/2086) - #### Bug Fixes * Fix `discarded_notification_center_observer` false positives when diff --git a/Source/SwiftLintFramework/Models/MasterRuleList.swift b/Source/SwiftLintFramework/Models/MasterRuleList.swift index 349a217925..4bbca82220 100644 --- a/Source/SwiftLintFramework/Models/MasterRuleList.swift +++ b/Source/SwiftLintFramework/Models/MasterRuleList.swift @@ -121,7 +121,6 @@ public let masterRuleList = RuleList(rules: [ OverriddenSuperCallRule.self, OverrideInExtensionRule.self, PatternMatchingKeywordsRule.self, - PhohibitedNaNComparisonRule.self, PreferSelfTypeOverTypeOfSelfRule.self, PrefixedTopLevelConstantRule.self, PrivateActionRule.self, diff --git a/Source/SwiftLintFramework/Rules/Lint/PhohibitedNaNComparisonRule.swift b/Source/SwiftLintFramework/Rules/Lint/PhohibitedNaNComparisonRule.swift deleted file mode 100644 index c267ea9829..0000000000 --- a/Source/SwiftLintFramework/Rules/Lint/PhohibitedNaNComparisonRule.swift +++ /dev/null @@ -1,99 +0,0 @@ -import Foundation -import SourceKittenFramework -#if canImport(SwiftSyntax) -import SwiftSyntax -#endif - -public struct PhohibitedNaNComparisonRule: ConfigurationProviderRule, SyntaxRule, OptInRule, AutomaticTestableRule { - public var configuration = SeverityConfiguration(.warning) - - public init() {} - - public static let description = RuleDescription( - identifier: "prohibited_nan_comparison", - name: "Prohibited NaN Comparison", - description: "Use `isNaN` instead of comparing values to the `.nan` constant.", - kind: .lint, - minSwiftVersion: .fiveDotOne, - nonTriggeringExamples: [ - Example("if number.isNaN {}"), - Example("if foo.nan == 3 {}") // allows using in non-types - ], - triggeringExamples: [ - Example("if number == .nan {}"), - Example("if number + 1 == .nan {}"), - Example("if number == Float.nan {}"), - Example("if .nan == number {}"), - Example("if Double.nan == number {}") - ] - ) - - public func validate(file: SwiftLintFile) -> [StyleViolation] { - #if canImport(SwiftSyntax) - return validate(file: file, visitor: BinaryOperatorVisitor()) - #else - return [] - #endif - } -} - -#if canImport(SwiftSyntax) -private class BinaryOperatorVisitor: SyntaxRuleVisitor { - private var positions = [AbsolutePosition]() - private let operators: Set = ["==", "!="] - - func visit(_ node: BinaryOperatorExprSyntax) -> SyntaxVisitorContinueKind { - if operators.contains(node.operatorToken.withoutTrivia().text), let children = node.parent?.children { - let array = Array(children) - let before = array[array.index(before: node.indexInParent)] - let after = array[array.index(after: node.indexInParent)] - - if before.isNaN || after.isNaN { - positions.append(node.operatorToken.positionAfterSkippingLeadingTrivia) - } - } - - return .visitChildren - } - - func violations(for rule: PhohibitedNaNComparisonRule, in file: SwiftLintFile) -> [StyleViolation] { - return positions.map { position in - StyleViolation(ruleDescription: type(of: rule).description, - severity: rule.configuration.severity, - location: Location(file: file, byteOffset: ByteCount(position.utf8Offset))) - } - } -} - -private extension Syntax { - var isNaN: Bool { - guard let memberExpr = self as? MemberAccessExprSyntax else { - return false - } - - let isNaNProperty = memberExpr.name.withoutTrivia().text == "nan" - return isNaNProperty && (memberExpr.base == nil || memberExpr.base?.referringToType == true) - } -} - -private extension ExprSyntax { - var referringToType: Bool { - guard let expr = self as? IdentifierExprSyntax else { - return false - } - - return expr.identifier.classifications.map { $0.kind } == [.identifier] && - expr.identifier.withoutTrivia().text.isTypeLike - } -} - -private extension String { - var isTypeLike: Bool { - guard let firstLetter = unicodeScalars.first else { - return false - } - - return CharacterSet.uppercaseLetters.contains(firstLetter) - } -} -#endif diff --git a/SwiftLint.xcodeproj/project.pbxproj b/SwiftLint.xcodeproj/project.pbxproj index 2a73d26106..8575d7d009 100644 --- a/SwiftLint.xcodeproj/project.pbxproj +++ b/SwiftLint.xcodeproj/project.pbxproj @@ -264,7 +264,6 @@ D40AD08A1E032F9700F48C30 /* UnusedClosureParameterRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40AD0891E032F9700F48C30 /* UnusedClosureParameterRule.swift */; }; D40E041C1F46E3B30043BC4E /* SuperfluousDisableCommandRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40E041B1F46E3B30043BC4E /* SuperfluousDisableCommandRule.swift */; }; D40F83881DE9179200524C62 /* TrailingCommaConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40F83871DE9179200524C62 /* TrailingCommaConfiguration.swift */; }; - D40F8B6723E6D60E00A5218E /* PhohibitedNaNComparisonRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40F8B6623E6D60E00A5218E /* PhohibitedNaNComparisonRule.swift */; }; D40FE89D1F867BFF006433E2 /* OverrideInExtensionRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40FE89C1F867BFF006433E2 /* OverrideInExtensionRule.swift */; }; D4130D971E16183F00242361 /* IdentifierNameRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4130D961E16183F00242361 /* IdentifierNameRuleExamples.swift */; }; D4130D991E16CC1300242361 /* TypeNameRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */; }; @@ -794,7 +793,6 @@ D40AD0891E032F9700F48C30 /* UnusedClosureParameterRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnusedClosureParameterRule.swift; sourceTree = ""; }; D40E041B1F46E3B30043BC4E /* SuperfluousDisableCommandRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SuperfluousDisableCommandRule.swift; sourceTree = ""; }; D40F83871DE9179200524C62 /* TrailingCommaConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrailingCommaConfiguration.swift; sourceTree = ""; }; - D40F8B6623E6D60E00A5218E /* PhohibitedNaNComparisonRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhohibitedNaNComparisonRule.swift; sourceTree = ""; }; D40FE89C1F867BFF006433E2 /* OverrideInExtensionRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverrideInExtensionRule.swift; sourceTree = ""; }; D4130D961E16183F00242361 /* IdentifierNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IdentifierNameRuleExamples.swift; sourceTree = ""; }; D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypeNameRuleExamples.swift; sourceTree = ""; }; @@ -1200,7 +1198,6 @@ 094385021D5D4F78009168CF /* PrivateOutletRule.swift */, B2902A0B1D66815600BFCCF7 /* PrivateUnitTestRule.swift */, D44037962132730000FDA77B /* ProhibitedInterfaceBuilderRule.swift */, - D40F8B6623E6D60E00A5218E /* PhohibitedNaNComparisonRule.swift */, 009E09271DFEE4C200B588A7 /* ProhibitedSuperRule.swift */, 623E36EF1F3DB1B1002E5B71 /* QuickDiscouragedCallRule.swift */, 623E36F11F3DB988002E5B71 /* QuickDiscouragedCallRuleExamples.swift */, @@ -2158,7 +2155,6 @@ E8EA41171C2D1DBE004F9930 /* CheckstyleReporter.swift in Sources */, 006ECFC41C44E99E00EF6364 /* LegacyConstantRule.swift in Sources */, 82FE254120F604CB00295958 /* VerticalWhitespaceClosingBracesRule.swift in Sources */, - D40F8B6723E6D60E00A5218E /* PhohibitedNaNComparisonRule.swift in Sources */, 429644B61FB0A9B400D75128 /* SortedFirstLastRule.swift in Sources */, C3EF547821B5A4000009262F /* LegacyHashingRule.swift in Sources */, 31F1B6CC1F60BF4500A57456 /* SwitchCaseAlignmentRule.swift in Sources */, diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 41d56cf9a3..7ab92a7619 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -1064,12 +1064,6 @@ extension PatternMatchingKeywordsRuleTests { ] } -extension PhohibitedNaNComparisonRuleTests { - static var allTests: [(String, (PhohibitedNaNComparisonRuleTests) -> () throws -> Void)] = [ - ("testWithDefaultConfiguration", testWithDefaultConfiguration) - ] -} - extension PreferSelfTypeOverTypeOfSelfRuleTests { static var allTests: [(String, (PreferSelfTypeOverTypeOfSelfRuleTests) -> () throws -> Void)] = [ ("testWithDefaultConfiguration", testWithDefaultConfiguration) @@ -1808,7 +1802,6 @@ XCTMain([ testCase(OverriddenSuperCallRuleTests.allTests), testCase(OverrideInExtensionRuleTests.allTests), testCase(PatternMatchingKeywordsRuleTests.allTests), - testCase(PhohibitedNaNComparisonRuleTests.allTests), testCase(PreferSelfTypeOverTypeOfSelfRuleTests.allTests), testCase(PrefixedTopLevelConstantRuleTests.allTests), testCase(PrivateActionRuleTests.allTests), diff --git a/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift b/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift index 304bf1e107..3e4cbade98 100644 --- a/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift +++ b/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift @@ -510,12 +510,6 @@ class PatternMatchingKeywordsRuleTests: XCTestCase { } } -class PhohibitedNaNComparisonRuleTests: XCTestCase { - func testWithDefaultConfiguration() { - verifyRule(PhohibitedNaNComparisonRule.description) - } -} - class PreferSelfTypeOverTypeOfSelfRuleTests: XCTestCase { func testWithDefaultConfiguration() { verifyRule(PreferSelfTypeOverTypeOfSelfRule.description)