Skip to content

Incorrect warning with #require in optional assignment #80633

Closed as duplicate of#79202
@macguru

Description

@macguru

Description

When using try #require inside an optional assignment, an incorrect warning ist being produced:

'#require(_:_:)' is redundant because 'x' never equals 'nil' (from macro 'require')

Reproduction

import Testing

struct A {
	var value: Int?
}

@Test func run() throws {
	let one = A()
	var two = A()
	
	two.value = try #require(one.value)
	// warning: '#require(_:_:)' is redundant because 'one.value' never equals 'nil' (from macro 'require')
}

Expected behavior

There should be no warning, the code is correct, as one.value can indeed become nil. Changing the code as follows does not have the warning:

@Test func run() throws {
	let one = A()
	let two = try #require(one.value)
}

Environment

Swift 6.1, Xcode Version 16.3 (16E140)

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

This was not an issue with Swift 6.0 and previous Xcode releases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions