roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

Detecting record fields than don’t have value semantics

Open lookbusy1344 opened this issue 2 years ago • 1 comments
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?

lookbusy1344 avatar Sep 26 '23 21:09 lookbusy1344

I wrote one myself that seems to work ok: https://github.com/lookbusy1344/RecordValueAnalyser

lookbusy1344 avatar Oct 05 '23 20:10 lookbusy1344