From bb01d4dca34a230a6a1b43e373e6772360d6f333 Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Tue, 23 Apr 2024 18:01:41 -0700 Subject: [PATCH 01/10] Add `consider_default_literal_types_redundant` opt --- CHANGELOG.md | 6 ++ .../RedundantTypeAnnotationRule.swift | 95 +++++++++++++------ ...RedundantTypeAnnotationConfiguration.swift | 2 + .../default_rule_configurations.yml | 1 + 4 files changed, 75 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed5a4a1b3c..672f8b80f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,12 @@ [Martin Redington](https://github.com/mildm8nnered) [#4792](https://github.com/realm/SwiftLint/issues/4792) +* Add `consider_default_literal_types_redundant` option to + `RedundantTypeAnnotationRule` supporting `Bool`, `Double`, `Int` and `String`. + Note that this option must be set to `true` to maintain the previous behavior. + [Garric Nahapetian](https://github.com/garricn) + [#4756](https://github.com/realm/SwiftLint/pull/4756) + #### Experimental * Add two new options to the `lint` and `analyze` commands: `--write-baseline` diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift index a75b3be334..2dea92dbcf 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift @@ -52,7 +52,11 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule { @IgnoreMe let i: Int = Int(1) return i } - """, configuration: ["ignore_attributes": ["IgnoreMe"]]) + """, configuration: ["ignore_attributes": ["IgnoreMe"]]), + Example("var bol: Bool = true"), + Example("var dbl: Double = 0.0"), + Example("var int: Int = 0"), + Example("var str: String = \"str\"") ], triggeringExamples: [ Example("var url↓:URL=URL()"), @@ -84,7 +88,6 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule { } } """), - Example("var isEnabled↓: Bool = true"), Example("let a↓: [Int] = [Int]()"), Example("let a↓: A.B = A.B()"), Example(""" @@ -102,7 +105,11 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule { let i↓: Int = Int(1) return i } - """, configuration: ["ignore_attributes": ["IgnoreMe"]]) + """, configuration: ["ignore_attributes": ["IgnoreMe"]]), + Example("var bol↓: Bool = true", configuration: ["consider_default_literal_types_redundant": true]), + Example("var dbl↓: Double = 0.0", configuration: ["consider_default_literal_types_redundant": true]), + Example("var int↓: Int = 0", configuration: ["consider_default_literal_types_redundant": true]), + Example("var str↓: String = \"str\"", configuration: ["consider_default_literal_types_redundant": true]) ], corrections: [ Example("var url↓: URL = URL()"): Example("var url = URL()"), @@ -159,7 +166,15 @@ struct RedundantTypeAnnotationRule: OptInRule, SwiftSyntaxCorrectableRule { let i = Int(1) return i } - """) + """), + Example("var bol: Bool = true", configuration: ["consider_default_literal_types_redundant": true]): + Example("var bol = true"), + Example("var dbl: Double = 0.0", configuration: ["consider_default_literal_types_redundant": true]): + Example("var dbl = 0.0"), + Example("var int: Int = 0", configuration: ["consider_default_literal_types_redundant": true]): + Example("var int = 0"), + Example("var str: String = \"str\"", configuration: ["consider_default_literal_types_redundant": true]): + Example("var str = \"str\"") ] ) } @@ -168,47 +183,69 @@ private extension RedundantTypeAnnotationRule { final class Visitor: ViolationsSyntaxVisitor { override func visitPost(_ node: PatternBindingSyntax) { guard let varDecl = node.parent?.parent?.as(VariableDeclSyntax.self), - configuration.ignoreAttributes.allSatisfy({ !varDecl.attributes.contains(attributeNamed: $0) }) else { + configuration.ignoreAttributes.allSatisfy({ !varDecl.attributes.contains(attributeNamed: $0) }), + let typeAnnotation = node.typeAnnotation, + let initializer = node.initializer?.value else { return } - if let typeAnnotation = node.typeAnnotation, - let initializer = node.initializer?.value, - typeAnnotation.isRedundant(with: initializer) { - violations.append(typeAnnotation.positionAfterSkippingLeadingTrivia) - violationCorrections.append(ViolationCorrection( - start: typeAnnotation.position, - end: typeAnnotation.endPositionBeforeTrailingTrivia, - replacement: "" - )) + let isRedundant: Bool = typeAnnotation.isRedundant( + with: initializer, + considerLiteralsRedundant: configuration.considerDefaultLiteralTypesRedundant + ) + guard isRedundant else { + return } + violations.append(typeAnnotation.positionAfterSkippingLeadingTrivia) + violationCorrections.append(ViolationCorrection( + start: typeAnnotation.position, + end: typeAnnotation.endPositionBeforeTrailingTrivia, + replacement: "" + )) } override func visitPost(_ node: OptionalBindingConditionSyntax) { - if let typeAnnotation = node.typeAnnotation, - let initializer = node.initializer?.value, - typeAnnotation.isRedundant(with: initializer) { - violations.append(typeAnnotation.positionAfterSkippingLeadingTrivia) - violationCorrections.append(ViolationCorrection( - start: typeAnnotation.position, - end: typeAnnotation.endPositionBeforeTrailingTrivia, - replacement: "" - )) + guard let typeAnnotation = node.typeAnnotation, + let initializer = node.initializer?.value else { + return } + let isRedundant: Bool = typeAnnotation.isRedundant( + with: initializer, + considerLiteralsRedundant: configuration.considerDefaultLiteralTypesRedundant + ) + guard isRedundant else { + return + } + violations.append(typeAnnotation.positionAfterSkippingLeadingTrivia) + violationCorrections.append(ViolationCorrection( + start: typeAnnotation.position, + end: typeAnnotation.endPositionBeforeTrailingTrivia, + replacement: "" + )) } } } private extension TypeAnnotationSyntax { - func isRedundant(with initializerExpr: ExprSyntax) -> Bool { + func isRedundant(with initializerExpr: ExprSyntax, considerLiteralsRedundant: Bool) -> Bool { var initializer = initializerExpr if let forceUnwrap = initializer.as(ForceUnwrapExprSyntax.self) { initializer = forceUnwrap.expression } - - // If the initializer is a boolean expression, we consider using the `Bool` type - // annotation as redundant. - if initializer.is(BooleanLiteralExprSyntax.self) { - return type.trimmedDescription == "Bool" + // Consider the type annotation redundant if `considerLiteralsRedundant` is true and the expression is one of + // the supported compiler-inferred literals. + if considerLiteralsRedundant { + return switch initializer { + case let expr where expr.is(BooleanLiteralExprSyntax.self): + type.trimmedDescription == "Bool" + case let expr where expr.is(FloatLiteralExprSyntax.self): + type.trimmedDescription == "Double" + case let expr where expr.is(IntegerLiteralExprSyntax.self): + type.trimmedDescription == "Int" + case let expr where expr.is(StringLiteralExprSyntax.self): + type.trimmedDescription == "String" + default: + false + } } return initializer.accessedNames.contains(type.trimmedDescription) } diff --git a/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/RedundantTypeAnnotationConfiguration.swift b/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/RedundantTypeAnnotationConfiguration.swift index 01c22d6005..fa837304f5 100644 --- a/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/RedundantTypeAnnotationConfiguration.swift +++ b/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/RedundantTypeAnnotationConfiguration.swift @@ -8,4 +8,6 @@ struct RedundantTypeAnnotationConfiguration: SeverityBasedRuleConfiguration { var severityConfiguration = SeverityConfiguration(.warning) @ConfigurationElement(key: "ignore_attributes") var ignoreAttributes = Set(["IBInspectable"]) + @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 a8796526ee..404b9ef45d 100644 --- a/Tests/IntegrationTests/default_rule_configurations.yml +++ b/Tests/IntegrationTests/default_rule_configurations.yml @@ -457,6 +457,7 @@ redundant_string_enum_value: redundant_type_annotation: severity: warning ignore_attributes: ["IBInspectable"] + consider_default_literal_types_redundant: false redundant_void_return: severity: warning include_closures: true From 123cbeb9feeb7b97d09ca24a1ab0c6868d127caa Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Thu, 25 Apr 2024 17:05:20 -0700 Subject: [PATCH 02/10] Refactor to method --- .../RedundantTypeAnnotationRule.swift | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift index 2dea92dbcf..026c746abf 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift @@ -234,21 +234,26 @@ private extension TypeAnnotationSyntax { // Consider the type annotation redundant if `considerLiteralsRedundant` is true and the expression is one of // the supported compiler-inferred literals. if considerLiteralsRedundant { - return switch initializer { - case let expr where expr.is(BooleanLiteralExprSyntax.self): - type.trimmedDescription == "Bool" - case let expr where expr.is(FloatLiteralExprSyntax.self): - type.trimmedDescription == "Double" - case let expr where expr.is(IntegerLiteralExprSyntax.self): - type.trimmedDescription == "Int" - case let expr where expr.is(StringLiteralExprSyntax.self): - type.trimmedDescription == "String" - default: - false - } + return isCompilerInferredLiteral(expression: initializer) } return initializer.accessedNames.contains(type.trimmedDescription) } + + // Returns true if the expression is one of the supported compiler-inferred literals. + private func isCompilerInferredLiteral(expression: ExprSyntax) -> Bool { + return switch expression { + case let expr where expr.is(BooleanLiteralExprSyntax.self): + type.trimmedDescription == "Bool" + case let expr where expr.is(FloatLiteralExprSyntax.self): + type.trimmedDescription == "Double" + case let expr where expr.is(IntegerLiteralExprSyntax.self): + type.trimmedDescription == "Int" + case let expr where expr.is(StringLiteralExprSyntax.self): + type.trimmedDescription == "String" + default: + false + } + } } private extension ExprSyntax { From 761ab09271b7f05406347a17206d9f066bc7683c Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Thu, 25 Apr 2024 17:05:28 -0700 Subject: [PATCH 03/10] Update CHANGELOG --- CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 672f8b80f2..ba7c154471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,11 +21,11 @@ [Martin Redington](https://github.com/mildm8nnered) [#4792](https://github.com/realm/SwiftLint/issues/4792) -* Add `consider_default_literal_types_redundant` option to - `RedundantTypeAnnotationRule` supporting `Bool`, `Double`, `Int` and `String`. - Note that this option must be set to `true` to maintain the previous behavior. +* Add `consider_default_literal_types_redundant` option to the + `redundant_type_annotation` rule supporting `Bool`, `Double`, `Int` and + `String`. Setting the option to `true` lets the rule consider said types in + declarations like `let i: Int = 1` or `let s: String = ""` as redundant. [Garric Nahapetian](https://github.com/garricn) - [#4756](https://github.com/realm/SwiftLint/pull/4756) #### Experimental From 13b368153a89a3225ac57b891423807c7da27004 Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Fri, 26 Apr 2024 11:48:23 -0700 Subject: [PATCH 04/10] Improve implementation --- .../Rules/Idiomatic/RedundantTypeAnnotationRule.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift index 026c746abf..b28e36493d 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift @@ -234,14 +234,14 @@ private extension TypeAnnotationSyntax { // Consider the type annotation redundant if `considerLiteralsRedundant` is true and the expression is one of // the supported compiler-inferred literals. if considerLiteralsRedundant { - return isCompilerInferredLiteral(expression: initializer) + return isCompilerInferredLiteral(initializer) } return initializer.accessedNames.contains(type.trimmedDescription) } // Returns true if the expression is one of the supported compiler-inferred literals. - private func isCompilerInferredLiteral(expression: ExprSyntax) -> Bool { - return switch expression { + private func isCompilerInferredLiteral(_ expr: ExprSyntax) -> Bool { + switch expr { case let expr where expr.is(BooleanLiteralExprSyntax.self): type.trimmedDescription == "Bool" case let expr where expr.is(FloatLiteralExprSyntax.self): From c98ccb50aea7d5546deda61d2e0edda44234ba5f Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Fri, 26 Apr 2024 11:48:44 -0700 Subject: [PATCH 05/10] Improve CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba7c154471..4e0911c66d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,8 @@ * Add `consider_default_literal_types_redundant` option to the `redundant_type_annotation` rule supporting `Bool`, `Double`, `Int` and `String`. Setting the option to `true` lets the rule consider said types in - declarations like `let i: Int = 1` or `let s: String = ""` as redundant. + declarations like `let i: Int = 1` or `let s: String = ""` as redundant. + Note that `Bool` literals will no longer be considered redundant by default. [Garric Nahapetian](https://github.com/garricn) #### Experimental From 17a42f31197551607f2386b45d82e8a04f5f715a Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Fri, 26 Apr 2024 16:56:58 -0700 Subject: [PATCH 06/10] Improve implementation --- .../RedundantTypeAnnotationRule.swift | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift index b28e36493d..eeba1fefea 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift @@ -188,11 +188,9 @@ private extension RedundantTypeAnnotationRule { let initializer = node.initializer?.value else { return } - let isRedundant: Bool = typeAnnotation.isRedundant( - with: initializer, - considerLiteralsRedundant: configuration.considerDefaultLiteralTypesRedundant - ) - guard isRedundant else { + let validateLiterals = configuration.considerDefaultLiteralTypesRedundant + let isLiteralRedundant = validateLiterals && initializer.hasRedundantLiteralType(typeAnnotation.type) + guard isLiteralRedundant || initializer.hasRedundantType(typeAnnotation.type) else { return } violations.append(typeAnnotation.positionAfterSkippingLeadingTrivia) @@ -208,11 +206,9 @@ private extension RedundantTypeAnnotationRule { let initializer = node.initializer?.value else { return } - let isRedundant: Bool = typeAnnotation.isRedundant( - with: initializer, - considerLiteralsRedundant: configuration.considerDefaultLiteralTypesRedundant - ) - guard isRedundant else { + let validateLiterals = configuration.considerDefaultLiteralTypesRedundant + let isLiteralRedundant = validateLiterals && initializer.hasRedundantLiteralType(typeAnnotation.type) + guard isLiteralRedundant || initializer.hasRedundantType(typeAnnotation.type) else { return } violations.append(typeAnnotation.positionAfterSkippingLeadingTrivia) @@ -225,31 +221,26 @@ private extension RedundantTypeAnnotationRule { } } -private extension TypeAnnotationSyntax { - func isRedundant(with initializerExpr: ExprSyntax, considerLiteralsRedundant: Bool) -> Bool { - var initializer = initializerExpr - if let forceUnwrap = initializer.as(ForceUnwrapExprSyntax.self) { - initializer = forceUnwrap.expression - } - // Consider the type annotation redundant if `considerLiteralsRedundant` is true and the expression is one of - // the supported compiler-inferred literals. - if considerLiteralsRedundant { - return isCompilerInferredLiteral(initializer) +private extension SyntaxKind { + var compilerInferredLiteralType: String? { + switch self { + case .booleanLiteralExpr: + "Bool" + case .floatLiteralExpr: + "Double" + case .integerLiteralExpr: + "Int" + case .stringLiteralExpr: + "String" + default: + nil } - return initializer.accessedNames.contains(type.trimmedDescription) } - // Returns true if the expression is one of the supported compiler-inferred literals. - private func isCompilerInferredLiteral(_ expr: ExprSyntax) -> Bool { - switch expr { - case let expr where expr.is(BooleanLiteralExprSyntax.self): - type.trimmedDescription == "Bool" - case let expr where expr.is(FloatLiteralExprSyntax.self): - type.trimmedDescription == "Double" - case let expr where expr.is(IntegerLiteralExprSyntax.self): - type.trimmedDescription == "Int" - case let expr where expr.is(StringLiteralExprSyntax.self): - type.trimmedDescription == "String" + func isLiteralExpression() -> Bool { + switch self { + case .booleanLiteralExpr, .floatLiteralExpr, .integerLiteralExpr, .stringLiteralExpr: + true default: false } @@ -275,4 +266,16 @@ private extension ExprSyntax { [] } } + + func hasRedundantLiteralType(_ type: TypeSyntax) -> Bool { + kind.isLiteralExpression() && kind.compilerInferredLiteralType == type.trimmedDescription + } + + func hasRedundantType(_ type: TypeSyntax) -> Bool { + var expr = self + if let forceUnwrap = expr.as(ForceUnwrapExprSyntax.self) { + expr = forceUnwrap.expression + } + return expr.accessedNames.contains(type.trimmedDescription) + } } From 01707d713c34d16f89f07e1dfa32c304489d5ad7 Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Fri, 26 Apr 2024 17:05:14 -0700 Subject: [PATCH 07/10] Move --- .../RedundantTypeAnnotationRule.swift | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift index eeba1fefea..e9ee074c40 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift @@ -221,32 +221,6 @@ private extension RedundantTypeAnnotationRule { } } -private extension SyntaxKind { - var compilerInferredLiteralType: String? { - switch self { - case .booleanLiteralExpr: - "Bool" - case .floatLiteralExpr: - "Double" - case .integerLiteralExpr: - "Int" - case .stringLiteralExpr: - "String" - default: - nil - } - } - - func isLiteralExpression() -> Bool { - switch self { - case .booleanLiteralExpr, .floatLiteralExpr, .integerLiteralExpr, .stringLiteralExpr: - true - default: - false - } - } -} - private extension ExprSyntax { /// An expression can represent an access to an identifier in one or another way depending on the exact underlying /// expression type. E.g. the expression `A` accesses `A` while `f()` accesses `f` and `a.b.c` accesses `a` in the @@ -279,3 +253,29 @@ private extension ExprSyntax { return expr.accessedNames.contains(type.trimmedDescription) } } + +private extension SyntaxKind { + var compilerInferredLiteralType: String? { + switch self { + case .booleanLiteralExpr: + "Bool" + case .floatLiteralExpr: + "Double" + case .integerLiteralExpr: + "Int" + case .stringLiteralExpr: + "String" + default: + nil + } + } + + func isLiteralExpression() -> Bool { + switch self { + case .booleanLiteralExpr, .floatLiteralExpr, .integerLiteralExpr, .stringLiteralExpr: + true + default: + false + } + } +} From 83dc20a01a3f7665750796b58c8cedfd2fdb6659 Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Fri, 26 Apr 2024 17:09:55 -0700 Subject: [PATCH 08/10] Reorder --- .../Rules/Idiomatic/RedundantTypeAnnotationRule.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift index e9ee074c40..a642d792fb 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift @@ -242,7 +242,7 @@ private extension ExprSyntax { } func hasRedundantLiteralType(_ type: TypeSyntax) -> Bool { - kind.isLiteralExpression() && kind.compilerInferredLiteralType == type.trimmedDescription + kind.isLiteralExpression() && type.trimmedDescription == kind.compilerInferredLiteralType } func hasRedundantType(_ type: TypeSyntax) -> Bool { From efa5b170dd9ea732e6ce23d9c12d0e10e9b8b567 Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Wed, 1 May 2024 11:36:28 -0700 Subject: [PATCH 09/10] Remove method as not needed --- .../Rules/Idiomatic/RedundantTypeAnnotationRule.swift | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift index a642d792fb..5810619632 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantTypeAnnotationRule.swift @@ -242,7 +242,7 @@ private extension ExprSyntax { } func hasRedundantLiteralType(_ type: TypeSyntax) -> Bool { - kind.isLiteralExpression() && type.trimmedDescription == kind.compilerInferredLiteralType + type.trimmedDescription == kind.compilerInferredLiteralType } func hasRedundantType(_ type: TypeSyntax) -> Bool { @@ -269,13 +269,4 @@ private extension SyntaxKind { nil } } - - func isLiteralExpression() -> Bool { - switch self { - case .booleanLiteralExpr, .floatLiteralExpr, .integerLiteralExpr, .stringLiteralExpr: - true - default: - false - } - } } From c044d710e221edce5cfa5264cef258ec2cb980ea Mon Sep 17 00:00:00 2001 From: Garric Nahapetian Date: Wed, 1 May 2024 11:37:09 -0700 Subject: [PATCH 10/10] Separate CHANGELOG entry --- CHANGELOG.md | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e0911c66d..4ce64a9118 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,11 +21,10 @@ [Martin Redington](https://github.com/mildm8nnered) [#4792](https://github.com/realm/SwiftLint/issues/4792) -* Add `consider_default_literal_types_redundant` option to the - `redundant_type_annotation` rule supporting `Bool`, `Double`, `Int` and - `String`. Setting the option to `true` lets the rule consider said types in - declarations like `let i: Int = 1` or `let s: String = ""` as redundant. - Note that `Bool` literals will no longer be considered redundant by default. +* With the introduction of the `consider_default_literal_types_redundant` + option to the `redundant_type_annotation` rule, `Bool` literals will no + longer be considered redundant by default. Set this option to true to + preserve the previous behavior. [Garric Nahapetian](https://github.com/garricn) #### Experimental @@ -195,6 +194,14 @@ [Martin Redington](https://github.com/mildm8nnered) [#5470](https://github.com/realm/SwiftLint/issues/5470) +* Include `Double`, `Int` and `String` to the exiting redundant type validation + check of `Bool` in the `redundant_type_annotation` rule. Add + `consider_default_literal_types_redundant` option supporting `Bool`, + `Double`, `Int` and `String`. Setting this option to `true` lets the rule + consider said types in declarations like `let i: Int = 1` or + `let s: String = ""` as redundant. + [Garric Nahapetian](https://github.com/garricn) + #### Bug Fixes * Silence `discarded_notification_center_observer` rule in closures. Furthermore,