1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// RUN: %target-swift-emit-sil %s -verify | %FileCheck %s
func testUnreachableAfterReturn() -> Int {
var x: Int = 3
return x
x += 1 //expected-warning {{code after 'return' will never be executed}}
}
func testUnreachableAfterIfReturn(a: Bool) -> Int {
if a {
return 1
} else {
return 0
}
var _: Int = testUnreachableAfterReturn() // expected-warning {{will never be executed}}
}
func testUnreachableForAfterContinue(b: Bool) {
for _ in 0..<10 {
var y: Int = 300
y += 1
if b {
break
y += 1 // expected-warning {{code after 'break' will never be executed}}
}
continue
y -= 1 // expected-warning {{code after 'continue' will never be executed}}
}
}
func testUnreachableWhileAfterContinue(b: Bool) {
var i:Int = 0
while (i<10) {
var y: Int = 300
y += 1
if b {
break
y += 1 // expected-warning {{code after 'break' will never be executed}}
}
continue
i += 1 // expected-warning {{code after 'continue' will never be executed}}
}
}
func testBreakAndContinue() {
var m = 0
for _ in 0 ..< 10 {
m += 1
if m == 15 {
break
} else {
continue
}
m += 1 // expected-warning {{will never be executed}}
}
}
// <rdar://problem/20253447> `case let Case` without bindings incorrectly matches other cases
enum Tree {
case Leaf(Int)
case Branch(Int)
}
func testUnreachableCase1(a : Tree) {
switch a {
case let Leaf:
_ = Leaf
return
case .Branch(_): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return
}
}
func testUnreachableCase2(a : Tree) {
switch a {
case let Leaf:
_ = Leaf
fallthrough
case .Branch(_): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return
}
}
func testUnreachableCase3(a : Tree) {
switch a {
case _:
break
case .Branch(_): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return
}
}
func testUnreachableCase4(a : Tree) {
switch a {
case .Leaf(_):
return
case .Branch(_):
return
}
}
func testUnreachableCase5(a : Tree) {
switch a {
case _:
break
default: // expected-warning {{default will never be executed}}
return
}
}
// https://github.com/apple/swift/issues/48333
func testOptionalEvaluationBreak(a : Tree) {
class C { func foo() {} }
func createOptional() -> C? { return C() }
switch a {
case _:
break
createOptional()?.foo() // expected-warning {{code after 'break' will never be executed}}
}
}
func testUnreachableAfterThrow(e: Error) throws {
throw e
return // expected-warning {{code after 'throw' will never be executed}}
}
class TestThrowInInit {
required init(e: Error) throws {
throw e // no unreachable code diagnostic for the implicit return.
}
}
// https://github.com/apple/swift/issues/48696
func f_48696() {
var bar: String? = ""
return;
bar?.append("x") // expected-warning{{code after 'return' will never be executed}}
}
func testUnreachableCatchClause() {
enum ErrorEnum: Error { case someError }
do {
throw ErrorEnum.someError
} catch let error {
print(error)
} catch ErrorEnum.someError { // expected-warning {{case will never be executed}}
print("some error")
}
}
// https://github.com/apple/swift/issues/56075
func f_56075() -> Int {
return Foo.bar
struct Foo { // no-warning
static var bar = 0
// CHECK: sil private @$s16unreachable_code7f_56075SiyF3FooL_V7fooFuncyyF : $@convention(method) (Foo) -> ()
func fooFunc() {}
}
func appendix() {} // no-warning
}
|