Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve @_exported import statements in unused_imports #5242

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -19,7 +19,10 @@

#### Bug Fixes

* None.
* Fix false positive in `unused_import` rule that triggered on
`@_exported` imports which could break downstream modules if removed.
[jszumski](https://github.com/jszumski)
[#5242](https://github.com/realm/SwiftLint/pull/5242)

## 0.53.0: Laundry List

Expand Down
Expand Up @@ -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 })
}
Expand Down
Expand Up @@ -16,6 +16,9 @@ struct UnusedImportRuleExamples {
Example("""
import UnknownModule
func foo(error: Swift.Error) {}
"""),
Example("""
@_exported import UnknownModule
""")
] + nonTriggeringExamplesVersionAdditions

Expand Down Expand Up @@ -122,7 +125,7 @@ struct UnusedImportRuleExamples {
dispatchMain()
"""),
Example("""
↓@_exported import Foundation
↓@_implementationOnly import Foundation
Copy link
Contributor Author

@jszumski jszumski Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original intent in #2993 seemed to make sure any attributed import was correctable, so I arbitrarily chose @_implementationOnly as a replacement.

import Dispatch
dispatchMain()
"""):
Expand Down