Skip to content

implicit_optional_initialization: add ignore_attributes support - #6513

Merged
SimplyDanny merged 4 commits into
realm:mainfrom
theamodhshetty:codex/implicit-optional-ignore-attributes
Feb 28, 2026
Merged

SimplyDanny merged 4 commits into
realm:mainfrom
theamodhshetty:codex/implicit-optional-ignore-attributes

Conversation

@theamodhshetty

Copy link
Copy Markdown
Contributor

Summary

Adds a targeted escape hatch for implicit_optional_initialization when explicit = nil is required by specific wrappers/attributes.

Fixes #3998.

What changed

  • Added ignore_attributes configuration to implicit_optional_initialization.
  • Updated rule logic to skip style checks for declarations annotated with ignored attributes (for style: always).
  • Added rule examples covering both ignored and non-ignored attribute cases.
  • Added a changelog entry.

Why

Some property wrappers/attributes require explicit optional initialization (= nil) to compile.
A previous broad approach (skip all custom attributes) was too permissive. This keeps behavior explicit and configurable.

Validation

  • swift test --filter ImplicitOptionalInitializationRuleGeneratedTests

Example config

implicit_optional_initialization:
  style: always
  ignore_attributes:
    - Wrapper

@SwiftLintBot

SwiftLintBot commented Feb 27, 2026 •

Copy link
Copy Markdown
19 Messages
📖 Building this branch resulted in a binary size of 27277.54 KiB vs 27276.45 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.82 s vs 0.8 s on main (2% slower).
📖 Linting Alamofire with this PR took 1.03 s vs 1.05 s on main (1% faster).
📖 Linting Brave with this PR took 7.12 s vs 7.15 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 25.64 s vs 25.62 s on main (0% slower).
📖 Linting Firefox with this PR took 11.8 s vs 11.79 s on main (0% slower).
📖 Linting Kickstarter with this PR took 8.02 s vs 8.02 s on main (0% slower).
📖 Linting Moya with this PR took 0.45 s vs 0.46 s on main (2% faster).
📖 Linting NetNewsWire with this PR took 2.44 s vs 2.45 s on main (0% faster).
📖 Linting Nimble with this PR took 0.71 s vs 0.7 s on main (1% slower).
📖 Linting PocketCasts with this PR took 7.61 s vs 7.66 s on main (0% faster).
📖 Linting Quick with this PR took 0.44 s vs 0.46 s on main (4% faster).
📖 Linting Realm with this PR took 3.03 s vs 2.99 s on main (1% slower).
📖 Linting Sourcery with this PR took 1.88 s vs 1.83 s on main (2% slower).
📖 Linting Swift with this PR took 4.58 s vs 4.65 s on main (1% faster).
📖 Linting SwiftLintPerformanceTests with this PR took 0.35 s vs 0.35 s on main (0% slower).
📖 Linting VLC with this PR took 1.18 s vs 1.16 s on main (1% slower).
📖 Linting Wire with this PR took 18.5 s vs 18.48 s on main (0% slower).
📖 Linting WordPress with this PR took 12.5 s vs 12.51 s on main (0% faster).

Generated by 🚫 Danger

@SimplyDanny SimplyDanny left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! What about the never style for which auto-correction would add a = nil.

@theamodhshetty

Copy link
Copy Markdown
Contributor Author

Thanks for the catch on style: never.

I pushed 1da5182 to apply ignore_attributes checks for both styles (always and never) and added examples/tests for the never path.

@theamodhshetty

theamodhshetty commented Feb 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Follow-up fix for the failing Registration step:

  • ran rule/reporter registration and committed the generated default config update
  • added ignore_attributes: [] under implicit_optional_initialization in Tests/IntegrationTests/Resources/default_rule_configurations.yml

Commit: d9e7257

@SimplyDanny SimplyDanny left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now! However, you need to make sure the rewriter applies the same logic.

@theamodhshetty

theamodhshetty commented Feb 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for calling this out. I added explicit correction coverage to ensure the rewriter follows ignore_attributes for both styles:

  • style: never + ignored attribute remains unchanged
  • style: always + ignored attribute remains unchanged

I also ran:
swift test --filter ImplicitOptionalInitializationRuleGeneratedTests

and it passes locally.

@SimplyDanny
SimplyDanny enabled auto-merge (squash) February 28, 2026 10:41
@SimplyDanny
SimplyDanny merged commit 15d7a3d into realm:main Feb 28, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

redundant_optional_initialization warns on wrapped nilable property leading to compile errors

3 participants