roslynator
roslynator copied to clipboard
Detecting record fields than don’t have value semantics
trafficstars
Is it possible to check records for field types that lack a customised Equals override, or that use reference equality? These fields can cause subtle bugs, and I’m not aware of any way to detect this at present. Eg
public record Info(DirectoryInfo Folder, string Name);
var a = new Info(new DirectoryInfo("."), "test");
var b = new Info(new DirectoryInfo("."), "test");
var same = a == b; // false!
DirectoryInfo has reference semantics, so isn’t suitable for use in records without a custom Equals check.
Is it possible to create a warning for this kind of issue?
I wrote one myself that seems to work ok: https://github.com/lookbusy1344/RecordValueAnalyser