Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoWarn not working in DNX

In my test project, I've got private fields that are not assigned to in the code, but are assigned with reflection.

When compiling I get warnings like:

Warning CS0649 Field 'CLASSNAME.FIELDNAME' is never assigned to, and will always have its default value null

I've tried adding <NoWarn>649</NoWarn> to the first PropertyGroup in the xproj. But I still get the errors.

Does NoWarn not work in DNX? Or am I doing something wrong? Is there any other solution to the problem?

like image 851
Allrameest Avatar asked Jun 25 '15 06:06

Allrameest


People also ask

How do I suppress a warning in nowarn?

From the right-click or context menu, select Properties. In the NoWarn box of the package's properties, enter the warning number you want to suppress for this package. If you want to suppress more than one warning, use a comma to separate the warning numbers.

How do I get rid of nowarn warnings in Visual Basic?

On the menu bar, choose Project > Reload Project. On the menu bar, choose Build > Rebuild Solution. The Output window no longer shows the warnings that you specified. For more information, see the /nowarn compiler option for the Visual Basic command-line compiler.

How do I enable nowarn in Visual Studio code editor?

In Solution Explorer, open the right-click or shortcut menu for the project, and then choose Edit <ProjectName>.vbproj. The XML project file opens in the code editor. Locate the <NoWarn> element for the build configuration you're building with, and add one or more warning numbers as the value of the <NoWarn> element.


1 Answers

Sample code:

class Example {
    private string warningHere;    // CS0649
    void UseField() {
        Console.WriteLine(warningHere);
    }
}

You have to convince the compiler that you know what you're doing, it refuses to consider the possibility that you use Reflection to poke a value into the field. That's pretty simple to do:

    private string warningHere = null;  // Fine

You might object "But that's completely pointless! The CLR already initializes the field to null!". Which is certainly true. No harm done however, eliminating superfluous code like this is the job of the jitter optimizer. It is particularly good at removing needless null assignments.


I could have <NoWarn>0649</NoWarn> in the csproj

Do keep in mind that this is equivalent to solving a local problem with a global sledgehammer. This warning is pretty important, you want to have it in effect for all the code you compile. Just to demonstrate, in the above snippet change class to struct, keep the reflection code the same. And note that you cannot get give that warningHere field a value. A side-effect of the struct getting boxed before it is passed to FieldInfo.SetValue(), only the boxed copy is updated. That's a nasty bug to diagnose if you don't have a warning to alert you.

Using #pragma warning in the source is okayish, but not superior, too easy to forget to restore it.

The project switched to building with MSBuild just two months ago, do make sure your pull isn't too old and that you switched as well. You can file a bug at github to remind them if the feature is still awol. And do consider scratching that itch if you can't wait, fix it yourself. The ultimate benefit of an open source project :)

like image 103
Hans Passant Avatar answered Sep 17 '22 09:09

Hans Passant