diff --git a/src/EditorFeatures/CSharpTest/ImplementInterface/ImplementInterfaceTests.cs b/src/EditorFeatures/CSharpTest/ImplementInterface/ImplementInterfaceTests.cs index 549e083a205ee..dd5e6c7332ab9 100644 --- a/src/EditorFeatures/CSharpTest/ImplementInterface/ImplementInterfaceTests.cs +++ b/src/EditorFeatures/CSharpTest/ImplementInterface/ImplementInterfaceTests.cs @@ -13,6 +13,7 @@ using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; using Xunit; +using Xunit.Sdk; namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ImplementInterface { @@ -7892,6 +7893,137 @@ public void M() where T : notnull { throw new System.NotImplementedException(); } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)] + public async Task TestWithNullableProperty() + { + await TestInRegularAndScriptAsync( +@"#nullable enable + +public interface ITest +{ + string? P { get; } +} +public class Test : [|ITest|] +{ +}", +@"#nullable enable + +public interface ITest +{ + string? P { get; } +} +public class Test : ITest +{ + public string? P => throw new System.NotImplementedException(); +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)] + public async Task TestWithNullablePropertyAlreadyImplemented() + { + await TestMissingAsync( +@"#nullable enable + +public interface ITest +{ + string? P { get; } +} +public class Test : [|ITest|] +{ + public string? P => throw new System.NotImplementedException(); +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)] + public async Task TestWithNullableMethod() + { + await TestInRegularAndScriptAsync( +@"#nullable enable + +public interface ITest +{ + string? P(); +} +public class Test : [|ITest|] +{ +}", +@"#nullable enable + +public interface ITest +{ + string? P(); +} +public class Test : ITest +{ + public string? P() + { + throw new System.NotImplementedException(); + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)] + public async Task TestWithNullableEvent() + { + // Question whether this is needed, + // see https://github.com/dotnet/roslyn/issues/36673 + await TestInRegularAndScriptAsync( +@"#nullable enable + +using System; + +public interface ITest +{ + event EventHandler? SomeEvent; +} +public class Test : [|ITest|] +{ +}", +@"#nullable enable + +using System; + +public interface ITest +{ + event EventHandler? SomeEvent; +} +public class Test : ITest +{ + public event EventHandler? SomeEvent; +}"); + } + + [Fact(Skip = "https://github.com/dotnet/roslyn/issues/36101"), Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)] + public async Task TestWithNullableDisabled() + { + await TestInRegularAndScriptAsync( +@"#nullable enable + +public interface ITest +{ + string? P { get; } +} + +#nullable disable + +public class Test : [|ITest|] +{ +}", +@"#nullable enable + +public interface ITest +{ + string? P { get; } +} + +#nullable disable + +public class Test : ITest +{ + public string P => throw new System.NotImplementedException(); }"); } } diff --git a/src/Workspaces/Core/Portable/CodeGeneration/CodeGenerationSymbolFactory.cs b/src/Workspaces/Core/Portable/CodeGeneration/CodeGenerationSymbolFactory.cs index f3663dc225007..b9cb108694cfa 100644 --- a/src/Workspaces/Core/Portable/CodeGeneration/CodeGenerationSymbolFactory.cs +++ b/src/Workspaces/Core/Portable/CodeGeneration/CodeGenerationSymbolFactory.cs @@ -434,7 +434,7 @@ internal static IMethodSymbol CreateMethodSymbol( attributes, accessibility ?? method.DeclaredAccessibility, modifiers ?? method.GetSymbolModifiers(), - method.ReturnType, + method.GetReturnTypeWithAnnotatedNullability(), method.RefKind, explicitInterfaceImplementations, name ?? method.Name, @@ -460,7 +460,7 @@ internal static IPropertySymbol CreatePropertySymbol( attributes, accessibility ?? property.DeclaredAccessibility, modifiers ?? property.GetSymbolModifiers(), - property.Type, + property.GetTypeWithAnnotatedNullability(), property.RefKind, explicitInterfaceImplementations, name ?? property.Name, @@ -484,7 +484,7 @@ internal static IEventSymbol CreateEventSymbol( attributes, accessibility ?? @event.DeclaredAccessibility, modifiers ?? @event.GetSymbolModifiers(), - @event.Type, + @event.GetTypeWithAnnotatedNullability(), explicitInterfaceImplementations, name ?? @event.Name, addMethod,