-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Restrict some types in object initializer setters when not directly settable #75507
Open
Rekkonnect
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
Rekkonnect:fix/74331-recommend-settable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,13 +119,60 @@ private static bool CanSupportObjectInitializer(ISymbol symbol) | |
|
||
if (symbol is IFieldSymbol fieldSymbol) | ||
{ | ||
return !fieldSymbol.Type.IsStructType(); | ||
return MemberTypeCanSupportObjectInitializer(fieldSymbol.Type); | ||
} | ||
else if (symbol is IPropertySymbol propertySymbol) | ||
{ | ||
return !propertySymbol.Type.IsStructType(); | ||
return MemberTypeCanSupportObjectInitializer(propertySymbol.Type); | ||
} | ||
|
||
throw ExceptionUtilities.Unreachable(); | ||
} | ||
|
||
private static bool MemberTypeCanSupportObjectInitializer(ITypeSymbol type) | ||
{ | ||
// NOTE: While in C# it is legal to write 'Member = {}' on a member of any of | ||
// the ruled out types below, it has no effects and is thus a needless recommendation | ||
|
||
// We avoid some types that are common and easy to rule out | ||
switch (type.SpecialType) | ||
{ | ||
case SpecialType.System_Enum: | ||
case SpecialType.System_String: | ||
case SpecialType.System_Object: | ||
case SpecialType.System_Delegate: | ||
case SpecialType.System_MulticastDelegate: | ||
|
||
// We cannot use collection initializers in Array members, | ||
// but for members of an array type with a typed rank we can | ||
// For example, assuming Array2D is int[,]: | ||
// Array2D = { [0, 0] = value, [0, 1] = value1 }, | ||
case SpecialType.System_Array: | ||
|
||
// We cannot add to an enumerable or enumerator | ||
// so we cannot use a collection initializer | ||
case SpecialType.System_Collections_IEnumerable: | ||
case SpecialType.System_Collections_IEnumerator: | ||
return false; | ||
} | ||
|
||
if (type is INamedTypeSymbol { IsGenericType: true } named) | ||
{ | ||
var definition = named.OriginalDefinition; | ||
switch (definition.SpecialType) | ||
{ | ||
case SpecialType.System_Collections_Generic_IEnumerable_T: | ||
case SpecialType.System_Collections_Generic_IEnumerator_T: | ||
return false; | ||
} | ||
} | ||
|
||
// - Delegate types have no settable members, which is the case for Delegate and MulticastDelegate too | ||
// - Non-settable struct members cannot be used in object initializers | ||
// - Pointers and function pointers do not have accessible members | ||
return !type.IsDelegateType() | ||
&& !type.IsStructType() | ||
&& !type.IsFunctionPointerType() | ||
&& type.TypeKind != TypeKind.Pointer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these need explanations. |
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
these need explanations.