Skip to content

accessibility_trait_for_button false positive with if #available check #6446

@hbiede

Description

@hbiede

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementIdeas for improvements of existing features and rules.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions