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

Swift type checking using is #5561

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

ikelax
Copy link

@ikelax ikelax commented May 5, 2024

Addresses #5295.

This new rule triggers on code like

if a as? Dog != nil { 
   doSomeThing()
}

which can be replaced by

if a is Dog  { 
   doSomeThing()
}

or

if let a = a as? Dog  { 
   a.run()
}

However, so far there is not a Rewriter for fixing this automatically. If I am not mistaken, a as? Dog != nil can always be replaced by a is Dog.

@SwiftLintBot
Copy link

SwiftLintBot commented May 5, 2024

10 Warnings
⚠️ This PR introduced a violation in Firefox: /firefox-ios/Client/Frontend/Browser/TabDisplayManager.swift:459:46: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in Firefox: /firefox-ios/Client/Frontend/Home/HomepageContextMenuHelper.swift:268:24: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in Firefox: /firefox-ios/Client/Frontend/Home/HomepageViewController.swift:320:36: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in Firefox: /firefox-ios/Client/Frontend/Home/TopSites/DataManagement/TopSitesDataAdaptor.swift:183:19: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in Firefox: /firefox-ios/Client/Frontend/Library/HistoryPanel/HistoryPanel.swift:548:17: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in Firefox: /firefox-ios/Client/Frontend/Widgets/SiteTableViewController.swift:54:17: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in Nimble: /Sources/Nimble/Matchers/MatchError.swift:62:24: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in Sourcery: /SourceryTests/Stub/Performance-Code/Kiosk/App/AppViewController.swift:79:36: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in WordPress: /WordPress/Classes/ViewRelated/Blog/My Site/MySiteViewController.swift:788:52: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
⚠️ This PR introduced a violation in WordPress: /WordPress/Classes/ViewRelated/Reader/ReaderCardsStreamViewController.swift:245:18: error: No type casting and checking for nil Violation: Type casting and then checking for nil should be avoided (no_type_casting_and_checking_for_nil)
17 Messages
📖 Linting Aerial with this PR took 0.79s vs 0.8s on main (1% faster)
📖 Linting Alamofire with this PR took 1.13s vs 1.15s on main (1% faster)
📖 Linting Brave with this PR took 6.6s vs 6.66s on main (0% faster)
📖 Linting DuckDuckGo with this PR took 3.52s vs 3.47s on main (1% slower)
📖 Linting Firefox with this PR took 9.49s vs 9.37s on main (1% slower)
📖 Linting Kickstarter with this PR took 8.72s vs 8.46s on main (3% slower)
📖 Linting Moya with this PR took 0.49s vs 0.48s on main (2% slower)
📖 Linting NetNewsWire with this PR took 2.45s vs 2.37s on main (3% slower)
📖 Linting Nimble with this PR took 0.69s vs 0.68s on main (1% slower)
📖 Linting PocketCasts with this PR took 6.97s vs 7.08s on main (1% faster)
📖 Linting Quick with this PR took 0.33s vs 0.33s on main (0% slower)
📖 Linting Realm with this PR took 4.29s vs 4.23s on main (1% slower)
📖 Linting Sourcery with this PR took 2.12s vs 2.09s on main (1% slower)
📖 Linting Swift with this PR took 3.91s vs 3.97s on main (1% faster)
📖 Linting VLC with this PR took 1.12s vs 1.1s on main (1% slower)
📖 Linting Wire with this PR took 14.68s vs 14.54s on main (0% slower)
📖 Linting WordPress with this PR took 9.72s vs 9.86s on main (1% faster)

Generated by 🚫 Danger

@ikelax
Copy link
Author

ikelax commented May 29, 2024

I added a Rewriter for fixing this automatically. Also, I checked the findings of SwiftLintBot. As far as I can tell, there is not anything that speaks against this rule.

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.

None yet

2 participants