Skip to content

Commit

Permalink
Fix source generator including unused unsafe accessors (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis authored Dec 9, 2024
1 parent a6f44fa commit bb13320
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,30 @@ private static void FormatMemberAccessors(SourceWriter writer, ObjectShapeModel
FormatPropertyGetterAccessor(writer, objectShapeModel, property);
}

if (property is { EmitSetter: true, IsSetterAccessible: false } ||
(objectShapeModel.Constructor is not null && property.IsInitOnly))
if ((!property.IsSetterAccessible || property.IsInitOnly) &&
(property.EmitSetter || IsUsedByConstructor(property)))
{
writer.WriteLine();
FormatPropertySetterAccessor(writer, objectShapeModel, property);
}

bool IsUsedByConstructor(PropertyShapeModel property)
{
if (objectShapeModel.Constructor is not {} ctor)
{
return false;
}

foreach (var parameter in ctor.RequiredMembers.Concat(ctor.OptionalMembers))
{
if (parameter.Name == property.Name)
{
return true;
}
}

return false;
}
}
}

Expand Down

0 comments on commit bb13320

Please sign in to comment.