Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include experimental attribute in API definition for properties #7232

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,13 @@ private ApiName GetApiName(ISymbol symbol)
{
for (var current = symbol; current is not null; current = current.ContainingSymbol)
{
foreach (var attribute in current.GetAttributes())
var attributes = current.GetAttributes();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 It's possible for both the property and the method to have attributes. For example:

[Experimental("A")]
public object? Value { [NotNull] get; }

Perhaps we extract the body of the foreach to a local function, and then call for both current.GetAttributes() and (if no experimental attribute was found) also call it for the containing property if the current method is an accessor.

if (attributes.IsEmpty && current is IMethodSymbol { AssociatedSymbol: IPropertySymbol property })
{
attributes = property.GetAttributes();
}

foreach (var attribute in attributes)
{
if (attribute.AttributeClass is { Name: "ExperimentalAttribute", ContainingSymbol: INamespaceSymbol { Name: nameof(System.Diagnostics.CodeAnalysis), ContainingNamespace: { Name: nameof(System.Diagnostics), ContainingNamespace: { Name: nameof(System), ContainingNamespace.IsGlobalNamespace: true } } } })
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3295,6 +3295,106 @@ public async Task TestExperimentalApiWithInvalidArgumentAsync(string invalidArgu
await test.RunAsync();
}

[Fact]
[WorkItem(7195, "https://github.com/dotnet/roslyn-analyzers/issues/7195")]
public async Task TestExperimentalApiWithProperty()
{
var source = $$"""
using System.Diagnostics.CodeAnalysis;

class C
{
[Experimental("RSEXPERIMENTAL001")]
{{EnabledModifierCSharp}} bool DisableNullableAnalysis { {|{{AddNewApiId}}:get|}; }
}
""";

const string shippedText = """
C
C.C() -> void
""";
const string fixedUnshippedText = """
[RSEXPERIMENTAL001]C.DisableNullableAnalysis.get -> bool
""";

var test = new CSharpCodeFixTest<DeclarePublicApiAnalyzer, DeclarePublicApiFix, XUnitVerifier>()
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
CompilerDiagnostics = CompilerDiagnostics.None,
TestState =
{
Sources = { source },
AdditionalFiles =
{
(ShippedFileName, shippedText),
(UnshippedFileName, string.Empty),
},
},
FixedState =
{
AdditionalFiles =
{
(ShippedFileName, shippedText),
(UnshippedFileName, fixedUnshippedText),
},
},
};

test.DisabledDiagnostics.AddRange(DisabledDiagnostics);

await test.RunAsync();
}

[Fact]
[WorkItem(7195, "https://github.com/dotnet/roslyn-analyzers/issues/7195")]
public async Task TestExperimentalApiWithAbstractProperty()
{
var source = $$"""
using System.Diagnostics.CodeAnalysis;

class C
{
[Experimental("RSEXPERIMENTAL001")]
{{EnabledModifierCSharp}} abstract bool DisableNullableAnalysis { {|{{AddNewApiId}}:get|}; }
}
""";

const string shippedText = """
C
C.C() -> void
""";
const string fixedUnshippedText = """
[RSEXPERIMENTAL001]abstract C.DisableNullableAnalysis.get -> bool
""";

var test = new CSharpCodeFixTest<DeclarePublicApiAnalyzer, DeclarePublicApiFix, XUnitVerifier>()
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
CompilerDiagnostics = CompilerDiagnostics.None,
TestState =
{
Sources = { source },
AdditionalFiles =
{
(ShippedFileName, shippedText),
(UnshippedFileName, string.Empty),
},
},
FixedState =
{
AdditionalFiles =
{
(ShippedFileName, shippedText),
(UnshippedFileName, fixedUnshippedText),
},
},
};

test.DisabledDiagnostics.AddRange(DisabledDiagnostics);

await test.RunAsync();
}

#endregion
}
}