CollinAlpert / Lombok.NET

.NET adaptation for Java's Lombok using Source Generators.
MIT License
309 stars 16 forks source link

Generate Properties for Collections that Return a Copy #23

Closed RealDotNetDave closed 1 year ago

RealDotNetDave commented 1 year ago

I haven't used this library yet, but I thought of something that could be very useful. There is a code violation thrown when properties return collections. This violation states that properties that return collections should return a copy/ clone of that collection.

It would be a great addition to this package to auto-generate properties that automatically return a copy/ clone of the collection from a private field.

CollinAlpert commented 1 year ago

I'd say it's high-time to use it then ;)

Could you supply a sample of what you mean, including what should be generated? Also a link to the CS… violation would be helpful, I haven't heard of that one.

RealDotNetDave commented 1 year ago

Here is the link:https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1819 It does say it should return a copy.

for example, this field:

private byte[] _rowVersion;

According to CA1819, it should be changed to a method or the Collectiontype or a copy.

CollinAlpert commented 1 year ago

Thanks for sharing the link! That CA rule is about properties, not fields. But let's take your example as a property, so

private byte[] _rowVersion;

public byte[] RowVersion => _rowVersion;

Then CA1819 would be raised by the property. Are you proposing an attribute placed on _rowVersion which would cause RowVersion to be generated in a way that would not cause CA1819 to be raised?

CollinAlpert commented 1 year ago

Closing due to inactivity. Feel free to reopen.