Skip to content

redundant_type_annotation: flag when RHS is already typed #6949

Description

@cursor

Motivation

Opt-in redundant_type_annotation flags cases where the type name is restated on the RHS, e.g.:

var url: URL = URL()
let alphanumerics: CharacterSet = CharacterSet.alphanumerics

It does not flag an annotation that is redundant because the RHS is already a typed value/property of that type:

enum Metrics {
  static let avatarLabelSpacing: Double = 6
}

struct AvatarLabelStyle {
  var spacing: Double = Metrics.avatarLabelSpacing // annotation is unnecessary
}

Verified with SwiftLint 0.65.0 (only_rules: [redundant_type_annotation]): zero violations for the example above. consider_default_literal_types_redundant only adds literal cases like : Double = 6, not property references.

Feature request

Please extend redundant_type_annotation so it also treats an explicit type as redundant when the RHS expression’s type is unambiguously the same as the annotation—at least for simple cases like a reference to a static let / let / var of that type.

Example desired violation / correction:

- var spacing: Double = Metrics.avatarLabelSpacing
+ var spacing = Metrics.avatarLabelSpacing

Happy to discuss scope (e.g. identifier / member-access RHS only; skip cases that would change overload resolution). Related discussion also opened for SwiftFormat’s propertyTypes / redundantType: nicklockwood/SwiftFormat#2752

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions