You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 the ObservableObject protocol, and has a @Published property.
The unused_import rule is incorrectly reporting that import 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
2023-08-10 20:06:15.685 xcodebuild[73360:7531983] DVTCoreDeviceEnabledState: DVTCoreDeviceEnabledState_Disabled set via user default (DVTEnableCoreDevice=disabled)
Analyzing Swift files in current working directory
... (many Collecting lines omitted) ...
... (many Analyzing lines omitted) ...
Analyzing 'EditMode.swift' (9/14)
/Users/user/Projects/MyApp/MyApp/Views/EditMode.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
... (more Analyzing lines omitted) ...
Done analyzing! Found 1 violation, 0 serious in 41 files.
(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.)
The text was updated successfully, but these errors were encountered:
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.finalclassEditMode:ObservableObject{// 🛑 Cannot find type 'ObservableObject' in scope/// True if the view is in “edit” mode.@PublishedvarisEditing= 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. 🤷
swiftlint is asking for a cursorInfo for ObservableObject token
sourcekit replies that ObservableObject is actually Combine.ObservableObject from Combine module.
swiftlint then decides that no objects were references from SwiftUI and removes it from imports
The 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.
As an alternative, we could use the same configuration for detecting such issues, and not remove imports that are actually used like Umbrella import :)
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:
$ xcodebuild -project WebExtractor.xcodeproj -scheme WebExtractor -destination platform=macOS,arch=x86_64 > xcodebuild.log
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.)The text was updated successfully, but these errors were encountered: