realm / realm/SwiftLint

`accessibility_trait_for_button` false positive with `if #available` check

Open
#6,446 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.