Open grantneufeld opened 1 year ago
Update:
If the import SwiftUI
line is removed from the sample failing file above, two errors show up in Xcode:
// removed:
// import SwiftUI
/// Since EditMode only exists for iOS, here’s a simplified version for macOS.
final class EditMode: ObservableObject { // 🛑 Cannot find type 'ObservableObject' in scope
/// True if the view is in “edit” mode.
@Published var isEditing = false // 🛑 Unknown attribute 'Published'
}
If I instead swap in Foundation
for SwiftUI
, then the errors go away, and SwiftLint Analyze does not incorrectly report an unused_import
violation.
Weird.
Even though import SwiftUI
makes the code work, the rule is considered violated when I use that import instead of import Foundation
. 🤷
More data for this:
import Foundation
enum A11y {
static let min: CGFloat = 44
}
The above example triggers the false rule violation. (CGFloat
is not an available type unless defined by an import).
Substituting import CoreGraphics
for the import Foundation
line also produces the false rule violation.
But substituting import CoreFoundation.CFNumber
does not trigger the rule violation.
So it seems that the unused_import
rule is triggering anytime the import is needed, but could be replaced with a more specific import. Weird.
Right, so what is happening is this:
swiftlint
is asking for a cursorInfo for ObservableObject
tokensourcekit
replies that ObservableObject
is actually Combine.ObservableObject
from Combine module.swiftlint
then decides that no objects were references from SwiftUI
and removes it from importsThe issue is that ObservableObject
is actually was imported throught SwiftUI.
So suggestion is to have something like allowed_transitive
rules, which will check if we actually using one of the objects and inside the re-exported module.
OR:
As an alternative, we could use the same configuration for detecting such issues, and not remove imports
that are actually used like Umbrella import :)
https://github.com/realm/SwiftLint/pull/5622 as a draft
@jpsim @SimplyDanny
New Issue Checklist
Describe the bug
Multi-target (iOS and macOS) project, when building for macOS (platform is important for my example: the code is a replacement for the
EditMode
SwiftUI provides on iOS but not on macOS), I have a file that imports SwiftUI to be able to specify that a class conforms to theObservableObject
protocol, and has a@Published
property.The
unused_import
rule is incorrectly reporting thatimport
call as unused. If I remove the import line, I get errors in Xcode:Cannot find type 'ObservableObject' in scope
Unknown attribute 'Published'
So, clearly, the compiler uses the import.
Complete output when running SwiftLint, including the stack trace and command used
Xcode Build:
Outputs:
The SwiftLint Analyse:
Outputs:
Environment
(I normally have the content of that file wrapped in
#if os(macOS)
…#endif
compiler directives, but trimmed that out and built just for the macOS destination to see if that would change anything, but the incorrect rule warning came out the same.)