Add test cases for the 'nil coalescing operator'.

This commit is contained in:
Crazy凡
2023-06-02 21:02:07 +08:00
parent 3480bf68f9
commit 4e346c1cf8
2 changed files with 24 additions and 1 deletions

View File

@@ -1557,3 +1557,26 @@ do {
result.append(value) // expected-error {{argument type 'NonHashable' does not conform to expected type 'Hashable'}}
}
}
// https://github.com/apple/swift/issues/66206
func testNilCoalescingOperatorRemoveFix() {
let _ = "" ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{13-19=}}
let _ = "" ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{13-23=}}
let _ = "" /* This is a comment */ ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{13-43=}}
let _ = "" // This is a comment
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1567:13-1568:10=}}
let _ = "" // This is a comment
/*
* The blank line below is part of the test case, do not delete it
*/
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1570:13-1575:10=}}
if ("" ?? // This is a comment // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1577:9-1578:9=}}
"").isEmpty {}
if ("" // This is a comment
?? "").isEmpty {} // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1580:9-1581:12=}}
}

View File

@@ -801,7 +801,7 @@ func testNilCoalescePrecedence(cond: Bool, a: Int?, r: ClosedRange<Int>?) {
// <rdar://problem/27457457> [Type checker] Diagnose unsavory optional injections
// Accidental optional injection for ??.
let i = 42
_ = i ?? 17 // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'Int', so the right side is never used}} {{9-15=}}
_ = i ?? 17 // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'Int', so the right side is never used}} {{8-14=}}
}
// <rdar://problem/19772570> Parsing of as and ?? regressed