Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()"),
Expand Down Expand Up @@ -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("""
Expand Down Expand Up @@ -183,7 +196,7 @@ private extension RedundantTypeAnnotationRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
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)
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ struct RedundantTypeAnnotationConfiguration: SeverityBasedRuleConfiguration {
var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "ignore_attributes")
var ignoreAttributes = Set<String>(["IBInspectable"])
@ConfigurationElement(key: "ignore_properties")
private(set) var ignoreProperties = false
@ConfigurationElement(key: "consider_default_literal_types_redundant")
private(set) var considerDefaultLiteralTypesRedundant = false
}
1 change: 1 addition & 0 deletions Tests/IntegrationTests/default_rule_configurations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down