SwiftLint
SwiftLint copied to clipboard
Optional TipKit parameters trigger a `redundant_optional_initialization` violation
New Issue Checklist
- [x] I've Updated SwiftLint to the latest version.
- [x] I've searched for existing GitHub issues.
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"]
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.
Can i work on this ?
Can i work on this ?
Absolutely :-)
I am sorry. I am getting too many lint errors. It might take some time from my side
I may have a fix for this. Are you okay with me opening up a pull request for it? @ShubhamDindorkar
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.