From fb3ce5adb226878de61f4754591868a71d5ffa1e Mon Sep 17 00:00:00 2001 From: Jared Grubb <1256381+jaredgrubb@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:27:59 -0800 Subject: [PATCH] Add `ignore_properties` option to `redundant_type_annotation` rule (#5839) --- CHANGELOG.md | 7 ++++++ .../RedundantTypeAnnotationRule.swift | 25 ++++++++++++++++++- ...RedundantTypeAnnotationConfiguration.swift | 2 ++ .../default_rule_configurations.yml | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfdf749b3a..3be513f1c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,13 @@ [Martin Redington](https://github.com/mildm8nnered) [#5801](https://github.com/realm/SwiftLint/issues/5801) +* The `redundant_type_annotation` rule gains a new option, + `ignore_properties`, that skips enforcement on members in a + type declaration (like a `struct`). This helps the rule coexist with + the `explicit_type_interface` rule that requires such redundancy. + [jaredgrubb](https://github.com/jaredgrubb) + [#3750](https://github.com/realm/SwiftLint/issues/3750) + #### Bug Fixes * Run command plugin in whole package if no targets are defined in the diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift index 019ff40ac0..ac7b3047bc 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift @@ -57,6 +57,12 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule { Example("var dbl: Double = 0.0"), Example("var int: Int = 0"), Example("var str: String = \"str\""), + Example(""" + struct Foo { + var url: URL = URL() + let myVar: Int? = 0, s: String = "" + } + """, configuration: ["ignore_properties": true]), ], triggeringExamples: [ Example("var url↓:URL=URL()"), @@ -88,6 +94,13 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule { } } """), + Example(""" + class ViewController: UIViewController { + func someMethod() { + let myVar↓: Int = Int(5) + } + } + """, configuration: ["ignore_properties": true]), Example("let a↓: [Int] = [Int]()"), Example("let a↓: A.B = A.B()"), Example(""" @@ -183,7 +196,7 @@ private extension RedundantTypeAnnotationRule { final class Visitor: ViolationsSyntaxVisitor { override func visitPost(_ node: PatternBindingSyntax) { if let varDecl = node.parent?.parent?.as(VariableDeclSyntax.self), - configuration.ignoreAttributes.allSatisfy({ !varDecl.attributes.contains(attributeNamed: $0) }), + !configuration.shouldSkipRuleCheck(for: varDecl), let typeAnnotation = node.typeAnnotation, let initializer = node.initializer?.value { collectViolation(forType: typeAnnotation, withInitializer: initializer) @@ -261,3 +274,13 @@ private extension SyntaxKind { } } } + +extension RedundantTypeAnnotationConfiguration { + func shouldSkipRuleCheck(for varDecl: VariableDeclSyntax) -> Bool { + if ignoreAttributes.contains(where: { varDecl.attributes.contains(attributeNamed: $0) }) { + return true + } + + return ignoreProperties && varDecl.parent?.is(MemberBlockItemSyntax.self) == true + } +} diff --git a/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/RedundantTypeAnnotationConfiguration.swift b/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/RedundantTypeAnnotationConfiguration.swift index 3eb9596298..7bb54dacfa 100644 --- a/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/RedundantTypeAnnotationConfiguration.swift +++ b/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/RedundantTypeAnnotationConfiguration.swift @@ -8,6 +8,8 @@ struct RedundantTypeAnnotationConfiguration: SeverityBasedRuleConfiguration { var severityConfiguration = SeverityConfiguration(.warning) @ConfigurationElement(key: "ignore_attributes") var ignoreAttributes = Set(["IBInspectable"]) + @ConfigurationElement(key: "ignore_properties") + private(set) var ignoreProperties = false @ConfigurationElement(key: "consider_default_literal_types_redundant") private(set) var considerDefaultLiteralTypesRedundant = false } diff --git a/Tests/IntegrationTests/default_rule_configurations.yml b/Tests/IntegrationTests/default_rule_configurations.yml index 57148068b8..f9014e5743 100644 --- a/Tests/IntegrationTests/default_rule_configurations.yml +++ b/Tests/IntegrationTests/default_rule_configurations.yml @@ -473,6 +473,7 @@ redundant_string_enum_value: redundant_type_annotation: severity: warning ignore_attributes: ["IBInspectable"] + ignore_properties: false consider_default_literal_types_redundant: false redundant_void_return: severity: warning