-
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?
Conversation
Co-authored-by: ericstj <[email protected]>
Co-authored-by: ericstj <[email protected]>
...tibility/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/CannotAddOrRemoveStaticKeyword.cs
Show resolved
Hide resolved
...tibility/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/CannotAddOrRemoveStaticKeyword.cs
Show resolved
Hide resolved
Co-authored-by: krwq <[email protected]>
krwq
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ericstj I'd appreciate second person looking at this - especially the strict mode warning
The log URL appears to be expired or inaccessible. I've verified:
The changes I made:
Could you provide more details about the failure? Specifically:
The failure might be unrelated to my changes, or there might be a test asset that legitimately changes static and now triggers the new rule. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new APICompat rule to detect when the static keyword is added to or removed from members and types, which is a binary breaking change. The implementation follows the established patterns in the codebase for compatibility rules.
- Implements
CannotAddOrRemoveStaticKeywordrule that checks for static modifier changes on methods, properties, fields, events, and types - Adds 4 new diagnostic IDs (CP0022-CP0025) with appropriate localization strings
- Includes comprehensive test coverage for all member types and strict mode behavior
- Correctly handles the nuance that removing static from types is compatible (members checked separately) but reported in strict mode for developer awareness
Reviewed changes
Copilot reviewed 18 out of 19 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
CannotAddOrRemoveStaticKeyword.cs |
New rule implementation with member and type-level checks for static keyword changes |
CannotAddOrRemoveStaticKeywordTests.cs |
Comprehensive test coverage including reproduction case, all member types, type-level changes, and strict mode |
DiagnosticIds.cs |
Adds four new diagnostic ID constants (CP0022-CP0025) |
Resources.resx |
Adds four new diagnostic message strings |
Resources.*.xlf |
Adds localization entries with state="new" for all supported languages |
RuleFactory.cs |
Registers the new rule to run by default |
CannotChangeGenericConstraints.cs |
Minor whitespace cleanup (trailing whitespace removal and spacing fix) |
The implementation is clean, well-tested, and follows the established patterns in the codebase. No issues were found that require changes.
...tibility/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/CannotAddOrRemoveStaticKeyword.cs
Outdated
Show resolved
Hide resolved
test/Microsoft.DotNet.ApiCompatibility.Tests/Rules/CannotAddOrRemoveStaticKeywordTests.cs
Outdated
Show resolved
Hide resolved
test/Microsoft.DotNet.ApiCompatibility.Tests/Rules/CannotAddOrRemoveStaticKeywordTests.cs
Outdated
Show resolved
Hide resolved
...tibility/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/CannotAddOrRemoveStaticKeyword.cs
Show resolved
Hide resolved
...tibility/ApiCompat/Microsoft.DotNet.ApiCompatibility/Rules/CannotAddOrRemoveStaticKeyword.cs
Outdated
Show resolved
Hide resolved
src/Compatibility/ApiCompat/Microsoft.DotNet.ApiCompatibility/DiagnosticIds.cs
Outdated
Show resolved
Hide resolved
|
@copilot please address feedback |
Co-authored-by: ericstj <[email protected]>
| <value>Visibility of '{0}' reduced from '{1}' to '{2}'.</value> | ||
| </data> | ||
| <data name="CannotChangeStatic" xml:space="preserve"> | ||
| <value>Cannot {0} static keyword {1} '{2}'.</value> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type of substitution is not localizable since it has English words in code. Can you please split this into two resources. Keep a single diagnostic ID but allow it to have two different messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please address this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split into two separate resource strings for proper localization:
CannotAddStatic: "Cannot add static keyword to '{0}'."CannotRemoveStatic: "Cannot remove static keyword from '{0}'."
The diagnostic ID remains CP0022 but uses the appropriate message based on the operation. Commit: 203993c
Co-authored-by: ericstj <[email protected]>
Latest Changes (this commit)
Split the resource string into two separate strings for proper localization:
CannotAddStatic: "Cannot add static keyword to '{0}'."CannotRemoveStatic: "Cannot remove static keyword from '{0}'."This allows translators to properly localize each message independently, instead of trying to substitute English words ("add"/"remove", "to"/"from") into a template which was not localizable.
The diagnostic ID remains CP0022 (CannotChangeStatic) with two different message formats depending on whether static is being added or removed.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.