History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: RSRP-42842
Type: Bug Bug
Status: Open Open
Priority: Major Major
Assignee: Eugene Pasynkov
Reporter: Corey Kosak
Votes: 1
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
ReSharper

Gives bad advice: "Parameter can be of type System.ICloneable"

Created: 10 Jun 07 19:44   Updated: 11 Jun 07 20:33
Component/s: Code Analysis
Fix Version/s: Mirabilie Futurum
Security Level: Everybody (All jira users)

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown

Build: 448


 Description  « Hide
(build 3.0.448.7)

I believe I can guess why it thinks so, but in this case it would not be good programming style to change the parameter to ICloneable, because anything but a Cow will make the routine fail. I think R# needs some more sophisticated analysis to understand that the input argument is basically constrained to being a Cow.

using System;

namespace ConsoleApplication1 {
  public class Program {
    public static void Main(string[] args) {
      Cow c1=new Cow();
      Cow c2=c1.CopyCow(c1);
    }

    private class Cow : ICloneable {
      public object Clone() {
        return this.MemberwiseClone();
      }

      public Cow CopyCow(Cow other) { //R# says: "Parameter can be of type System.ICloneable"
        return (Cow)other.Clone();
      }
    }
  }
}


 All   Comments   Work Log   Change History      Sort Order:
Vladimir Reshetnikov - 10 Jun 07 21:56
I would recommend to use the following approach:
public T Copy<T>(T other) where T : ICloneable // No warnings here
{
    return (T) other.Clone();
}

Corey Kosak - 10 Jun 07 22:32
It is possible my example was poorly chosen. Usually when I provide examples to you I try to create something as simple as possible that illustrates the problem.

There are always ways to work around a problem. Likewise there are always ways to get ReSharper to stop warning about something. This does not mean that ReSharper's warnings are always valid.

Here is another version of the code. I think that it is the best way to write it. If you are saying I should write longer code just to get ReSharper to stop warning about ICloneable, I would have to disagree.

using System;
using System.Collections;

namespace ConsoleApplication1 {
  public class MyClass {
    private readonly Hashtable ht;
    private readonly OperatingSystem os;

    public MyClass(Hashtable ht, OperatingSystem os) {
      this.ht=(Hashtable)ht.Clone();
      this.os=(OperatingSystem)os.Clone();
    }
  }
}

Eugene Pasynkov - 11 Jun 07 20:33
Right now, I do not know what to do with this request.
On suggesting parameter type, except code analysis, we do some simple heuristics.
This example shows that they are not enough