`accessibility_trait_for_button` false positive with `if #available` check
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
- I've Updated SwiftLint to the latest version.
- I've searched for existing GitHub issues.
Bug Description
Using . isToggle in an app that supports a version before iOS 17 requires an if #available, which breaks this lint rule. Example:
struct ContentView: View {
var body: some View {
Text("Hello, World!")
.onTapGesture {
print("foo")
}
.accessibilityAddTraits({
if #available(iOS 17, *) {
[.isToggle, .isButton]
} else {
.isButton
}
}())
}
}
Mention the command or other SwiftLint integration method that caused the issue. Include stack traces or command output.
$ swiftlint lint --strict
Linting Swift files in current working directory
Linting 'TestApp.swift' (2/2)
Linting 'ContentView.swift' (1/2)
~/Desktop/test/test/ContentView.swift:12:9: error: Accessibility Trait for Button Violation: All views with tap gestures added should include the .isButton or the .isLink accessibility traits (accessibility_trait_for_button)
Done linting! Found 1 violation, 1 serious in 2 files.
Environment
- SwiftLint version:
$swiftlint version
0.63.1
- Xcode version:
$xcodebuild -version
Xcode 26.2
Build version 17C52
- Installation method used: Homebrew
- Configuration file:
# Ran in minimal setup to test
opt_in_rules:
- accessibility_trait_for_button
Are you using nested configurations? No
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 accessibility_trait_for_button rule and reproduce the report using the provided Swift example and minimal opt_in_rules configuration. Trace how the if #available expression is analyzed; done means the valid .isButton trait in that branch no longer produces a false positive, with coverage for the reported case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100