SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Optional TipKit parameters trigger a `redundant_optional_initialization` violation

Open achernenko-schibsted opened this issue 11 months ago • 6 comments
trafficstars

New Issue Checklist

Bug Description

TipKit @Parameters are defined as follows:

import TipKit

extension Tips {
    @Parameter static var someParameter: Bool? = nil
}

This code triggers the redundant_optional_initialization rule. However, the initial value cannot be omitted here, otherwise @Parameter (which is a Swift macro) generates a build error: "Unable to expand due to missing identifier pattern.", so I have to disable the rule.

Environment

  • SwiftLint version: 0.57.1
  • Xcode version: 16.1
  • Installation method used: Homebrew
  • Configuration file:
swiftlint.yml
excluded: # paths to ignore during linting. Takes precedence over `included`.
  - Scripts
  - ReferenceImages
  - BuildSystem
  - Configurations
  - AppStoreConnect
  - node_modules
disabled_rules: # rule identifiers to exclude from running
  - line_length
  - identifier_name
  - todo
  - file_length
  - nesting
  - comma_inheritance
  - type_body_length
  - trailing_whitespace
  - static_over_final_class
opt_in_rules:
  - discouraged_assert
  - empty_count
  - first_where
  - empty_string
  - toggle_bool
  - convenience_type
  - overridden_super_call
  - shorthand_optional_binding
  - unneeded_parentheses_in_closure_argument
  - accessibility_label_for_image
  - accessibility_trait_for_button
  - array_init
  - attributes
  - closure_end_indentation
  - collection_alignment
  - yoda_condition
  - direct_return
analyzer_rules:
  - unused_import
custom_rules:
  invalid_hex_color:
    included: ".*\\.swift"
    name: "Invalid hex color"
    regex: 'hexString: "\#{0}[0-9a-fA-F]{3,8}"'
    message: "'hexString' should always be prefixed with a #"
    severity: error
  localizedDescription:
    included: ".*\\.swift"
    name: "Don't use localizedDescription"
    regex: '(\w+\.localizedDescription)'
    message: 'Don''t use .localizedDescription. Use "(error)" instead to get the full error string'
    severity: error
  no_ui_colors:
    name: "Don't use Standard UIColors"
    excluded: "(WidgetExtension)\\/.*|.*\\/WidgetExtension\\.swift|.*\\/UIColor\\+Extensions\\.swift|.*\\/Color\\+Extensions\\.swift"
    regex: '[.]\b(themeRed|themeBlue|themeWhite|themeBlack|systemBlue|systemGreen|systemIndigo|systemOrange|systemPink|systemPurple|systemRed|systemTeal|systemYellow|systemGray|systemGray2|systemGray3|systemGray4|systemGray5|systemGray6|black|blue|brown|cyan|darkGray|gray|green|lightGray|magenta|orange|purple|red|white|yellow)\b'
    message: "Only use the colors from UIColor+Extensions.swift"
    severity: warning
  no_color_initializers:
    name: "Don't use color initializers"
    excluded: "(WidgetExtension)\\/.*|.*\\/WidgetExtension\\.swift|.*\\/UIColor\\+Extensions\\.swift|.*\\/Color\\+Extensions\\.swift"
    regex: '\s(UIColor\(.*\)|.init\(hexString:.*\)|.init\(hue:.*saturation:.*brightness:.*\)|.init\(white:.*alpha:.*\)|.init\(red:.*green:.*blue:.*\)|.init\(red:.*green:.*blue:.*alpha:.*\))'
    message: "Only use the colors from UIColor+Extensions.swift"
    severity: warning
  no_color_generators:
    name: "Don't use color generators"
    excluded: "(WidgetExtension)\\/.*|.*\\/WidgetExtension\\.swift|.*\\/UIColor\\+Extensions\\.swift"
    regex: '\.(withAlphaComponent|darker|lighter)'
    message: "Only use the colors from UIColor+Extensions.swift"
    severity: warning
  no_optional_decode_initial_overridable:
    regex: 'decodeIfPresent\(InitialOverridable'
    name: "Don't decodeIfPresent InitialOverridable<T>"
    message: "InitialOverridable<T> should always be decoded as non-optional."
  no_raw_nsattributed_string_attributes:
    regex: 'NSAttributedString\(string: [\S]+,\s+attributes: [\S]+\)'
    name: "Do not use raw NSAttributedString attributes"
    message: "Use .init(string:safeAttributes:) instead"
  use_makefont:
    name: "Avoid UIFont(name:size)"
    regex: 'UIFont\(name: .*'
    message: "Use `UIFont.makeFont(name:size)` instead of `UIFont(name:size:)`"
    severity: warning
  disable_print:
    name: "print usage"
    regex: "((\\bprint)|(\\bdebugPrint)|(Swift\\.print))\\s*\\("
    message: "Don't use print statements in production code."
    severity: warning
    match_kinds:
      - identifier
overridden_super_call:
  included:
    - "*"
    - "viewSafeAreaInsetsDidChange()"
    - "viewLayoutMarginsDidChange()"
    - "viewDidLayoutSubviews()"
    - "viewWillTransition(to:with:)"
    - "collectionView(_:willDisplay:forItemAt:)"
    - "collectionView(_:didEndDisplaying:forItemAt:)"
attributes:
  attributes_with_arguments_always_on_line_above: false
  always_on_line_above:
    ["@MainActor", "@WidgetBundleBuilder", "@available", "@AppStorage"]

achernenko-schibsted avatar Dec 05 '24 10:12 achernenko-schibsted

To avoid restricting the rule too much by ignoring variable declarations with attributes entirely, we might instead go with a new option that allows to skip variables annotated by any of a defined set of parameter names.

SimplyDanny avatar Dec 05 '24 20:12 SimplyDanny

Can i work on this ?

ShubhamDindorkar avatar Mar 12 '25 16:03 ShubhamDindorkar

Can i work on this ?

Absolutely :-)

mildm8nnered avatar Mar 12 '25 17:03 mildm8nnered

I am sorry. I am getting too many lint errors. It might take some time from my side

ShubhamDindorkar avatar Mar 18 '25 13:03 ShubhamDindorkar

I may have a fix for this. Are you okay with me opening up a pull request for it? @ShubhamDindorkar

alex-x-x-x-x avatar Apr 07 '25 00:04 alex-x-x-x-x

Yeah I also have a fix but some tests are remaining. So if you are sure you can pass all the test, Sure go ahead.

ShubhamDindorkar avatar Apr 07 '25 07:04 ShubhamDindorkar