-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add APICompat rule to detect static keyword changes on members and types #52112
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
6e9c62f
59f685e
652024e
f0663a5
5334970
203993c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.DotNet.ApiCompatibility.Rules | ||
| { | ||
| /// <summary> | ||
| /// This class implements a rule to check that the 'static' keyword is not added to | ||
| /// or removed from a member or type. | ||
| /// </summary> | ||
| public class CannotAddOrRemoveStaticKeyword : IRule | ||
| { | ||
| private readonly IRuleSettings _settings; | ||
|
|
||
| public CannotAddOrRemoveStaticKeyword(IRuleSettings settings, IRuleRegistrationContext context) | ||
| { | ||
| _settings = settings; | ||
| context.RegisterOnMemberSymbolAction(RunOnMemberSymbol); | ||
| context.RegisterOnTypeSymbolAction(RunOnTypeSymbol); | ||
| } | ||
|
|
||
| private void RunOnMemberSymbol(ISymbol? left, ISymbol? right, MetadataInformation leftMetadata, MetadataInformation rightMetadata, IList<CompatDifference> differences) | ||
| { | ||
| // Members must exist | ||
| if (left is null || right is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Check if static modifier changed on the member | ||
| if (left.IsStatic && !right.IsStatic) | ||
| { | ||
| // Removing static is always breaking (both binary and source breaking) | ||
| differences.Add(new CompatDifference( | ||
| leftMetadata, | ||
| rightMetadata, | ||
| DiagnosticIds.CannotRemoveStaticFromMember, | ||
| string.Format(Resources.CannotRemoveStaticFromMember, right), | ||
| DifferenceType.Removed, | ||
| right)); | ||
| } | ||
| else if (!left.IsStatic && right.IsStatic) | ||
| { | ||
| // Adding static is always breaking (binary breaking) | ||
ericstj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| differences.Add(new CompatDifference( | ||
| leftMetadata, | ||
| rightMetadata, | ||
| DiagnosticIds.CannotAddStaticToMember, | ||
| string.Format(Resources.CannotAddStaticToMember, right), | ||
| DifferenceType.Added, | ||
| right)); | ||
| } | ||
| } | ||
|
|
||
| private void RunOnTypeSymbol(ITypeSymbol? left, ITypeSymbol? right, MetadataInformation leftMetadata, MetadataInformation rightMetadata, IList<CompatDifference> differences) | ||
| { | ||
| // Types must exist | ||
| if (left is null || right is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Check if static modifier was added to the type | ||
| // Adding static to a type is breaking since static types cannot be used as | ||
| // return types, generic parameters, or arguments | ||
ericstj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Removing static from a type is compatible (members will be checked separately) | ||
| if (!left.IsStatic && right.IsStatic) | ||
ericstj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| differences.Add(new CompatDifference( | ||
| leftMetadata, | ||
| rightMetadata, | ||
| DiagnosticIds.CannotAddStaticToType, | ||
| string.Format(Resources.CannotAddStaticToType, right), | ||
| DifferenceType.Added, | ||
| right)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.