File: optional_try_migration.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (112 lines) | stat: -rw-r--r-- 3,142 bytes parent folder | download | duplicates (2)
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
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -c -swift-version 4 -primary-file %s -emit-migrated-file-path %t/optional_try_migration.result.swift %api_diff_data_dir
// RUN: %diff -u %S/optional_try_migration.swift.expected %t/optional_try_migration.result.swift

func fetchOptInt() throws -> Int? {
    return 3
}

func fetchInt() throws -> Int {
    return 3
}

func fetchAny() throws -> Any {
    return 3
}

func testOnlyMigrateChangedBehavior() {
    // No migration needed
    let _ = try? fetchInt()

    // Migration needed
    let _ = try? fetchOptInt()
}

func testExplicitCasts() {

    // No migration needed, because there's an explicit cast on the try already
    let _ = (try? fetchOptInt()) as? Int

    // Migration needed; the 'as? Int' is part of the sub-expression
    let _ = try? fetchAny() as? Int

    // No migration needed; the subexpression is non-optional so behavior has not changed
    let _ = (try? fetchAny()) as? Int

    // No migration needed, because there's an explicit cast on the try already
    let _ = (try? fetchOptInt()) as! Int // expected-warning {{forced cast from 'Int??' to 'Int' only unwraps optionals; did you mean to use '!!'?}}

    // No migration needed; the subexpression is non-optional
    let _ = try? fetchAny() as! Int

    // No migration needed; the subexpression is non-optional so behavior has not changed
    let _ = (try? fetchAny()) as! Int

    // Migration needed; the explicit cast is not directly on the try?
    let _ = String(describing: try? fetchOptInt()) as Any

    // No migration needed, because the try's subexpression is non-optional
    let _ = String(describing: try? fetchInt()) as Any

}

func testOptionalChaining() {
    struct Thing {
        func fetchInt() throws -> Int { return 3 }
        func fetchOptInt() throws -> Int { return 3 }
    }

    let thing = Thing()
    let optThing: Thing? = Thing()

    // Migration needed
    let _ = try? optThing?.fetchInt()

    // Migration needed
    let _ = try? optThing?.fetchOptInt()

    // No migration needed
    let _ = try? optThing!.fetchOptInt()

    // No migration needed, because of the explicit cast
    let _ = (try? optThing?.fetchOptInt()) as? Int // expected-warning{{conditional downcast from 'Int?' to 'Int' does nothing}}

    // Migration needed
    let _ = try? thing.fetchInt()

    // Migration needed
    let _ = try? thing.fetchOptInt()

    // No migration needed, because of the explicit cast
    let _ = (try? thing.fetchOptInt()) as! Int // expected-warning {{forced cast from 'Int?' to 'Int' only unwraps optionals; did you mean to use '!'?}}
}


func testIfLet() {
    
    // Migration needed
    if let optionalX = try? fetchOptInt(),
        let x = optionalX
    {
        print(x)
    }
    
    // Don't change 'try?'s that haven't changed behavior
    if let x = try? fetchInt(),
        let y = try? fetchInt() {
        print(x, y)
    }
}


func testCaseMatching() {
    // Migration needed
    if case let x?? = try? fetchOptInt() {
        print(x)
    }

    // No migration needed
    if case let x? = try? fetchInt() {
        print(x)
    }
}