-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix IDE0032: Code fixer adds setter for static properties with instance constructor writes #81327
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
Conversation
|
@copilot the analyzer/fixer checks if there are writes to the field to determine if it should make a private setter. However it ignores writes inside of constructors (as a private setter isn't needed for an instance property in that case). However, as this is a static property we should not ignore instance constructor writes. |
…tance constructors - Added check to prevent diagnostic when static property has no setter and field is written in instance constructor - Added IsStaticConstructor abstract method to distinguish instance vs static constructors - Implemented IsStaticConstructor for both C# and VB analyzers - Added test cases to validate the fix Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
…nce constructor writes - Modified AbstractUseAutoPropertyCodeFixProvider to filter constructors based on static-ness - For static fields, only static constructors are considered valid initialization contexts - For instance fields, only instance constructors are considered valid initialization contexts - Writes in mismatched constructors (e.g., instance constructor writing static field) are now treated as writes outside constructor, causing a setter to be added - Updated test to expect a private setter to be generated instead of no diagnostic Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Fixed in dafbf3f. The code fixer now filters constructors by static-ness - for static fields, only static constructors are included when determining if writes are "outside constructor". Instance constructor writes to static fields now correctly trigger a private setter to be added. |
The IDE0032 code fixer incorrectly produced invalid code when a static field was assigned in an instance constructor, resulting in CS0200 (cannot assign to read-only property).
Changes
Code fixer logic (
AbstractUseAutoPropertyCodeFixProvider.cs)Result
The code fixer now generates valid code with a private setter:
Tests
Static field + instance constructor write → property with private setter
Static field + static constructor write → property with initializer (no setter needed)
Static field with existing setter + instance constructor write → property preserves setter
Fixes IDE0032 code fixer produces invalid code for static field assigned in constructor #81320
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.