Skip to content

Legacy_objc_type rule triggered but there's no suitable swift value types #6810

Open

Description

@victor

New Issue Checklist

Bug Description

The legacy_objc_type triggers when I'm using a method that needs conformance to NSObjectProtocol, so I can't use String instead of NSString (for example with NSItemProvider):

if provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) {
    // This triggers a violation:
    _ = provider.loadObject(ofClass: NSString.self) { string, _ in
        if let text = string as? String {
            …
        }
    }
    return true
}

This forces me to place a disabling comment, but ideally the linter should recognize that loadObject(ofClass:), and other APIs like it, expect an NSObjectProtocol conforming type, so value types are not really suitable.

I'm invoking SwiftLint like this:

$ swiftlint --strict

Environment

  • SwiftLint version 0.65.0
  • Xcode version 27.0 (27A5209h)
  • Installation method used: homebrew
  • Configuration file:
only_rules:
  # Spacing and Formatting
  - leading_whitespace
  - trailing_whitespace
  - trailing_newline
  - vertical_whitespace
  - vertical_whitespace_closing_braces
  - vertical_whitespace_opening_braces
  - indentation_width
  - empty_count
  - empty_string
  - empty_collection_literal

  # Code Structure
  - control_statement
  - colon
  - comma
  - opening_brace
  - closing_brace
  - return_arrow_whitespace
  - function_name_whitespace
  - operator_usage_whitespace
  - trailing_semicolon
  - generic_type_name
  - type_name
  - identifier_name
  - function_parameter_count

  # Swift Best Practices
  - redundant_string_enum_value
  - implicit_optional_initialization
  - redundant_nil_coalescing
  - redundant_objc_attribute
  - redundant_set_access_control
  - redundant_type_annotation
  - redundant_void_return
  - unused_import
  - unused_declaration
  - unused_closure_parameter
  - unused_enumerated
  - for_where
  - implicit_return
  - implicit_getter
  - is_disjoint
  - mark
  - no_fallthrough_only
  - no_space_in_method_call
  - notification_center_detachment
  - overridden_super_call
  - prefer_self_in_static_references
  - prefer_self_type_over_type_of_self
  - private_action
  - private_outlet
  - protocol_property_accessors_order
  - trailing_closure
  - unneeded_parentheses_in_closure_argument
  - unowned_variable_capture
  - untyped_error_in_catch
  - weak_delegate
  - xct_specific_matcher

  # Performance and Safety
  - class_delegate_protocol
  - discarded_notification_center_observer
  - duplicate_enum_cases
  - duplicate_imports
  - dynamic_inline
  - empty_enum_arguments
  - explicit_init
  - fallthrough
  - fatal_error_message
  - first_where
  - force_cast
  - force_try
  - force_unwrapping
  - function_default_parameter_at_end
  - identical_operands
  - last_where
  - legacy_cggeometry_functions
  - legacy_constant
  - legacy_constructor
  - legacy_hashing
  - legacy_nsgeometry_functions
  - legacy_random
  - multiple_closures_with_trailing_closure
  - no_extension_access_modifier
  - nsobject_prefer_isequal
  - optional_enum_case_matching
  - orphaned_doc_comment
  - prefer_nimble
  - prefer_zero_over_explicit_init
  - prohibited_interface_builder
  - prohibited_super_call
  - quick_discouraged_call
  - quick_discouraged_focused_test
  - quick_discouraged_pending_test
  - reduce_boolean
  - reduce_into
  - sorted_first_last
  - static_operator
  - strict_fileprivate
  - switch_case_alignment
  - toggle_bool
  - unavailable_function
  - unhandled_throwing_task
  - void_function_in_ternary
  - yoda_condition

  # Modern Swift
  - array_init
  - block_based_kvo
  - compiler_protocol_init
  - contains_over_filter_count
  - contains_over_filter_is_empty
  - contains_over_first_not_nil
  - contains_over_range_nil_comparison
  - deployment_target
  - discouraged_assert
  - discouraged_direct_init
  - discouraged_none_name
  - discouraged_object_literal
  - discouraged_optional_boolean
  - discouraged_optional_collection
  - empty_parentheses_with_trailing_closure
  - file_name_no_space
  - flatmap_over_map_reduce
  - ibinspectable_in_extension
  - joined_default_parameter
  - legacy_multiple
  - legacy_objc_type
  - lower_acl_than_parent
  - modifier_order
  - ns_number_init_as_function_reference
  - object_literal
  - pattern_matching_keywords
  - prefer_type_checking
  - self_in_property_initialization
  - shorthand_optional_binding
  - superfluous_disable_command
  - type_body_length
  - type_contents_order
  - unavailable_condition

# Rule Configuration
indentation_width:
  indentation_width: 4
  include_comments: false

identifier_name:
  min_length:
    warning: 2
    error: 1
  max_length:
    warning: 50
    error: 60
  excluded:
    - id
    - x
    - y
    - z
    - i
    - j
    - k

type_name:
  min_length:
    warning: 3
    error: 2
  max_length:
    warning: 50
    error: 60

function_parameter_count:
  warning: 5
  error: 8

type_body_length:
  warning: 400
  error: 500

excluded:
  - .build
  - .swiftpm
  - DerivedData
  - "**/Generated/*"

reporter: "xcode"

Are you using nested configurations? Nope

Activity

  1. added
    enhancement
    on Jul 7, 2026
  2. hiSandog commented on Jul 7, 2026

    @hiSandog

    This seems like a legitimate exception for legacy_objc_type: some Apple APIs still require an Objective-C class such as NSString.self for NSItemProvider. The rule could avoid triggering when the type is passed to APIs constrained to NSObjectProtocol/NSItemProviderReading, or at least document a narrow suppression pattern. A regression test with loadObject(ofClass: NSString.self) would capture the common case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions