unused_declaration does not catch View only used in PreviewPeovider
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
Describe the bug
When a View is only used by it's matching PreviewProvider unused_declarations does not report it as unused. E.g. if this is in a file and the View is not referenced anywhere else SwiftLint will not report any issues:
struct UnusedView: View {
var body: some View {
Text("Hello, World!")
}
}
struct UnusedView_Previews: PreviewProvider {
static var previews: some View {
UnusedView()
}
}
This may be expected behavior, the View is being used, however I was surprised by it and don't consider that a "real" use. If this isn't reported as unused then no SwiftUI View with a Preview will ever be "unused", even though they aren't actually being used anywhere.
Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint analyze --compiler-log-path <path>
Analyzing Swift files in current working directory
Collecting 'UnusedDeclarationsPreviewsApp.swift' (1/3)
Collecting 'ContentView.swift' (2/3)
Collecting 'UnusedView.swift' (3/3)
Analyzing 'UnusedDeclarationsPreviewsApp.swift' (1/3)
Analyzing 'ContentView.swift' (2/3)
Analyzing 'UnusedView.swift' (3/3)
Done analyzing! Found 0 violations, 0 serious in 3 files.
Environment
- SwiftLint version (run
swiftlint versionto be sure)?
0.50.3 - Installation method used (Homebrew, CocoaPods, building from source, etc)?
HomeBrew - Paste your configuration file:
analyzer_rules:
- unused_declaration
- Are you using nested configurations?
No - Which Xcode version are you using (check
xcodebuild -version)?
Xcode 14.2
Build version 14C18 - Do you have a sample that shows the issue?
See above for code, but I've also provided a sample project.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the unused_declaration analyzer and reproduce the report using the SwiftUI View and PreviewProvider example, or the linked sample project. Run swiftlint analyze with the provided compiler log setup and determine how PreviewProvider references are handled. Done means the behavior is clarified and covered by an appropriate analyzer regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100