-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Update 'use auto property' to support using the field
keyword
#74629
Update 'use auto property' to support using the field
keyword
#74629
Conversation
// Can't remove the field if it has attributes on it. | ||
var attributes = getterField.GetAttributes(); | ||
if (attributes.Length > 0 && !@this.SupportsFieldAttributesOnProperties) | ||
return false; |
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 is changed. we used to always prevent converting if there was an attribute. now we support conversion in C# (since that can have attributes on a property that apply to a field).
// A setter is optional though. | ||
var setMethod = property.SetMethod; | ||
if (setMethod != null) | ||
if (property.SetMethod != null) |
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.
similar to above. if there is a set-method, we now do a deeper analysis to see which fields it uses.
// so, it's likely that that's the field to use. | ||
getterFields = getterFields.Where( | ||
static (field, property) => field.Name.EndsWith(property.Name, StringComparison.OrdinalIgnoreCase), | ||
property); |
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.
special heuristic for the very common case where a field/property have virtually the same name (just differing in casing, or having a _
prefix, etc.).
|
||
private sealed record AnalysisResult( |
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.
moved to its own file.
TExpression, | ||
TIdentifierName> | ||
{ | ||
/// <param name="Property">The property we will make into an auto-property.</param> |
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.
just a move, and added docs.
@@ -2,8 +2,7 @@ | |||
// The .NET Foundation licenses this file to you under the MIT license. | |||
// See the LICENSE file in the project root for more information. | |||
|
|||
#nullable disable | |||
|
|||
using System; |
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.
second meat of the change.
var needsSetter = NeedsSetter(compilation, propertyDeclaration, isWrittenOutsideOfConstructor); | ||
var fieldInitializer = fieldDeclarator.Initializer?.Value; | ||
|
||
if (!isTrivialGetAccessor && !isTrivialSetAccessor && !needsSetter && fieldInitializer == null) |
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.
next, we update the property to use 'accessor list' form if needed.
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.
a lot of the below code changes because it used to be able to just unilaterally assume it would convert directly to get;
or get;set;
. But now we have to actually properly manipulate the individual accessors depending on if we get a simple accessor, or keep a complex one that uses field
within it.
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.
Looks good to me
4111c76
into
dotnet:features/field-keyword
No description provided.