From e75d39346974fdf3b16a0faebc83a1c5da2affb0 Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Sat, 8 Feb 2020 13:55:31 -0800 Subject: [PATCH] Do not trigger optional_enum_case_matching on `_?` Fixes #3057 --- CHANGELOG.md | 6 +++++- .../Rules/Style/OptionalEnumCaseMatchingRule.swift | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1ddc0ee92..db00f061de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,7 +62,11 @@ * Implicit_return description now reports current config correctly. [John Buckley](https://github.com/nhojb) - + +* Do not trigger `optional_enum_case_matching` rule on `_?` as the `?` might + be required in some situations. + [Marcelo Fabri](https://github.com/marcelofabri) + [#3057](https://github.com/realm/SwiftLint/issues/3057) ## 0.38.2: Machine Repair Manual diff --git a/Source/SwiftLintFramework/Rules/Style/OptionalEnumCaseMatchingRule.swift b/Source/SwiftLintFramework/Rules/Style/OptionalEnumCaseMatchingRule.swift index 845facdbf3..2354e12c09 100644 --- a/Source/SwiftLintFramework/Rules/Style/OptionalEnumCaseMatchingRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/OptionalEnumCaseMatchingRule.swift @@ -28,6 +28,16 @@ public struct OptionalEnumCaseMatchingRule: SubstitutionCorrectableASTRule, Conf case (_, .baz): break default: break } + """), + Example(""" + switch (x, y) { + case (.c, _?): + break + case (.c, nil): + break + case (_, _): + break + } """) ], triggeringExamples: [ @@ -180,6 +190,9 @@ public struct OptionalEnumCaseMatchingRule: SubstitutionCorrectableASTRule, Conf } return tokensToCheck.compactMap { tokenToCheck in + guard !file.isTokenUnderscoreKeyword(tokenToCheck) else { + return nil + } let questionMarkByteRange = ByteRange(location: tokenToCheck.range.upperBound, length: 1) guard contents.substringWithByteRange(questionMarkByteRange) == "?" else { return nil