Skip to content

Commit 2cabf87

Browse files
committed
Add a couple additional tests for Optional-to-Any warnings.
(cherry picked from commit dbb5bc4)
1 parent 0e1b5ab commit 2cabf87

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

test/Sema/diag_unintended_optional_behavior.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,22 @@ func warnCollectionOfOptionalToAnyCoercion(_ a: [Int?], _ d: [String : Int?]) {
145145
// expected-note@-1 {{explicitly cast to '[Any]' with 'as [Any]' to silence this warning}}{{25-25= as [Any]}}
146146
// expected-warning@-2 {{expression implicitly coerced from '[String : Int?]' to '[String : Any]'}}
147147
// expected-note@-3 {{explicitly cast to '[String : Any]' with 'as [String : Any]' to silence this warning}}{{28-28= as [String : Any]}}
148+
takesCollectionOfAny([a[0]], ["test" : a[0]]) // expected-warning {{expression implicitly coerced from 'Int?' to 'Any'}}
149+
// expected-note@-1 {{provide a default value to avoid this warning}}{{29-29= ?? <#default value#>}}
150+
// expected-note@-2 {{force-unwrap the value to avoid this warning}}{{29-29=!}}
151+
// expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{29-29= as Any}}
152+
// expected-warning@-4 {{expression implicitly coerced from 'Int?' to 'Any'}}
153+
// expected-note@-5 {{provide a default value to avoid this warning}}{{46-46= ?? <#default value#>}}
154+
// expected-note@-6 {{force-unwrap the value to avoid this warning}}{{46-46=!}}
155+
// expected-note@-7 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{46-46= as Any}}
148156

149157
takesCollectionOfAny(a as [Any], d as [String : Any])
150158
}
151159

160+
func nowarnCollectionOfIUOToAnyCoercion(_ a: Int!) {
161+
takesCollectionOfAny([a], ["test" : a])
162+
}
163+
152164
func warnCollectionOfTripleOptionalToAnyCoercion(_ a: [Any???], _ d: [String: Any???]) {
153165
takesCollectionOfAny(a, d) // expected-warning {{expression implicitly coerced from '[Any???]' to '[Any]'}}
154166
// expected-note@-1 {{explicitly cast to '[Any]' with 'as [Any]' to silence this warning}}{{25-25= as [Any]}}

test/Sema/diag_unintended_optional_behavior_swift_5.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,16 @@ func warnIUOToOptionalAnyCoercion(_ a: Int!, _ b: Any?!, _ c: Int??!, _ d: Any??
6767
takesOptionalAny(c!!, d!!!)
6868
takesOptionalAny(c as Any?, d as Any?)
6969
}
70+
71+
func takesCollectionOfAny(_ a: [Any], _ d: [String : Any]) {}
72+
73+
func warnCollectionOfIUOToAnyCoercion(_ a: Int!) {
74+
takesCollectionOfAny([a], ["test" : a]) // expected-warning {{expression implicitly coerced from 'Int?' to 'Any'}}
75+
// expected-note@-1 {{provide a default value to avoid this warning}}{{26-26= ?? <#default value#>}}
76+
// expected-note@-2 {{force-unwrap the value to avoid this warning}}{{26-26=!}}
77+
// expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{26-26= as Any}}
78+
// expected-warning@-4 {{expression implicitly coerced from 'Int?' to 'Any'}}
79+
// expected-note@-5 {{provide a default value to avoid this warning}}{{40-40= ?? <#default value#>}}
80+
// expected-note@-6 {{force-unwrap the value to avoid this warning}}{{40-40=!}}
81+
// expected-note@-7 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{40-40= as Any}}
82+
}

0 commit comments

Comments
 (0)