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

RedundantTypeAnnotationRule: Rewrite with SwiftSyntax and allow ignoring certain attributes #5389

Conversation

tonell-m
Copy link
Contributor

Hi, first time contributing here so just let me know if something is wrong or missing in my PR 🙂

This PR resolves #5366 by adding a new ignored_annotations configuration to this rule that accepts a set of strings listing annotations that should disable this rule for properties that are marked with it. Currently this is already done arbitrarily for @IBInspectable but now it is configurable. To solve the linked issue the configuration would be:

redundant_type_annotations:
  ignored_attributes:
    - Parameter

This configuration still defaults to only IBDesignable for retrocompatibility.

And, while I was at it and as @SimplyDanny suggested in the issue's comments, I took the opportunity to rewrite this rule using SwiftSyntax. Let me know what you think!

@SwiftLintBot
Copy link

SwiftLintBot commented Dec 11, 2023

19 Warnings
⚠️ This PR introduced a violation in Brave: /Sources/Growth/GrowthPreferences.swift:15:30: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Firefox: /firefox-ios/Client/Frontend/Widgets/SiteTableViewController.swift:30:13: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Firefox: /firefox-ios/Storage/ThirdParty/SwiftData.swift:328:19: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Kickstarter: /Kickstarter-iOS/Features/ProjectPage/Controller/ProjectPageViewControllerTests.swift:1057:26: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Kickstarter: /Library/ViewModels/ProjectNavigationSelectorViewModelTests.swift:33:64: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in NetNewsWire: /Account/Sources/Account/Folder.swift:24:26: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Realm: /RealmSwift/Tests/CustomColumnNameTests.swift:827:23: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Realm: /RealmSwift/Tests/CustomColumnNameTests.swift:865:23: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Sourcery: /SourceryTests/Stub/Performance-Code/Kiosk/App/Networking/Networking.swift:120:25: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Swift: /stdlib/private/StdlibUnittest/RaceTest.swift:439:28: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Swift: /stdlib/private/StdlibUnittest/StdlibUnittest.swift:992:27: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Swift: /stdlib/private/StdlibUnittest/StdlibUnittest.swift:994:28: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Swift: /stdlib/private/StdlibUnittest/StdlibUnittest.swift:996:28: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Swift: /stdlib/private/SwiftPrivate/IO.swift:38:23: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Swift: /stdlib/public/Backtracing/Context.swift:981:12: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Swift: /stdlib/public/core/ThreadLocalStorage.swift:87:13: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Swift: /stdlib/public/libexec/swift-backtrace/TargetMacOS.swift:417:12: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in VLC: /Sources/Playback/Player/VideoPlayer-iOS/VideoPlayerViewController.swift:2218:33: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
⚠️ This PR introduced a violation in Wire: /wire-ios/Wire-iOS/Sources/Helpers/ColorScheme.swift:245:25: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
34 Messages
📖 Linting Aerial with this PR took 0.8s vs 0.82s on main (2% faster)
📖 Linting Alamofire with this PR took 1.13s vs 1.14s on main (0% faster)
📖 Linting Brave with this PR took 6.5s vs 6.52s on main (0% faster)
📖 Linting DuckDuckGo with this PR took 3.68s vs 3.63s on main (1% slower)
📖 Linting Firefox with this PR took 9.41s vs 9.37s on main (0% slower)
📖 Linting Kickstarter with this PR took 7.95s vs 8.42s on main (5% faster)
📖 Linting Moya with this PR took 0.49s vs 0.48s on main (2% slower)
📖 Linting NetNewsWire with this PR took 2.41s vs 2.47s on main (2% faster)
📖 Linting Nimble with this PR took 0.66s vs 0.66s on main (0% slower)
📖 Linting PocketCasts with this PR took 6.92s vs 7.01s on main (1% faster)
📖 Linting Quick with this PR took 0.32s vs 0.33s on main (3% faster)
📖 Linting Realm with this PR took 4.31s vs 4.16s on main (3% slower)
📖 Linting Sourcery with this PR took 2.07s vs 2.05s on main (0% slower)
📖 Linting Swift with this PR took 3.91s vs 3.94s on main (0% faster)
📖 Linting VLC with this PR took 1.1s vs 1.1s on main (0% slower)
📖 Linting Wire with this PR took 14.59s vs 14.36s on main (1% slower)
📖 Linting WordPress with this PR took 9.5s vs 9.45s on main (0% slower)
📖 This PR fixed a violation in Aerial: /Aerial/Source/Models/Cache/VideoDownload.swift:239:25: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Aerial: /Resources/MainUI/VideosViewController.swift:224:20: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Brave: /Sources/Brave/Frontend/Browser/Tabs/TabTray/Views/TabTrayCell.swift:33:14: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Brave: /Sources/Brave/Frontend/Settings/Debug/Rewards Internals/QA/RewardsDebugSettingsViewController.swift:127:17: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Brave: /Sources/BraveWallet/Chart/LineChartView.swift:58:12: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in DuckDuckGo: /PacketTunnelProvider/ProxyServer/Utils/UInt128.swift:453:28: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Swift: /stdlib/public/Windows/WinSDK.swift:186:15: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Wire: /wire-ios-data-model/Source/Utilis/CryptoBox.swift:105:34: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Wire: /wire-ios-sync-engine/Source/Data Model/Conversation+MessageDestructionTimeout.swift:94:28: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Wire: /wire-ios/Wire-iOS/Sources/UserInterface/CustomAppLock/Setup/PasscodeError.swift:53:29: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Wire: /wire-ios/Wire-iOS/Sources/UserInterface/Helpers/UIView+Zeta.swift:40:27: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Wire: /wire-ios/Wire-iOS/Sources/Vendor/SCSiriWaveformView.swift:157:23: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in Wire: /wire-ios/WireCommonComponents/FileMetaDataGenerator.swift:87:18: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in WordPress: /WordPress/Classes/ViewRelated/Reader/OldReaderPostCardCell.swift:229:36: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in WordPress: /WordPress/Classes/ViewRelated/Reader/OldReaderPostCardCell.swift:230:35: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in WordPress: /WordPress/Classes/ViewRelated/Reader/ReaderSearchViewController.swift:247:24: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)
📖 This PR fixed a violation in WordPress: /WordPress/Classes/ViewRelated/Themes/ThemeBrowserViewController.swift:198:24: warning: Redundant Type Annotation Violation: Variables should not have redundant type annotation (redundant_type_annotation)

