SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

RedundantTypeAnnotationRule: Rewrite with SwiftSyntax and allow ignoring certain attributes

Open tonell-m opened this issue 1 year ago • 6 comments

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!

tonell-m avatar Dec 11 '23 21:12 tonell-m

19 Warnings
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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)
:warning: 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
:book: Linting Aerial with this PR took 0.8s vs 0.82s on main (2% faster)
:book: Linting Alamofire with this PR took 1.13s vs 1.14s on main (0% faster)
:book: Linting Brave with this PR took 6.5s vs 6.52s on main (0% faster)
:book: Linting DuckDuckGo with this PR took 3.68s vs 3.63s on main (1% slower)
:book: Linting Firefox with this PR took 9.41s vs 9.37s on main (0% slower)
:book: Linting Kickstarter with this PR took 7.95s vs 8.42s on main (5% faster)
:book: Linting Moya with this PR took 0.49s vs 0.48s on main (2% slower)
:book: Linting NetNewsWire with this PR took 2.41s vs 2.47s on main (2% faster)
:book: Linting Nimble with this PR took 0.66s vs 0.66s on main (0% slower)
:book: Linting PocketCasts with this PR took 6.92s vs 7.01s on main (1% faster)
:book: Linting Quick with this PR took 0.32s vs 0.33s on main (3% faster)
:book: Linting Realm with this PR took 4.31s vs 4.16s on main (3% slower)
:book: Linting Sourcery with this PR took 2.07s vs 2.05s on main (0% slower)
:book: Linting Swift with this PR took 3.91s vs 3.94s on main (0% faster)
:book: Linting VLC with this PR took 1.1s vs 1.1s on main (0% slower)
:book: Linting Wire with this PR took 14.59s vs 14.36s on main (1% slower)
:book: Linting WordPress with this PR took 9.5s vs 9.45s on main (0% slower)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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)
:book: 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 :no_entry_sign: Danger

SwiftLintBot avatar Dec 11 '23 21:12 SwiftLintBot

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.

garricn avatar Dec 16 '23 06:12 garricn

@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 avatar Dec 17 '23 12:12 tonell-m

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 avatar Jan 03 '24 21:01 SimplyDanny

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

tonell-m avatar Jan 05 '24 11:01 tonell-m

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 avatar Jan 05 '24 22:01 SimplyDanny

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

Rebased and addressed this point.

SimplyDanny avatar Apr 21 '24 15:04 SimplyDanny