Repository navigation
Skip force_unwrapping for calls with static string literal args - #6490
Conversation
… arguments When force-unwrapping the result of a function call where all arguments are static string literals (no interpolation), the violation is now suppressed. This covers common safe patterns like: URL(string: "https://example.com")! Data(hexString: "AABBCCDD")! Force unwraps with variable arguments or string interpolation still trigger as before. Closes realm#6487
Generated by 🚫 Danger |
Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Added a CHANGELOG entry under Enhancements. Thanks for the CI run — glad to see 0 errors and the real-world violation fixes across projects like Aerial. |
SimplyDanny
left a comment
There was a problem hiding this comment.
Thanks! The approach is reasonable, but we should be explicit about the function calls to ignore.
- Use key path syntax instead of closure to fix prefer_key_path violation - Use onlyElement?.is() per reviewer suggestion for cleaner check Co-Authored-By: Claude Opus 4.6 <[email protected]>
Replace the general heuristic (any function with static string args) with an explicit, configurable set of function names. Defaults include URL(string:), NSURL(string:), UIImage(named:), NSImage(named:), and Data(hexString:). Users can extend the list via the static_string_argument_functions configuration option. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Updated to use an explicit, configurable allowlist per your feedback. The new
Users can extend the set in their force_unwrapping:
static_string_argument_functions:
- MyType(identifier:)The postprocessor pattern (from Also normalizes |
SimplyDanny
left a comment
There was a problem hiding this comment.
Looks good! You need to run swift run swiftlint-dev rules register to make sure the test validating configurations passes.
Rename the configuration key and Swift property from static_string_argument_functions to ignored_literal_argument_functions per reviewer feedback. The more generic name leaves room for future extension to other literal types (integer, boolean, etc.). Also updates default_rule_configurations.yml to reflect the new option name and its default values. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add a non-triggering example that proves an arbitrary function configured in ignored_literal_argument_functions is correctly excluded from violations. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Added — the new non-triggering example uses |
realm#6490) Co-authored-by: Claude Opus 4.6 <[email protected]> Co-authored-by: Danny Mösch <[email protected]>
realm#6490) Co-authored-by: Claude Opus 4.6 <[email protected]> Co-authored-by: Danny Mösch <[email protected]>
Summary
Suppresses
force_unwrappingviolations when the force-unwrapped expression is a function call where all arguments are static string literals (no interpolation).Common patterns like these are now allowed:
While these still trigger violations:
Rationale
When a developer writes a function call with a hardcoded string literal, they know the input is valid at compile time. Force unwrapping in this case is a deliberate, informed choice rather than a potential crash risk. This is one of the most common patterns flagged as a false positive by the current rule.
Changes
isCallWithOnlyStaticStringArgumentscheck to theForceUnwrapExprSyntaxvisitorisStaticStringLiteralhelper that verifies a string has no interpolation segmentsnonTriggeringExamplesandtriggeringExamplesto cover the new behaviorNSURL(string: "http://www.google.com")!from triggering to non-triggeringCloses #6487