Generated by 🚫 Danger

@tonell-m tonell-m force-pushed the feature/add-attributes-ignore-config-to-redundanttypeannoations branch 2 times, most recently from 9c1904c to 72e1850 Compare December 12, 2023 16:10
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.

Thanks for taking up this rewrite! This is actually a bit more involved than adding the option for ignored attributes only. So I have a few comments ...

@garricn
Copy link
Contributor

garricn commented Dec 16, 2023

I'm really excited about this work! After this merges, perhaps we can also introduce another configuration element named ignore_literals which can address the issues raised by this PR as well as the concerns of @SimplyDanny therein in terms of not limiting the configuration element to just ignoring booleans.

@tonell-m
Copy link
Contributor Author

@SimplyDanny Thanks for the review!

After your comments I've rewrote the rule with a new approach that seems to be more flexible. Instead of visiting VariableDeclSyntax nodes I use PatternBindingSyntax and OptionalBindingConditionSyntax as entry points and moved the logic into a TypeAnnotationSyntax extension. That way it will check for all binding patterns even if there are multiple for a single var/let keyword, and it also works the same of guard/if clauses.
I've also improved the handling of generics to handle more complex cases as you suggested, so var s: Set = Set<Int>([]) will trigger and not var s: Set<Int> = Set([]). I've added a bunch of tests with various configurations to ensure that this is working as expected, let me know if you think of other cases that haven't been handled here :)

@tonell-m tonell-m force-pushed the feature/add-attributes-ignore-config-to-redundanttypeannoations branch from cd5eebf to ca246f5 Compare December 21, 2023 10:52
@SimplyDanny
Copy link
Collaborator

Looking at the OSS findings, most of them look valid.

However, there are cases where a sequence of MemberAccessExpressions leads to the final element/function. An easy example would be let a: A = A.b.c They were previously discovered by the rule, so they should also be discoverable by the rewritten rule.

Admittedly, these cases are debatable, but for the sake of a rewrite, the rule should behave as close to the previous version as it can.

@tonell-m: Would you consider these cases in your PR?

CHANGELOG.md Outdated Show resolved Hide resolved
@tonell-m
Copy link
Contributor Author

tonell-m commented Jan 5, 2024

Looking at the OSS findings, most of them look valid.

However, there are cases where a sequence of MemberAccessExpressions leads to the final element/function. An easy example would be let a: A = A.b.c They were previously discovered by the rule, so they should also be discoverable by the rewritten rule.

Admittedly, these cases are debatable, but for the sake of a rewrite, the rule should behave as close to the previous version as it can.

@tonell-m: Would you consider these cases in your PR?

@SimplyDanny This case is now handled as well. Do you think we also need to handle cases where there is a mix of MemberAccessExprSyntax and FunctionCallExprSyntax? Like let a: A = A.f().b

@SimplyDanny
Copy link
Collaborator

Looking at the OSS findings, most of them look valid.
However, there are cases where a sequence of MemberAccessExpressions leads to the final element/function. An easy example would be let a: A = A.b.c They were previously discovered by the rule, so they should also be discoverable by the rewritten rule.
Admittedly, these cases are debatable, but for the sake of a rewrite, the rule should behave as close to the previous version as it can.
@tonell-m: Would you consider these cases in your PR?

@SimplyDanny This case is now handled as well. Do you think we also need to handle cases where there is a mix of MemberAccessExprSyntax and FunctionCallExprSyntax? Like let a: A = A.f().b

That depends on what the previous implementation did. I guess, it triggered, hence the rewritten rule should trigger too.

@SimplyDanny SimplyDanny force-pushed the feature/add-attributes-ignore-config-to-redundanttypeannoations branch from 8771e14 to fec3ea9 Compare April 21, 2024 15:11
@SimplyDanny
Copy link
Collaborator

That depends on what the previous implementation did. I guess, it triggered, hence the rewritten rule should trigger too.

Rebased and addressed this point.

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