In recent Xcode 11 betas, Swift 5.1 has become more aggressive about releasing values assigned to the underscore. This means I must leave a warning in the code if I'm not using the value directly, but expecting a side effect (e.g., with Combine).
For example, the following unit test passes, but contains the warning on line 6: "Initialization of immutable value 'unusedButNeeded' was never used; consider replacing with assignment to '_' or removing it."
func testPassthroughSubjectSequence() {
let expectedResult = [1, 2, 3]
var actualResult: [Int] = []
let subject = PassthroughSubject<Int, Never>()
let unusedButNeeded = subject.sink { actualResult.append($0) }
for value in expectedResult {
subject.send(value)
}
XCTAssertEqual(actualResult, expectedResult)
}
However, if I take the advise of the warning and change that line to assign to '_' the test fails because the value is released before the closure is invoked. This worked in earlier compiler versions because apparently the release was delayed.
_ = subject.sink { actualResult.append($0) }
Is there some way to suppress this warning, or prevent the new aggressive release behavior in this case?
Thanks,
Nick