Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional_data_string_conversion throws error #5845

Open
HHuckebein opened this issue Oct 31, 2024 · 2 comments
Open

optional_data_string_conversion throws error #5845

HHuckebein opened this issue Oct 31, 2024 · 2 comments
Labels
discussion Topics that cannot be categorized as bugs or enhancements yet. They require further discussions.

Comments

@HHuckebein
Copy link

New Issue Checklist

Bug Description

When using the code below, SwiftLint 0.57.0 reports an error like this
Optional Data -> String Conversion Violation: Prefer failable String(data:encoding:) initializer when converting Data to String (optional_data_string_conversion)

but following the advice leads to Value of optional type 'String?' must be unwrapped to a value of type 'String'.

// This triggers a violation:
    var description: String {
        do {
            let data = try encode()
            return String(decoding: data, as: UTF8.self)
        } catch {
            return error.message.description
        }
    }

Mention the command or other SwiftLint integration method that caused the issue. Include stack traces or command output.

None

Environment

  • SwiftLint version (run swiftlint version to be sure) 0.57.0
  • Xcode version (run xcodebuild -version to be sure) Xcode 16.2 Build version 16B5100e
  • Installation method used (Homebrew, CocoaPods, building from source, etc) Homebrew
  • Configuration file:
disabled_rules:

opt_in_rules:
- collection_alignment
#- conditional_returns_on_newline
- convenience_type
- empty_count
- empty_string
- fallthrough
- fatal_error_message
- file_header
- file_name
- file_name_no_space
- first_where
- flatmap_over_map_reduce
- for_where
- force_unwrapping
- identical_operands
- implicit_return
- implicitly_unwrapped_optional
- is_disjoint
- joined_default_parameter
- legacy_multiple
- legacy_random
- last_where
- lower_acl_than_parent
- modifier_order
- multiline_parameters
- overridden_super_call
- prefixed_toplevel_constant
- prefer_zero_over_explicit_init
- private_action
- private_outlet
- static_operator
- strict_fileprivate
- toggle_bool
#- trailing_closure
- unavailable_function
- unowned_variable_capture
- unneeded_parentheses_in_closure_argument
- untyped_error_in_catch
- yoda_condition

analyzer_rules:
- unused_declaration
- unused_import
    
excluded: # paths to ignore during linting.
- Frameworks
- Products
- Recovered References
- Carthage
- packages/*/Package.swift
- Packages/*/Package.swift

- packages/*/Tests/
- Packages/*/Tests/

# Default rule configuration
cyclomatic_complexity:
    ignores_case_statements: true

empty_count:
    only_after_dot: true

function_parameter_count: 6

function_body_length: 80

identifier_name:
    allowed_symbols: _ # Is used for private property definitions
    max_length: 50

    excluded:
        - id
        - at
        - on
        - to
        - cc
        - i
        - ok
        - no
        - qa
        - ID
        - xl
        - d1
        - d2
        - tr
        - vc
        - up
        - by

large_tuple: # warn user when using 4 values in tuple, give error if there are 5
- 3

line_length: 200

nesting:
    type_level: 3

type_body_length: 270

type_name:
    max_length: 60
    excluded:
    - AX
    - LS

# Opt-in rule configuration
file_name:
 excluded:
 - "BundleConfig_Root.swift"
 - "BundleConfig_Root+Extension.swift"
 - "BundleConfig_Insurance.swift"
 - "BundleConfig_Insurance+Extension.swift"

file_header:
 required_pattern: |
                    \/\/
                    \/\/ Copyright \(c\) *******\. All rights reserved\.
                    \/\/

Are you using nested configurations? If so, paste their
relative paths and respective contents.

@SimplyDanny
Copy link
Collaborator

That's the idea of the rule. It asks you to use the failable initializer to make you think about the error case. Your current code will just fail at runtime in case data cannot be converted.

@SimplyDanny SimplyDanny added the discussion Topics that cannot be categorized as bugs or enhancements yet. They require further discussions. label Nov 3, 2024
@HHuckebein
Copy link
Author

The object created by try encode() is non-optional and String(decoding: data, as: UTF8.self) is a non-failing initializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Topics that cannot be categorized as bugs or enhancements yet. They require further discussions.
Projects
None yet
Development

No branches or pull requests

2 participants