File: optional_try_migration.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (112 lines) | stat: -rw-r--r-- 3,142 bytes parent folder | download
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)
    }
}