From 162ea1c26840c1bc6bb6c8921a4894ca28796557 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Mon, 18 Oct 2021 10:05:41 -0700 Subject: [PATCH] Honor suppression on switch expression --- .../Portable/Binder/Binder_Conversions.cs | 2 +- .../Semantics/NullableReferenceTypesTests.cs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs index 8c8bab1156c86..4e187ce3f65d5 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs @@ -427,7 +427,7 @@ private BoundExpression ConvertSwitchExpression(BoundUnconvertedSwitchExpression var newSwitchArms = builder.ToImmutableAndFree(); return new BoundConvertedSwitchExpression( source.Syntax, source.Type, targetTyped, source.Expression, newSwitchArms, source.DecisionDag, - source.DefaultLabel, source.ReportedNotExhaustive, destination, hasErrors || source.HasErrors); + source.DefaultLabel, source.ReportedNotExhaustive, destination, hasErrors || source.HasErrors).WithSuppression(source.IsSuppressed); } private BoundExpression CreateUserDefinedConversion( diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index 8bc4333f834af..09d8ebf1b08e0 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -153293,5 +153293,24 @@ public void M() Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "field").WithLocation(19, 17) ); } + + [Fact, WorkItem(52143, "https://github.com/dotnet/roslyn/issues/52143")] + public void SuppressedSwitchExpression() + { + var source = @" +#nullable enable +C? nullable = null; + +C c2 = (args[0].Length switch +{ + 0 => nullable, + _ => null +})!; + +class C { } +"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics(); + } } }