Skip to content

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

Merged
grynspan merged 1 commit into
release/6.1from
jgrynspan/suppress-non-optional-warning-sometimes-6.1
Feb 11, 2025
Merged

[6.1] Suppress warning about #require(nonOptional) in some cases.#949
grynspan merged 1 commit into
release/6.1from
jgrynspan/suppress-non-optional-warning-sometimes-6.1

Conversation

@grynspan

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

@swift-ci test

@grynspan grynspan merged commit 0e6ae14 into release/6.1 Feb 11, 2025
@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