Daniel
Daniel

Reputation: 47914

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.

Upvotes: 16

Views: 3069

Answers (2)

jbtule
jbtule

Reputation: 31809

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)))

Upvotes: 1

Brian
Brian

Reputation: 118895

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.)

Upvotes: 10

Related Questions