From c58d64553243fb08c16c5516a8015b267d9d11ed Mon Sep 17 00:00:00 2001 From: John Szumski Date: Wed, 27 Sep 2023 16:47:21 -0400 Subject: [PATCH 1/2] Preserve `@_exported` import statements even if unused to avoid breaking downstream modules depending on the re-exported module. --- CHANGELOG.md | 5 ++++- .../SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift | 2 +- .../Rules/Lint/UnusedImportRuleExamples.swift | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0471dadffa..289a4f728b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,10 @@ #### Bug Fixes -* None. +* Fix false positive in `unused_import` rule that triggered on + `@_exported` imports which could break downstream modules even if removed. + [jszumski](https://github.com/jszumski) + [#5242](https://github.com/realm/SwiftLint/pull/5242) ## 0.53.0: Laundry List diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift index cf1c56cd5a..ffb283baf2 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift @@ -185,7 +185,7 @@ private extension SwiftLintFile { func rangedAndSortedUnusedImports(of unusedImports: [String]) -> [(String, NSRange)] { return unusedImports .compactMap { module in - match(pattern: "^(@\\w+ +)?import +\(module)\\b.*?\n").first.map { (module, $0.0) } + match(pattern: "^(@(?!_exported)\\w+ +)?import +\(module)\\b.*?\n").first.map { (module, $0.0) } } .sorted(by: { $0.1.location < $1.1.location }) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRuleExamples.swift index 8b67b9289e..4c1d5d1c11 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRuleExamples.swift @@ -16,6 +16,9 @@ struct UnusedImportRuleExamples { Example(""" import UnknownModule func foo(error: Swift.Error) {} + """), + Example(""" + @_exported import UnknownModule """) ] + nonTriggeringExamplesVersionAdditions @@ -122,7 +125,7 @@ struct UnusedImportRuleExamples { dispatchMain() """), Example(""" - ↓@_exported import Foundation + ↓@_implementationOnly import Foundation import Dispatch dispatchMain() """): From a229e18d07c80b761db44ee5f782a41b277c5641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Sat, 30 Sep 2023 14:27:16 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 289a4f728b..c6d44aaa82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ #### Bug Fixes * Fix false positive in `unused_import` rule that triggered on - `@_exported` imports which could break downstream modules even if removed. + `@_exported` imports which could break downstream modules if removed. [jszumski](https://github.com/jszumski) [#5242](https://github.com/realm/SwiftLint/pull/5242)