Suppressing "Is Never Used" and "Is Never Assigned To" Warnings in C#

Suppressing is never used and is never assigned to warnings in C#

Yes, these can be suppressed.

Normally, I'm opposed to suppressing warnings, but in this case, structs used for interop absolutely requires some fields to be present, even though you never intend to (or can) use them, so in this case I think it should be justified.

Normally, to suppress those two warnings, you would fix the offending code. The first ("... is never used") is usually a code-smell of leftovers from earlier versions of the code. Perhaps code was deleted, but fields left behind.

The second is usually a code-smell for incorrectly used fields. For instance, you might incorrectly write the new value of a property back to the property itself, never writing to the backing field.


To suppress warnings for "Field XYZ is never used", you do this:

#pragma warning disable 0169
... field declaration
#pragma warning restore 0169

To suppress warnings for "Field XYZ is never assigned to, and will always have its default value XX", you do this:

#pragma warning disable 0649
... field declaration
#pragma warning restore 0649

To find such warning numbers yourself (ie. how did I know to use 0169 and 0649), you do this:

  • Compile the code as normal, this will add some warnings to your error list in Visual Studio
  • Switch to the Output window, and the Build output, and hunt for the same warnings
  • Copy the 4-digit warning code from the relevant message, which should look like this:

    C:\Dev\VS.NET\ConsoleApplication19\ConsoleApplication19\Program.cs(10,28):
    warning CS0649: Field 'ConsoleApplication19.Program.dwReserved' is never
    assigned to, and will always have its default value 0


Caveat: As per the comment by @Jon Hanna, perhaps a few warnings is in order for this, for future finders of this question and answer.

  • First, and foremost, the act of suppressing a warning is akin to swallowing pills for headache. Sure, it might be the right thing to do sometimes, but it's not a catch-all solution. Sometimes, a headache is a real symptom that you shouldn't mask, same with warnings. It is always best to try to treat the warnings by fixing their cause, instead of just blindly removing them from the build output.
  • Having said that, if you need to suppress a warning, follow the pattern I laid out above. The first code line, #pragma warning disable XYZK, disables the warning for the rest of that file, or at least until a corresponding #pragma warning restore XYZK is found. Minimize the number of lines you disable these warnings on. The pattern above disables the warning for just one line.
  • Also, as Jon mentions, a comment as to why you're doing this is a good idea. Disabling a warning is definitely a code-smell when done without cause, and a comment will prevent future maintainers from spending time either wondering why you did it, or even by removing it and trying to fix the warnings.

Suppressing is never used warning for events in C#

The reason this warning is raised is because by default events will generate fields. This makes the object larger which is undesirable if you never raise the event. To work around this use explicit add/remove methods for the event and do nothing in them:

interface I { event EventHandler E; }

class C : I
{
public event EventHandler E { add { } remove { } }
}

Suppress Member is never assigned to warning in C#

How to cleanly suppress these warnings in my code

An alternative to an inappropriate assignment would be to just a #pragma:

#pragma warning disable 0649 // Assigned by reflection
ViewPortViewModel _Trochoid;
#pragma warning restore 0649

That should work, and it keeps the ugliness at exactly the place that it makes sense to document it - at the field declaration.

If you have multiple fields handled in the same way, you could put them all in the same "block" of disabled warnings, with a comment applicable to all of them.

Whether you view this as "clean" or not is a matter of taste, of course. I think I prefer it to assignments which are only there for the side-effect of removing the warnings.

Resharper attribute to disable never assigned but not never used warnings

Oh, there are parameters to the MeansImplicitUse attribute that specify the type of use, so I actually want to add [MeansImplicitUse(ImplicitUseKindFlags.Assign)] to my attribute definition. Simple!

Can I make an attribute suppress warnings?

You can assign a default value, like null. That will make the warning disappear. Your assignment and also inspector assignments will overwrite the default value later on.

[Assign(AssignmentTime.Awake)]
private Rigidbody _rigidBody = null;

Visual studio warning: x is never assigned to

Is there a way to make Visual Studio stop warning about this?

The best way would be to understand the warning and fix the bug that it indicates.

If you never assign to _name this means that reading from _name will always return null. Is that really what you intend?

You can also suppress this warning.

Also note, that ?? simply computes a new value from its operands. It does not assign a new value to anything.

Suppress warning when attribute is being used - C# (Unity)

You should be able to stop the warning by adding the MeansImplicitUseAttribute to your GetComponentAttribute class.

using System;
using JetBrains.Annotations;

[MeansImplicitUse(ImplicitUseKindFlags.Assign)]
public class GetComponentAttribute : Attribute
{

}

Visual Studio 2010 Pro - SuppressMessage

The CSxxxx warnings you are seeing are C# compiler warnings, not FxCop/Code Analysis warnings. They must be suppressed using #pragma warning disable directives, not SuppressMessage attributes.

Incidentally, integrated Code Analysis is only availabe in Premium or Ultimate, not Pro.



Related Topics



Leave a reply



Submit