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

Dont trigger for non-optional type casting, or for optional types #5805

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

Conversation

mildm8nnered
Copy link
Collaborator

@mildm8nnered mildm8nnered commented Sep 21, 2024

Addresses #5802

We no longer trigger unless the type casting is optional (as?), or if the type is optional.

Essentially, in either case, the correction would change the resulting true or false value. See #5802 for details.

We also now check for nil != foo as? Bar cases

@SwiftLintBot
Copy link

SwiftLintBot commented Sep 21, 2024

1 Warning
⚠️ This PR introduced a violation in Realm: /Realm/ObjectServerTests/ClientResetTests.swift:284:36: warning: Prefer Type Checking Violation: Prefer a is X to a as? X != nil (prefer_type_checking)
17 Messages
📖 Linting Aerial with this PR took 0.18s vs 0.2s on main (10% faster)
📖 Linting Alamofire with this PR took 0.17s vs 0.18s on main (5% faster)
📖 Linting Brave with this PR took 0.73s vs 0.76s on main (3% faster)
📖 Linting DuckDuckGo with this PR took 0.54s vs 0.54s on main (0% slower)
📖 Linting Firefox with this PR took 1.49s vs 1.46s on main (2% slower)
📖 Linting Kickstarter with this PR took 0.86s vs 0.85s on main (1% slower)
📖 Linting Moya with this PR took 0.13s vs 0.13s on main (0% slower)
📖 Linting NetNewsWire with this PR took 0.34s vs 0.34s on main (0% slower)
📖 Linting Nimble with this PR took 0.14s vs 0.14s on main (0% slower)
📖 Linting PocketCasts with this PR took 0.82s vs 0.84s on main (2% faster)
📖 Linting Quick with this PR took 0.12s vs 0.12s on main (0% slower)
📖 Linting Realm with this PR took 0.45s vs 0.45s on main (0% slower)
📖 Linting Sourcery with this PR took 0.3s vs 0.31s on main (3% faster)
📖 Linting Swift with this PR took 0.49s vs 0.5s on main (2% faster)
📖 Linting VLC with this PR took 0.22s vs 0.22s on main (0% slower)
📖 Linting Wire with this PR took 1.79s vs 1.81s on main (1% faster)
📖 Linting WordPress with this PR took 1.24s vs 1.26s on main (1% faster)

Generated by 🚫 Danger

@mildm8nnered mildm8nnered force-pushed the mildm8nnered-prefer-type-checking-fix branch from 2020e54 to 4999b47 Compare September 21, 2024 18:47
@mildm8nnered mildm8nnered marked this pull request as ready for review September 22, 2024 01:47
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-prefer-type-checking-fix branch from 4999b47 to efa6b93 Compare September 29, 2024 22:16
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-prefer-type-checking-fix branch from efa6b93 to 1b33458 Compare October 5, 2024 17:23
@SimplyDanny SimplyDanny linked an issue Oct 7, 2024 that may be closed by this pull request
2 tasks
@@ -24,6 +24,9 @@ struct PreferTypeCheckingRule: Rule {
foo.run()
}
"""),
Example("bar as Foo? != nil"),
Example("bar as Foo? != nil"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line is a duplicate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤦

@@ -24,6 +24,9 @@ struct PreferTypeCheckingRule: Rule {
foo.run()
}
"""),
Example("bar as Foo? != nil"),
Example("bar as Foo? != nil"),
Example("bar as? Foo? != nil"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could actually be written as bar is Foo, couldn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that depends on whether bar is nil or not:

var bar: String? = nil
print(bar as? String? != nil)
print(bar is String?)
print(bar is String)

prints

true
true
false

but

var bar: String? = "foo"
print(bar as? String? != nil)
print(bar is String?)
print(bar is String)

prints

true
true
true

var asExprWithOptionalTypeChecking: AsExprSyntax? {
if let asExpr = leftOperand.as(AsExprSyntax.self),
asExpr.questionOrExclamationMark?.tokenKind == .postfixQuestionMark,
asExpr.type.is(OptionalTypeSyntax.self) == 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
asExpr.type.is(OptionalTypeSyntax.self) == false,
!asExpr.type.is(OptionalTypeSyntax.self),

Copy link
Collaborator

Choose a reason for hiding this comment

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

This rule doesn't consider that != is commutative. So cases like nil != bar as? Int are not found.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is true - I'm not sure how common that is, but it should be pretty easy to cover

@mildm8nnered mildm8nnered force-pushed the mildm8nnered-prefer-type-checking-fix branch from 63e1ee5 to bddd8c2 Compare October 8, 2024 18:03
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.

prefer_type_checking and optional types violation
4 participants