realm/SwiftLint

Optional TipKit parameters trigger a `redundant_optional_initialization` violation

Open

#5,884 创建于 2024年12月5日

在 GitHub 查看
 (6 评论) (0 反应) (0 负责人)Swift (19,570 star) (2,295 fork)batch import
enhancementgood first issue

描述

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:
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"]

贡献者指南