3

In the following example, Resharper complains about DoA() method never being used, despite being implemented by the 'AImplementator' class:

namespace Test
{
    public interface A
    {
        // Method 'DoA' is never used
        void DoA();
    }

    public class AImplementator: A
    {
        public void DoA()
        {
            throw new System.NotImplementedException();
        }
    }
}

I can't understand this behavior, since the interface IS being used.

  1. Why this happens?
  2. How can I fix this warning?

Obs: I can supress the warning using comments or the [UsedImplicitly] attribute. But neither of these options seems to be correct given the situation. I'm using Resharper 9.1.

ulrichb
  • 19,610
  • 8
  • 73
  • 87
Genos
  • 413
  • 4
  • 12
  • 2
    DoA is a method not a property. Aside that, I've just tried this and it doesn't raise a warning regarding `DoA` is never used. Can you provide a screenshot? The only warning I get is to rename `interface A` to `interface IA`. – Darren Jun 05 '15 at 19:05
  • Can't repeat this warning with 8.2. `DoA()` is a method of course. – StuartLC Jun 05 '15 at 19:05
  • Implementing it in a class is not considered as using it. That makes perfect sense. – Lex Li Jun 05 '15 at 23:40
  • Doesn't make perfect sense to me. Its a member of a public interface. Resharper has no way of knowing if the i/f member is called nowhere or in millions of developer projects worldwide. Possibly a bug in Resharper? Not even Jetbrains are perfect! – Stephen Kennedy Jun 06 '15 at 21:20

2 Answers2

1

This warning will only be displayed when the Solution-Wide Analysis is enabled.

The warning is about the usage (in this case e.g. a call) of the interface method. The following example should demonstrate it (note the var vs. interface as local variable type).

var instance = new AImplementator();
// Does NOT make A.DoA() "used":
instance.DoA();

A instanceAsInterface = new AImplementator();
// DOES make A.DoA() "used":
instanceAsInterface.DoA();
ulrichb
  • 19,610
  • 8
  • 73
  • 87
1

To answer other people going here.

If you do have resharper 10.

you can use jetrains annotatoins:

putting the attribute: [UsedImplicitly] above the class will suffice.

What is great to know is that it also works putting it on the base class.

In this case Putting it on interface A will fix the message showing up for all subclasses (great for ioc implementations)

namespace Test
{
    [JetBrains.Annotations.UsedImplicitly]
    public interface A
    {
        // Method 'DoA' is never used
        void DoA();
    }

    public class AImplementator: A
    {
        public void DoA()
        {
            throw new System.NotImplementedException();
        }
    }
}

Tests

Best solution would be to write unit tests to cover these functions. The advantage would be that also your specs are covered.

Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • The reason why this is a bad solution, is that it is "used implicitly" until it is not. So if someone goes and removes the single implementation, then it really IS never used, and the warning is hidden by the annotation – JumpingJezza Dec 02 '21 at 03:06
  • @JumpingJezza very good point. If you are building a library i think this is the only way. unless you write tests :) – Joel Harkes Dec 02 '21 at 10:46