@@ -2720,11 +2720,12 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
27202720 // course we're in a context that could never handle an 'async'. Then, we
27212721 // produce an error.
27222722 if (!Flags.has (ContextFlags::HasAnyAsyncSite)) {
2723- if (CurContext.handlesAsync (ConditionalEffectKind::Conditional))
2724- Ctx. Diags . diagnose (E-> getAwaitLoc (), diag::no_async_in_await );
2725- else
2723+ if (CurContext.handlesAsync (ConditionalEffectKind::Conditional)) {
2724+ diagnoseRedundantAwait (E );
2725+ } else {
27262726 CurContext.diagnoseUnhandledAsyncSite (Ctx.Diags , E, None,
27272727 /* forAwait=*/ true );
2728+ }
27282729 }
27292730
27302731 // Inform the parent of the walk that an 'await' exists here.
@@ -2742,7 +2743,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
27422743 // Warn about 'try' expressions that weren't actually needed.
27432744 if (!Flags.has (ContextFlags::HasTryThrowSite)) {
27442745 if (!E->isImplicit ())
2745- Ctx. Diags . diagnose (E-> getTryLoc (), diag::no_throw_in_try );
2746+ diagnoseRedundantTry (E );
27462747
27472748 // Diagnose all the call sites within a single unhandled 'try'
27482749 // at the same time.
@@ -2762,9 +2763,8 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
27622763 E->getSubExpr ()->walk (*this );
27632764
27642765 // Warn about 'try' expressions that weren't actually needed.
2765- if (!Flags.has (ContextFlags::HasTryThrowSite)) {
2766- Ctx.Diags .diagnose (E->getLoc (), diag::no_throw_in_try);
2767- }
2766+ if (!Flags.has (ContextFlags::HasTryThrowSite))
2767+ diagnoseRedundantTry (E);
27682768
27692769 scope.preserveCoverageFromOptionalOrForcedTryOperand ();
27702770 return ShouldNotRecurse;
@@ -2778,9 +2778,8 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
27782778 E->getSubExpr ()->walk (*this );
27792779
27802780 // Warn about 'try' expressions that weren't actually needed.
2781- if (!Flags.has (ContextFlags::HasTryThrowSite)) {
2782- Ctx.Diags .diagnose (E->getLoc (), diag::no_throw_in_try);
2783- }
2781+ if (!Flags.has (ContextFlags::HasTryThrowSite))
2782+ diagnoseRedundantTry (E);
27842783
27852784 scope.preserveCoverageFromOptionalOrForcedTryOperand ();
27862785 return ShouldNotRecurse;
@@ -2818,6 +2817,30 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
28182817 return ShouldRecurse;
28192818 }
28202819
2820+ void diagnoseRedundantTry (AnyTryExpr *E) const {
2821+ if (auto *SVE = SingleValueStmtExpr::tryDigOutSingleValueStmtExpr (E)) {
2822+ // For an if/switch expression, produce an error instead of a warning.
2823+ Ctx.Diags .diagnose (E->getTryLoc (),
2824+ diag::effect_marker_on_single_value_stmt,
2825+ " try" , SVE->getStmt ()->getKind ())
2826+ .highlight (E->getTryLoc ());
2827+ return ;
2828+ }
2829+ Ctx.Diags .diagnose (E->getTryLoc (), diag::no_throw_in_try);
2830+ }
2831+
2832+ void diagnoseRedundantAwait (AwaitExpr *E) const {
2833+ if (auto *SVE = SingleValueStmtExpr::tryDigOutSingleValueStmtExpr (E)) {
2834+ // For an if/switch expression, produce an error instead of a warning.
2835+ Ctx.Diags .diagnose (E->getAwaitLoc (),
2836+ diag::effect_marker_on_single_value_stmt,
2837+ " await" , SVE->getStmt ()->getKind ())
2838+ .highlight (E->getAwaitLoc ());
2839+ return ;
2840+ }
2841+ Ctx.Diags .diagnose (E->getAwaitLoc (), diag::no_async_in_await);
2842+ }
2843+
28212844 void diagnoseUncoveredAsyncSite (const Expr *anchor) const {
28222845 auto asyncPointIter = uncoveredAsync.find (anchor);
28232846 if (asyncPointIter == uncoveredAsync.end ())
0 commit comments