Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable compiler warnings per line

Is it possible to disable compiler warnings for specific lines?

In C#, this works:

[Obsolete]
class Old { }

#pragma warning disable 612
    var oldWithoutWarning = new Old();
#pragma warning restore 612
    var oldWithWarning = new Old();

This would be very useful for disabling incomplete pattern matches warnings, especially when a function accepts a particular case of a DU.

like image 589
Daniel Avatar asked Aug 29 '11 16:08

Daniel


People also ask

How do you suppress Code Analysis warnings?

You can suppress violations in code using a preprocessor directive, the #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code. Or, you can use the SuppressMessage attribute.

How do I turn off GCC warnings?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

How do you suppress all warnings in C++?

To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction. Now let's dive into the code for each compiler.


2 Answers

No, the warnings are turned off per-file (or possibly 'from here to the bottom of the file') when using #nowarn. (Or per compilation/project when using project properties / --nowarn command-line.)

like image 114
Brian Avatar answered Sep 30 '22 05:09

Brian


Since everything is an expression in F# it's not hard to pull out a line or a part of a line and put it in it's own file.

Example of my issue, where :: pattern matching warned about empty list possiblity, but my state passed to Seq.fold always has a list with at least one item.

module FoldBookmarks
#nowarn "25"

let foldIntoBookmarks: (string * int * int) seq -> XamlReport.PDF.Bookmark seq = 
        Seq.fold (fun ((tl,pl,acc)::l) (t,p,_) -> (t,acc,p+acc)::((tl,pl,acc)::l)) [("",0,1)]
        >> Seq.map(fun (x,y,_) -> PDF.Bookmark(Title=x, PageNumber= System.Nullable(y)))
like image 21
jbtule Avatar answered Sep 30 '22 04:09

jbtule