Skip to content

Add include_variables option to non_optional_string_data_conversion rule#6172

Merged
SimplyDanny merged 1 commit intomainfrom
copilot/fix-6094-2
Aug 2, 2025
Merged

Add include_variables option to non_optional_string_data_conversion rule#6172
SimplyDanny merged 1 commit intomainfrom
copilot/fix-6094-2

Conversation

Copy link
Contributor

Copilot AI commented Aug 1, 2025

The non_optional_string_data_conversion rule was previously limited to only triggering on string literals like "foo".data(using: .utf8), but would not catch variable usage like stringVariable.data(using: .utf8).

This PR adds an optional include_variables configuration option (default: false) that allows the rule to also trigger on variables, properties, function calls, and other non-literal expressions when enabled.

Changes Made

  • Added custom configuration: Created NonOptionalStringDataConversionConfiguration in a separate file with include_variables: false by default
  • Enhanced visitor logic: Updated the rule to check both string literals (always) and variables (when enabled)
  • Integrated examples into rule description: Added comprehensive examples covering string literals, variables, properties, function calls, and chained expressions directly in the rule's description for better documentation
  • Updated changelog: Added entry documenting the new option and its behavior

Usage

Default behavior (unchanged for backward compatibility):

"foo".data(using: .utf8)        // ⚠️ Warning
stringVar.data(using: .utf8)    // ✅ No warning

With include_variables enabled:

non_optional_string_data_conversion:
  include_variables: true
"foo".data(using: .utf8)              // ⚠️ Warning
stringVar.data(using: .utf8)          // ⚠️ Warning  
obj.prop.data(using: .utf8)           // ⚠️ Warning
getString().data(using: .utf8)        // ⚠️ Warning
optional?.data(using: .utf8)          // ⚠️ Warning

// Still correctly ignored:
Data("foo".utf8)                      // ✅ Correct usage
string.data(using: .ascii)            // ✅ Non-UTF8 encoding

This addresses the maintainer's suggestion in the issue comments to add an optional feature for variable checking while balancing false positive concerns.

Fixes #6094.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] non_optional_string_data_conversion does not trigger with non-literal strings Add include_variables option to non_optional_string_data_conversion rule Aug 1, 2025
Copilot AI requested a review from SimplyDanny August 1, 2025 14:52
@SimplyDanny SimplyDanny marked this pull request as ready for review August 1, 2025 14:53
@SwiftLintBot
Copy link

SwiftLintBot commented Aug 1, 2025

18 Messages
📖 Building this branch resulted in a binary size of 25491.34 KiB vs 25468.68 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 1.05 s vs 1.06 s on main (0% faster).
📖 Linting Alamofire with this PR took 1.41 s vs 1.41 s on main (0% slower).
📖 Linting Brave with this PR took 9.73 s vs 9.54 s on main (1% slower).
📖 Linting DuckDuckGo with this PR took 26.71 s vs 26.73 s on main (0% faster).
📖 Linting Firefox with this PR took 13.9 s vs 13.9 s on main (0% slower).
📖 Linting Kickstarter with this PR took 10.73 s vs 10.71 s on main (0% slower).
📖 Linting Moya with this PR took 0.56 s vs 0.56 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 3.21 s vs 3.24 s on main (0% faster).
📖 Linting Nimble with this PR took 0.86 s vs 0.85 s on main (1% slower).
📖 Linting PocketCasts with this PR took 9.33 s vs 9.36 s on main (0% faster).
📖 Linting Quick with this PR took 0.49 s vs 0.51 s on main (3% faster).
📖 Linting Realm with this PR took 4.9 s vs 4.88 s on main (0% slower).
📖 Linting Sourcery with this PR took 2.49 s vs 2.48 s on main (0% slower).
📖 Linting Swift with this PR took 5.72 s vs 5.71 s on main (0% slower).
📖 Linting VLC with this PR took 1.5 s vs 1.5 s on main (0% slower).
📖 Linting Wire with this PR took 22.67 s vs 22.68 s on main (0% faster).
📖 Linting WordPress with this PR took 13.03 s vs 13.05 s on main (0% faster).

Generated by 🚫 Danger

Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

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

Add a changelog entry to CHANGELOG.md to let users know about the new option and its behavior.

Copy link
Contributor Author

Copilot AI commented Aug 1, 2025

Add a changelog entry to CHANGELOG.md to let users know about the new option and its behavior.

Added changelog entry to the Enhancements section documenting the new include_variables option and its default behavior. b7c74dd

Copilot AI requested a review from SimplyDanny August 1, 2025 15:33
@SimplyDanny SimplyDanny force-pushed the copilot/fix-6094-2 branch 2 times, most recently from d9b9463 to e468e33 Compare August 1, 2025 21:25
@SimplyDanny SimplyDanny enabled auto-merge (squash) August 1, 2025 21:25
@SimplyDanny SimplyDanny merged commit 8bb69b0 into main Aug 2, 2025
20 of 22 checks passed
@SimplyDanny SimplyDanny deleted the copilot/fix-6094-2 branch August 2, 2025 21:49
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.

non_optional_string_data_conversion does not trigger with non-literal strings

3 participants