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

Incorrect warning: '#require(_:_:)' is redundant because '<expression>' never equals 'nil' #79202

Open
mhrawdon opened this issue Feb 6, 2025 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels type checker Area → compiler: Semantic analysis

Comments

@mhrawdon
Copy link

mhrawdon commented Feb 6, 2025

Description

In a Swift Testing test, use of #require can in some cases result in a build warning about how the expression in the macro can never equal nil, even though it clearly can.

For example, in swift-build, some tests have checks like this:

if let fileRef = try? #require(fileGroup.children[1] as? FileReference) {

This results in the warning:

'#require(_:_:)' is redundant because 'fileGroup.children[1] as? FileReference' never equals 'nil' (from macro 'require')

But fileGroup.children is of type [GroupTreeReference], so the check is downcasting it to FileReference, which seems entirely valid to do, and could end up being nil if the instance is not a FileReference. It works correctly, so the warning seems incorrect.

#require is being used here because it will emit an issue automatically if the expression is false or nil, which is the desired behavior. This could be worked around by not using #require and instead handling that case with explicit code, but that seems unnecessary and contrary to the intent of #require.

Reproduction

See Description.

Expected behavior

This warning should not be emitted in cases such as the one in the Description.

Environment

Swift 6

Additional information

No response

@mhrawdon mhrawdon added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels type checker Area → compiler: Semantic analysis labels Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

1 participant