Bagzli
Bagzli

Reputation: 6569

Resharper Method is never used

I have a public facing interface in one project that will end up becoming a nuget package. Here is the code:

public interface MyInterface
{
    void MyMethod();
}

Now every method in this interface is not used in this library but is used in other via the package. However, every single method here has a warning of Method x is never used.

Is there a simple way to tell ReSharper to ignore every public method in every project in every solution that I ever open to not perform this check in public methods.

I have a couple of ways to fix this but it is far too tedious.

Option 1: [UsedImplicitly] on every method inside my class. This is far too tedious in my opinion. Option 2: [SuppressMessage("ReSharper", "UnusedMember.Global")] A little less tedious, but still have to implement on every class.

There has to be a better way?

I also read somewhere that this only happens if Enable solution-wide analysis is checked. In my case it is not. I found this setting under Reshaper Options > Code Inspection > Settings

Upvotes: 6

Views: 3891

Answers (3)

Josh Gallagher
Josh Gallagher

Reputation: 5329

Taken from @Matthias's comment on the original question, for deliberately public methods that are intended for export from your library, use the [PublicAPI] attribute on the class from the JetBrains.Annotations package and namespace:

using JetBrains.Annotations;

[PublicAPI]
public interface MyInterface
{
    void MyMethod();
}

This is not an ideal answer to the original question as it is just as tedious as applying [UsedImplicitly] onto every class. However, for me it has the advantage over a blanket disabling of the inspection as it will point out places where I have made an error in making a method or class public.

It also has an advantage over [UsedImplicitly] as it is a positive declaration of the intent that this class is to supposed to used by consumers of the library. [UsedImplicitly], in contrast, would indicate to me that the method/class was used somehow (e.g. de/serialization) with the context of the current solution.

Upvotes: 2

Alexander Kurakin
Alexander Kurakin

Reputation: 13523

In your case, since you've mentioned that SWEA is disabled, I would recommend disabling the following checkbox ReSharper | Options | Code Inspection | Settings | General | Show unused non-private type members when solution-wide analysis is off.

Upvotes: 6

fondente
fondente

Reputation: 25

Solution #1

You could use

 // ReSharper disable UnusedMember.Global

If you put this before the method you disable the warning for that method...

public interface IMyInterface
{
    // ReSharper disable once UnusedMember.Global
    void MyMethod();
}

If you put it before the namespace declaration you disable the warning for the entire file.

Otherwise you can do like this, so you disable the warning for the interface:

[SuppressMessage("ReSharper", "UnusedMember.Global")]
public interface IMyInterface
{
    void MyMethod();
}

Solution #2

The last solution you can take is to disable this kind of message in the Resharper Options unchecking Code Inspection -> Compiler Warnings -> Field is never used.

I guess is the only solution that solve your problem withoud passing through all the classes.

Upvotes: 0

Related Questions