Skip to content

Commit

Permalink
Merge branch 'main' into jszumski/fix-unused-imports-constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jszumski committed Sep 30, 2023
2 parents 7911d3f + 68b27d8 commit bfb4101
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 70 deletions.
17 changes: 11 additions & 6 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,11 @@

#### Bug Fixes

* 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)

* Fix false positive in `unused_import` rule when using a constructor
defined in a transitive module.
[jszumski](https://github.com/jszumski)
Expand Down Expand Up @@ -65,7 +70,7 @@
Bash, Zsh and fish.
[SimplyDanny](https://github.com/SimplyDanny)

* Add new `private_swiftui_state_property` opt-in rule to encourage setting
* Add new `private_swiftui_state` opt-in rule to encourage setting
SwiftUI `@State` and `@StateObject` properties to private.
[mt00chikin](https://github.com/mt00chikin)
[#3173](https://github.com/realm/SwiftLint/issues/3173)
Expand Down Expand Up @@ -100,7 +105,7 @@
[#5203](https://github.com/realm/SwiftLint/issues/5203)

* Improved the reported location and reasons provided for issues
detected by the `invalid_seiftlint_command` rule.
detected by the `invalid_swiftlint_command` rule.
[Martin Redington](https://github.com/mildm8nnered)
[#5204](https://github.com/realm/SwiftLint/issues/5204)

Expand All @@ -110,7 +115,7 @@
[#5215](https://github.com/realm/SwiftLint/issues/5215)

* Adds a `strict` configuration file setting, equivalent to the `--strict`
command line option.
command line option.
[Martin Redington](https://github.com/mildm8nnered)
[#5226](https://github.com/realm/SwiftLint/issues/5226)

Expand Down Expand Up @@ -468,7 +473,7 @@
subclasses.
[AndrewDMontgomery](https://github.com/andrewdmontgomery)
[#4875](https://github.com/realm/SwiftLint/pull/4875)

* Prepend `warning: ` to error messages so that they show in Xcode.
[whiteio](https://github.com/whiteio)
[#4923](https://github.com/realm/SwiftLint/issues/4923)
Expand Down Expand Up @@ -527,8 +532,8 @@
At the same time, make it an opt-in rule.
[SimplyDanny](https://github.com/SimplyDanny)
[#4615](https://github.com/realm/SwiftLint/issues/4615)
* Interpret strings in `excluded` option of `identifier_name`,

* Interpret strings in `excluded` option of `identifier_name`,
`type_name` and `generic_type_name` rules as regular expression. Existing
configurations should remain working without notice as long as they don't
contain characters that must be escaped in regular expression.
Expand Down
Expand Up @@ -70,6 +70,15 @@ struct UnusedDeclarationRuleExamples {
func testExample() {}
}
"""),
Example("""
struct S {
var i: Int? = nil
func f() {
if let i { print(i) }
}
}
S().f()
"""),
Example("""
enum Component {
case string(StaticString)
Expand Down Expand Up @@ -128,7 +137,7 @@ struct UnusedDeclarationRuleExamples {
"hello"
}
""")
] + platformSpecificNonTriggeringExamples + versionSpecificNonTriggeringExamples
] + platformSpecificNonTriggeringExamples

static let triggeringExamples = [
Example("""
Expand Down Expand Up @@ -301,20 +310,4 @@ struct UnusedDeclarationRuleExamples {
private static let platformSpecificNonTriggeringExamples = [Example]()
private static let platformSpecificTriggeringExamples = [Example]()
#endif

#if compiler(>=5.8)
private static let versionSpecificNonTriggeringExamples = [
Example("""
struct S {
var i: Int? = nil
func f() {
if let i { print(i) }
}
}
S().f()
""")
]
#else
private static let versionSpecificNonTriggeringExamples = [Example]()
#endif
}
Expand Up @@ -190,7 +190,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,26 +16,16 @@ struct UnusedImportRuleExamples {
Example("""
import UnknownModule
func foo(error: Swift.Error) {}
""")
] + nonTriggeringExamplesVersionAdditions

#if compiler(>=5.8)
private static let nonTriggeringExamplesVersionAdditions = [
"""),
Example("""
import Foundation
let 👨‍👩‍👧‍👦 = #selector(NSArray.contains(_:))
👨‍👩‍👧‍👦 == 👨‍👩‍👧‍👦
""")
]
#else
private static let nonTriggeringExamplesVersionAdditions = [
@_exported import UnknownModule
"""),
Example("""
import Foundation
let 👨‍👩‍👧‍👦 = #selector(NSArray.contains(_:))
👨‍👩‍👧‍👦 == 👨‍👩‍👧‍👦
""")
]
#endif

static let triggeringExamples = [
Example("""
Expand Down Expand Up @@ -122,7 +112,7 @@ struct UnusedImportRuleExamples {
dispatchMain()
"""),
Example("""
↓@_exported import Foundation
↓@_implementationOnly import Foundation
import Dispatch
dispatchMain()
"""):
Expand Down
Expand Up @@ -166,8 +166,31 @@ struct RedundantSelfInClosureRuleExamples {
f { let g = ↓self.g() }
}
}
""", excludeFromDocumentation: true)
] + triggeringCompilerSpecificExamples
""", excludeFromDocumentation: true),
Example("""
class C {
var x = 0
func f(_ work: @escaping () -> Void) { work() }
func g() {
f { [weak self] in
self?.x = 1
guard let self else { return }
↓self.x = 1
}
f { [weak self] in
self?.x = 1
if let self = self else { ↓self.x = 1 }
self?.x = 1
}
f { [weak self] in
self?.x = 1
while let self else { ↓self.x = 1 }
self?.x = 1
}
}
}
""")
]

static let corrections = [
Example("""
Expand All @@ -194,34 +217,4 @@ struct RedundantSelfInClosureRuleExamples {
}
""")
]

#if compiler(>=5.8)
private static let triggeringCompilerSpecificExamples = [
Example("""
class C {
var x = 0
func f(_ work: @escaping () -> Void) { work() }
func g() {
f { [weak self] in
self?.x = 1
guard let self else { return }
↓self.x = 1
}
f { [weak self] in
self?.x = 1
if let self = self else { ↓self.x = 1 }
self?.x = 1
}
f { [weak self] in
self?.x = 1
while let self else { ↓self.x = 1 }
self?.x = 1
}
}
}
""")
]
#else
private static let triggeringCompilerSpecificExamples = [Example]()
#endif
}

0 comments on commit bfb4101

Please sign in to comment.