Minor Bug in CodeCleanup logic exposed by Clone pattern in DAL library: would someone with access please create a bug?
Hello,
I'm working in a Data Access Library that employs the clone pattern. Running code cleanup exposes a bug in the Resharper cleanup logic (as to what is a safe refactor), examples for a simple class below. I'm running Visual Studio 2015 Ultimate (C# .Net 4.6), Windows 10 Enterprise, Resharper version 9.1.3.
Here is the code before cleanup:
namespace Test_Resharper_9_1_3_CodeCleanup_Before
{
using System;
using System.Collections.Generic;
internal class SampleDataTransferObject : ICloneable, IEquatable<SampleDataTransferObject>
{
public object Clone()
{
return new SampleDataTransferObject { Name = Name, Value = Value };
}
public string Name
{
get;
private set;
}
public int Value
{
get;
private set;
}
public override bool Equals(object obj)
{
return Equals(obj as SampleDataTransferObject);
}
public bool Equals(SampleDataTransferObject other)
{
if (other == null)
{
return false;
}
var rv = EqualityComparer<string>.Default.Equals(Name, other.Name) && EqualityComparer<int>.Default.Equals(Value, other.Value);
return rv;
}
public override int GetHashCode()
{
unchecked
{
return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ Value;
}
}
}
}
And the code after cleanup:
namespace Test_Resharper_9_1_3_CodeCleanup_After
{
using System;
using System.Collections.Generic;
internal class SampleDataTransferObject : ICloneable, IEquatable<SampleDataTransferObject>
{
public object Clone()
{
// Bug: Clone is not a .ctor, it does not have access to the private (hidden) auto-property setters that a .ctor would, resulting in a compiler error after code cleanup. (Yes, I could create a 2 parameter .ctor, I just want to run code cleanup, not manually refactor through the library)
return new SampleDataTransferObject { Name = Name, Value = Value };
}
public string Name
{
get;
}
public int Value
{
get;
}
public override bool Equals(object obj)
{
return Equals(obj as SampleDataTransferObject);
}
public bool Equals(SampleDataTransferObject other)
{
if (other == null)
{
return false;
}
var rv = EqualityComparer<string>.Default.Equals(Name, other.Name) && EqualityComparer<int>.Default.Equals(Value, other.Value);
return rv;
}
public override int GetHashCode()
{
unchecked
{
return ((Name?.GetHashCode() ?? 0) * 397) ^ Value;
}
}
}
}
This does not affect me in other projects, but I means I'm manually formatting when working with this one. Would someone with access please put this on the backlog of items to address?
Thank you,
~M
Moderator:
Yes, I know how to file a bug (I'm dealing with OAuth2.0 issues and simply cannot file a bug at this time with JetBrains\Resharper -- that is probably on my end). Feel free to ping me if I've failed to provide sufficient information. I won't code C# without R#, I'd just like to see this fixed in priority order :). I care not if this makes it to the forum. This was my last resort. I hope you know someone that can file a bug on my behalf, I'm counting on you.
Have a great weekend,
~M
Please sign in to leave a comment.