Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedundantTypeAnnotation: add 'ignore_type_interfaces' option #5839

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jaredgrubb
Copy link

The redundant_type_annotation and explicit_type_interface rules conflict. For users that want to have an explicit type interface, but still be able to write simple code inside the bodies of functions, I want to add an option to redundant_type_annotation that has it ignore "type interfaces".

For example, this allows:

struct Foo {
    var bar: Bar = Bar() // OK: ignore this! I want explicit types on interfaces.
    func baz() {
       let bar: Bar = Bar() // WARN: redundant_type_annotation kicks in here though.
    }
}

I suggest that this addresses the complaint made in Issue #3750.

The `redundant_type_annotation` and `explicit_type_interface` rules conflict.
For users that want to have an explicit type interface, but still be able to write
simple code inside the bodies of functions, I want to add an option to
`redundant_type_annotation` that has it ignore "type interfaces".

For example, this allows:
```
struct Foo {
    var bar: Bar = Bar() // OK: ignore this! I want explicit types on interfaces.
    func baz() {
       let bar: Bar = Bar() // WARN: redundant_type_annotation kicks in here though.
    }
}
```
@SwiftLintBot
Copy link

17 Messages
📖 Linting Aerial with this PR took 0.92s vs 0.93s on main (1% faster)
📖 Linting Alamofire with this PR took 1.26s vs 1.26s on main (0% slower)
📖 Linting Brave with this PR took 7.2s vs 7.18s on main (0% slower)
📖 Linting DuckDuckGo with this PR took 5.08s vs 5.11s on main (0% faster)
📖 Linting Firefox with this PR took 10.64s vs 10.65s on main (0% faster)
📖 Linting Kickstarter with this PR took 9.84s vs 9.83s on main (0% slower)
📖 Linting Moya with this PR took 0.55s vs 0.53s on main (3% slower)
📖 Linting NetNewsWire with this PR took 2.63s vs 2.62s on main (0% slower)
📖 Linting Nimble with this PR took 0.77s vs 0.77s on main (0% slower)
📖 Linting PocketCasts with this PR took 8.54s vs 8.54s on main (0% slower)
📖 Linting Quick with this PR took 0.48s vs 0.44s on main (9% slower)
📖 Linting Realm with this PR took 4.53s vs 4.5s on main (0% slower)
📖 Linting Sourcery with this PR took 2.31s vs 2.31s on main (0% slower)
📖 Linting Swift with this PR took 4.49s vs 4.48s on main (0% slower)
📖 Linting VLC with this PR took 1.26s vs 1.25s on main (0% slower)
📖 Linting Wire with this PR took 17.57s vs 17.47s on main (0% slower)
📖 Linting WordPress with this PR took 11.55s vs 11.47s on main (0% slower)

Generated by 🚫 Danger

Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new option makes sense to me. Thanks for adding it!

@@ -8,6 +8,8 @@ struct RedundantTypeAnnotationConfiguration: SeverityBasedRuleConfiguration {
var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "ignore_attributes")
var ignoreAttributes = Set<String>(["IBInspectable"])
@ConfigurationElement(key: "ignore_type_interfaces")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which other elements apart from properties does this include?

If it's only about properties, we could be more specific with the name by calling it ignore_properties.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it is limited only to that. I was trying to echo the other rule (explicit_type_interfaces) by the name, but your suggestion is probably more accurate. I can change it!

Comment on lines +283 to +288
if ignoreTypeInterfaces,
let parentNode = varDecl.parent,
parentNode.as(CodeBlockItemSyntax.self) == nil {
return true
}
return false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ignoreTypeInterfaces,
let parentNode = varDecl.parent,
parentNode.as(CodeBlockItemSyntax.self) == nil {
return true
}
return false
return ignoreTypeInterfaces && varDecl.parent?.is(MemberBlockItemSyntax.self) == true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants