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

prefer_type_checking and optional types violation #5802

Closed
2 tasks done
cyberowl opened this issue Sep 20, 2024 · 2 comments · Fixed by #5805
Closed
2 tasks done

prefer_type_checking and optional types violation #5802

cyberowl opened this issue Sep 20, 2024 · 2 comments · Fixed by #5805
Assignees
Labels
bug Unexpected and reproducible misbehavior.

Comments

@cyberowl
Copy link

cyberowl commented Sep 20, 2024

New Issue Checklist

Bug Description

The following code causes false positive prefer_type_checking violation:

let value: String? = nil
print(value as Any? != nil) // -> false

Let's try fix it:

swiftlint . --fix

Now we got this:

let value: String? = nil
// xcodebuild warning: `'is' test is always true`
print(value is Any?) // -> now true, that's not expected

Environment

  • SwiftLint version: 0.57.0
  • Xcode version: 16.0 (16A242d)
  • Installation method used: Homebrew
  • Configuration file: None
@mildm8nnered
Copy link
Collaborator

At first I thought we should just ignore SomeType? completely, but actually I wonder if we should be triggering on as at all, as opposed to as??

That would fix this case.

someVar as? SomeType? != nil would still get caught, but we could ignore SomeType? as well

@mildm8nnered mildm8nnered added the bug Unexpected and reproducible misbehavior. label Sep 20, 2024
@mildm8nnered
Copy link
Collaborator

So I think this case will also be problematic

let x: String? = nil
print(x as String? != nil) // false
print(x is String?) // true

So I think we want to exclude the OptionalTypeSyntax cases as well

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

Successfully merging a pull request may close this issue.

2 participants