Skip to content

[6.1] Suppress warning about #require(nonOptional) in some cases. #949

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

Merged

Conversation

grynspan
Copy link
Contributor

When using `try #require()` to unwrap an optional value, we emit a
compile-time warning if the value is not actually optional. However,
there is a bug in the type checker
(swiftlang/swift#79202) that triggers a false
positive when downcasting an object (of `class` type), e.g.:

```swift
class Animal {}
class Duck: Animal {}
let beast: Animal = Duck()
let definitelyADuck = try #require(beast as? Duck)
// ⚠️ '#require(_:_:)' is redundant because 'beast as? Duck' never equals 'nil'
```

This change suppresses the warning we emit if the expression contains
certain syntax tokens (namely `?`, `nil`, or `Optional`) on the
assumption that their presence means the test author is expecting an
optional value and we've hit a false positive.

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
@grynspan grynspan added bug 🪲 Something isn't working workaround Workaround for an issue in another component (may need to revert later) issue-handling Related to Issue handling within the testing library labels Feb 11, 2025
@grynspan grynspan added this to the Swift 6.1 milestone Feb 11, 2025
@grynspan grynspan self-assigned this Feb 11, 2025
@grynspan grynspan changed the base branch from main to release/6.1 February 11, 2025 18:05
@grynspan grynspan changed the title Jgrynspan/suppress non optional warning sometimes 6.1 [6.1] Suppress warning about #require(nonOptional) in some cases. Feb 11, 2025
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan merged commit 0e6ae14 into release/6.1 Feb 11, 2025
3 checks passed
@grynspan grynspan deleted the jgrynspan/suppress-non-optional-warning-sometimes-6.1 branch February 11, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🪲 Something isn't working issue-handling Related to Issue handling within the testing library workaround Workaround for an issue in another component (may need to revert later)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants