Skip to content

Commit 4e346c1

Browse files
committed
Add test cases for the 'nil coalescing operator'.
1 parent 3480bf6 commit 4e346c1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

test/Constraints/diagnostics.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,3 +1557,26 @@ do {
15571557
result.append(value) // expected-error {{argument type 'NonHashable' does not conform to expected type 'Hashable'}}
15581558
}
15591559
}
1560+
1561+
// https://github.com/apple/swift/issues/66206
1562+
func testNilCoalescingOperatorRemoveFix() {
1563+
let _ = "" ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{13-19=}}
1564+
let _ = "" ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{13-23=}}
1565+
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=}}
1566+
1567+
let _ = "" // This is a comment
1568+
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1567:13-1568:10=}}
1569+
1570+
let _ = "" // This is a comment
1571+
/*
1572+
* The blank line below is part of the test case, do not delete it
1573+
*/
1574+
1575+
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1570:13-1575:10=}}
1576+
1577+
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=}}
1578+
"").isEmpty {}
1579+
1580+
if ("" // This is a comment
1581+
?? "").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=}}
1582+
}

test/expr/expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ func testNilCoalescePrecedence(cond: Bool, a: Int?, r: ClosedRange<Int>?) {
801801
// <rdar://problem/27457457> [Type checker] Diagnose unsavory optional injections
802802
// Accidental optional injection for ??.
803803
let i = 42
804-
_ = i ?? 17 // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'Int', so the right side is never used}} {{9-15=}}
804+
_ = i ?? 17 // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'Int', so the right side is never used}} {{8-14=}}
805805
}
806806

807807
// <rdar://problem/19772570> Parsing of as and ?? regressed

0 commit comments

Comments
 (0)