Skip to content

Commit 9315ad1

Browse files
committed
[TypeCheckEffects] Redundant effects markers on statement expressions should
be a warning. (cherry picked from commit 9fc7032)
1 parent 512f7b7 commit 9315ad1

File tree

4 files changed

+106
-106
lines changed

4 files changed

+106
-106
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,8 @@ ERROR(single_value_stmt_branch_must_end_in_result,none,
12441244
ERROR(cannot_jump_in_single_value_stmt,none,
12451245
"cannot '%0' in '%1' when used as expression",
12461246
(StmtKind, StmtKind))
1247-
ERROR(effect_marker_on_single_value_stmt,none,
1248-
"'%0' may not be used on '%1' expression", (StringRef, StmtKind))
1247+
WARNING(effect_marker_on_single_value_stmt,none,
1248+
"'%0' has no effect on '%1' expression", (StringRef, StmtKind))
12491249
ERROR(out_of_place_then_stmt,none,
12501250
"'then' may only appear as the last statement in an 'if' or 'switch' "
12511251
"expression", ())

lib/Sema/TypeCheckEffects.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,7 +2902,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
29022902

29032903
void diagnoseRedundantTry(AnyTryExpr *E) const {
29042904
if (auto *SVE = SingleValueStmtExpr::tryDigOutSingleValueStmtExpr(E)) {
2905-
// For an if/switch expression, produce an error instead of a warning.
2905+
// For an if/switch expression, produce a tailored warning.
29062906
Ctx.Diags.diagnose(E->getTryLoc(),
29072907
diag::effect_marker_on_single_value_stmt,
29082908
"try", SVE->getStmt()->getKind())
@@ -2914,7 +2914,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
29142914

29152915
void diagnoseRedundantAwait(AwaitExpr *E) const {
29162916
if (auto *SVE = SingleValueStmtExpr::tryDigOutSingleValueStmtExpr(E)) {
2917-
// For an if/switch expression, produce an error instead of a warning.
2917+
// For an if/switch expression, produce a tailored warning.
29182918
Ctx.Diags.diagnose(E->getAwaitLoc(),
29192919
diag::effect_marker_on_single_value_stmt,
29202920
"await", SVE->getStmt()->getKind())

test/expr/unary/if_expr.swift

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func stmts() {
548548

549549
if try if .random() { true } else { false } {}
550550
// expected-error@-1 {{'if' may only be used as expression in return, throw, or as the source of an assignment}}
551-
// expected-error@-2 {{'try' may not be used on 'if' expression}}
551+
// expected-warning@-2 {{'try' has no effect on 'if' expression}}
552552

553553
// expected-error@+1 {{'if' may only be used as expression in return, throw, or as the source of an assignment}}
554554
guard if .random() { true } else { false } else {
@@ -988,28 +988,28 @@ func return4() throws -> Int {
988988

989989
func tryIf1() -> Int {
990990
try if .random() { 0 } else { 1 }
991-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
991+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
992992
}
993993

994994
func tryIf2() -> Int {
995995
let x = try if .random() { 0 } else { 1 }
996-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
996+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
997997
return x
998998
}
999999

10001000
func tryIf3() -> Int {
10011001
return try if .random() { 0 } else { 1 }
1002-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1002+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10031003
}
10041004

10051005
func tryIf4() throws -> Int {
10061006
return try if .random() { 0 } else { 1 }
1007-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1007+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10081008
}
10091009

10101010
func tryIf5() throws -> Int {
10111011
return try if .random() { tryIf4() } else { 1 }
1012-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1012+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10131013
// expected-error@-2 {{call can throw but is not marked with 'try'}}
10141014
// expected-note@-3 {{did you mean to use 'try'?}}
10151015
// expected-note@-4 {{did you mean to handle error as optional value?}}
@@ -1018,7 +1018,7 @@ func tryIf5() throws -> Int {
10181018

10191019
func tryIf6() throws -> Int {
10201020
try if .random() { tryIf4() } else { 1 }
1021-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1021+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10221022
// expected-error@-2 {{call can throw but is not marked with 'try'}}
10231023
// expected-note@-3 {{did you mean to use 'try'?}}
10241024
// expected-note@-4 {{did you mean to handle error as optional value?}}
@@ -1027,7 +1027,7 @@ func tryIf6() throws -> Int {
10271027

10281028
func tryIf7() throws -> Int {
10291029
let x = try if .random() { tryIf4() } else { 1 }
1030-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1030+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10311031
// expected-error@-2 {{call can throw but is not marked with 'try'}}
10321032
// expected-note@-3 {{did you mean to use 'try'?}}
10331033
// expected-note@-4 {{did you mean to handle error as optional value?}}
@@ -1037,23 +1037,23 @@ func tryIf7() throws -> Int {
10371037

10381038
func tryIf8() throws -> Int {
10391039
return try if .random() { try tryIf4() } else { 1 }
1040-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1040+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10411041
}
10421042

10431043
func tryIf9() throws -> Int {
10441044
try if .random() { try tryIf4() } else { 1 }
1045-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1045+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10461046
}
10471047

10481048
func tryIf10() throws -> Int {
10491049
let x = try if .random() { try tryIf4() } else { 1 }
1050-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1050+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10511051
return x
10521052
}
10531053

10541054
func tryIf11() throws -> Int {
10551055
let x = try if .random() { try tryIf4() } else { tryIf4() }
1056-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1056+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10571057
// expected-error@-2 {{call can throw but is not marked with 'try'}}
10581058
// expected-note@-3 {{did you mean to use 'try'?}}
10591059
// expected-note@-4 {{did you mean to handle error as optional value?}}
@@ -1063,7 +1063,7 @@ func tryIf11() throws -> Int {
10631063

10641064
func tryIf12() throws -> Int {
10651065
let x = try if .random() { tryIf4() } else { tryIf4() }
1066-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1066+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
10671067
// expected-error@-2 2{{call can throw but is not marked with 'try'}}
10681068
// expected-note@-3 2{{did you mean to use 'try'?}}
10691069
// expected-note@-4 2{{did you mean to handle error as optional value?}}
@@ -1072,7 +1072,7 @@ func tryIf12() throws -> Int {
10721072
}
10731073

10741074
func tryIf13() throws -> Int {
1075-
let x = try if .random() { // expected-error {{'try' may not be used on 'if' expression}}
1075+
let x = try if .random() { // expected-warning {{'try' has no effect on 'if' expression}}
10761076
tryIf4() // expected-warning {{result of call to 'tryIf4()' is unused}}
10771077
// expected-error@-1 {{call can throw but is not marked with 'try'}}
10781078
// expected-note@-2 {{did you mean to use 'try'?}}
@@ -1104,7 +1104,7 @@ func throwsBool() throws -> Bool { true }
11041104

11051105
func tryIf14() throws -> Int {
11061106
try if throwsBool() { 0 } else { 1 }
1107-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1107+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
11081108
// expected-error@-2 {{call can throw but is not marked with 'try'}}
11091109
// expected-note@-3 {{did you mean to use 'try'?}}
11101110
// expected-note@-4 {{did you mean to handle error as optional value?}}
@@ -1113,7 +1113,7 @@ func tryIf14() throws -> Int {
11131113

11141114
func tryIf15() throws -> Int {
11151115
try if try throwsBool() { 0 } else { 1 }
1116-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1116+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
11171117
}
11181118

11191119
func tryIf16() throws -> Int {
@@ -1223,81 +1223,81 @@ func tryIf29(_ fn: () throws -> Int) rethrows -> Int {
12231223

12241224
func awaitIf1() async -> Int {
12251225
await if .random() { 0 } else { 1 }
1226-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1226+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12271227
}
12281228

12291229
func awaitIf2() async -> Int {
12301230
let x = await if .random() { 0 } else { 1 }
1231-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1231+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12321232
return x
12331233
}
12341234

12351235
func awaitIf3() async -> Int {
12361236
return await if .random() { 0 } else { 1 }
1237-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1237+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12381238
}
12391239

12401240
func awaitIf4() async -> Int {
12411241
return await if .random() { 0 } else { 1 }
1242-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1242+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12431243
}
12441244

12451245
func awaitIf5() async -> Int {
12461246
return await if .random() { awaitIf4() } else { 1 }
1247-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1247+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12481248
// expected-error@-2 {{expression is 'async' but is not marked with 'await'}}
12491249
// expected-note@-3 {{call is 'async'}}
12501250
}
12511251

12521252
func awaitIf6() async -> Int {
12531253
await if .random() { awaitIf4() } else { 1 }
1254-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1254+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12551255
// expected-error@-2 {{expression is 'async' but is not marked with 'await'}}
12561256
// expected-note@-3 {{call is 'async'}}
12571257
}
12581258

12591259
func awaitIf7() async -> Int {
12601260
let x = await if .random() { awaitIf4() } else { 1 }
1261-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1261+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12621262
// expected-error@-2 {{expression is 'async' but is not marked with 'await'}}
12631263
// expected-note@-3 {{call is 'async'}}
12641264
return x
12651265
}
12661266

12671267
func awaitIf8() async -> Int {
12681268
return await if .random() { await awaitIf4() } else { 1 }
1269-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1269+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12701270
}
12711271

12721272
func awaitIf9() async -> Int {
12731273
await if .random() { await awaitIf4() } else { 1 }
1274-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1274+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12751275
}
12761276

12771277
func awaitIf10() async -> Int {
12781278
let x = await if .random() { await awaitIf4() } else { 1 }
1279-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1279+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12801280
return x
12811281
}
12821282

12831283
func awaitIf11() async -> Int {
12841284
let x = await if .random() { await awaitIf4() } else { awaitIf4() }
1285-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1285+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12861286
// expected-error@-2 {{expression is 'async' but is not marked with 'await'}}
12871287
// expected-note@-3 {{call is 'async'}}
12881288
return x
12891289
}
12901290

12911291
func awaitIf12() async -> Int {
12921292
let x = await if .random() { awaitIf4() } else { awaitIf4() }
1293-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1293+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
12941294
// expected-error@-2 2{{expression is 'async' but is not marked with 'await'}}
12951295
// expected-note@-3 2{{call is 'async'}}
12961296
return x
12971297
}
12981298

12991299
func awaitIf13() async throws -> Int {
1300-
let x = await if .random() { // expected-error {{'await' may not be used on 'if' expression}}
1300+
let x = await if .random() { // expected-warning {{'await' has no effect on 'if' expression}}
13011301
awaitIf4() // expected-warning {{result of call to 'awaitIf4()' is unused}}
13021302
// expected-error@-1 {{expression is 'async' but is not marked with 'await'}}
13031303
// expected-note@-2 {{call is 'async'}}
@@ -1325,14 +1325,14 @@ func asyncBool() async -> Bool { true }
13251325

13261326
func awaitIf14() async -> Int {
13271327
await if asyncBool() { 0 } else { 1 }
1328-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1328+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
13291329
// expected-error@-2 {{expression is 'async' but is not marked with 'await'}}
13301330
// expected-note@-3 {{call is 'async'}}
13311331
}
13321332

13331333
func awaitIf15() async -> Int {
13341334
await if await asyncBool() { 0 } else { 1 }
1335-
// expected-error@-1 {{'await' may not be used on 'if' expression}}
1335+
// expected-warning@-1 {{'await' has no effect on 'if' expression}}
13361336
}
13371337

13381338
func awaitIf16() async -> Int {
@@ -1364,20 +1364,20 @@ func awaitIf20() async -> Int {
13641364

13651365
func tryAwaitIf1() async throws -> Int {
13661366
try await if .random() { 0 } else { 1 }
1367-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1368-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1367+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1368+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
13691369
}
13701370

13711371
func tryAwaitIf2() async throws -> Int {
13721372
try await if .random() { 0 } else { 1 } as Int
1373-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1374-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1373+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1374+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
13751375
}
13761376

13771377
func tryAwaitIf3() async throws -> Int {
13781378
try await if .random() { tryAwaitIf2() } else { 1 } as Int
1379-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1380-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1379+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1380+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
13811381
// expected-error@-3 {{call can throw but is not marked with 'try'}}
13821382
// expected-note@-4 {{did you mean to use 'try'?}}
13831383
// expected-note@-5 {{did you mean to handle error as optional value?}}
@@ -1388,16 +1388,16 @@ func tryAwaitIf3() async throws -> Int {
13881388

13891389
func tryAwaitIf4() async throws -> Int {
13901390
try await if .random() { try tryAwaitIf2() } else { 1 } as Int
1391-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1392-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1391+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1392+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
13931393
// expected-error@-3 {{expression is 'async' but is not marked with 'await'}}
13941394
// expected-note@-4 {{call is 'async'}}
13951395
}
13961396

13971397
func tryAwaitIf5() async throws -> Int {
13981398
try await if .random() { await tryAwaitIf2() } else { 1 } as Int
1399-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1400-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1399+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1400+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
14011401
// expected-error@-3 {{call can throw but is not marked with 'try'}}
14021402
// expected-note@-4 {{did you mean to use 'try'?}}
14031403
// expected-note@-5 {{did you mean to handle error as optional value?}}
@@ -1406,14 +1406,14 @@ func tryAwaitIf5() async throws -> Int {
14061406

14071407
func tryAwaitIf6() async throws -> Int {
14081408
try await if .random() { try await tryAwaitIf2() } else { 1 } as Int
1409-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1410-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1409+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1410+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
14111411
}
14121412

14131413
func tryAwaitIf7() async throws -> Int {
14141414
try await if .random() { tryAwaitIf2() } else { 1 }
1415-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1416-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1415+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1416+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
14171417
// expected-error@-3 {{call can throw but is not marked with 'try'}}
14181418
// expected-note@-4 {{did you mean to use 'try'?}}
14191419
// expected-note@-5 {{did you mean to handle error as optional value?}}
@@ -1424,16 +1424,16 @@ func tryAwaitIf7() async throws -> Int {
14241424

14251425
func tryAwaitIf8() async throws -> Int {
14261426
try await if .random() { try tryAwaitIf2() } else { 1 }
1427-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1428-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1427+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1428+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
14291429
// expected-error@-3 {{expression is 'async' but is not marked with 'await'}}
14301430
// expected-note@-4 {{call is 'async'}}
14311431
}
14321432

14331433
func tryAwaitIf9() async throws -> Int {
14341434
try await if .random() { await tryAwaitIf2() } else { 1 }
1435-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1436-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1435+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1436+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
14371437
// expected-error@-3 {{call can throw but is not marked with 'try'}}
14381438
// expected-note@-4 {{did you mean to use 'try'?}}
14391439
// expected-note@-5 {{did you mean to handle error as optional value?}}
@@ -1442,8 +1442,8 @@ func tryAwaitIf9() async throws -> Int {
14421442

14431443
func tryAwaitIf10() async throws -> Int {
14441444
try await if .random() { try await tryAwaitIf2() } else { 1 }
1445-
// expected-error@-1 {{'try' may not be used on 'if' expression}}
1446-
// expected-error@-2 {{'await' may not be used on 'if' expression}}
1445+
// expected-warning@-1 {{'try' has no effect on 'if' expression}}
1446+
// expected-warning@-2 {{'await' has no effect on 'if' expression}}
14471447
}
14481448

14491449
func tryAwaitIf11(_ fn: () async throws -> Int) async rethrows -> Int {

0 commit comments

Comments
 (0)