Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no warning for unused let bindings?

Tags:

f#

C# warns for unused variables that are compile-time constants:

static void Main(string[] args)
{
    var unused = "hey"; //CS0219 The variable 'unused' is assigned but its value is never used
    Console.WriteLine("Hello World!");
}

But the F# compiler does not, even though the editor now does pick it up:

enter image description here

If it covered not just compile-time constants but all let bindings, this would have caught a real bug in production caused by a trivial mistake, something like

let callApiXyz connectionInfo = async {
    let fullUrl = sprintf "%s..." connectionInfo.Url
    ...
    let! result = httpGet connectionInfo // fail, didn't use the modified url
    // Should have been:
    // let! result = httpGet { connectionInfo with Url = fullUrl }
    ...
}

Is there any reason not to have this (other than "features are not free")? I feel this should be more important in a functional-first language where expressions tend not to have side-effects, than in C#.

like image 595
Asik Avatar asked Mar 09 '18 18:03

Asik


People also ask

How do I get rid of the unused variable warning?

Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.

What does unused variable mean in C?

No nothing is wrong the compiler just warns you that you declared a variable and you are not using it. It is just a warning not an error. While nothing is wrong, You must avoid declaring variables that you do not need because they just occupy memory and add to the overhead when they are not needed in the first place.


1 Answers

For those of us that don't have Visual Studio and edit the fsproj by hand, the way to implement Tomas's answer is

<PropertyGroup>
    <OtherFlags>$(OtherFlags) --warnon:1182</OtherFlags>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

As an example, see the project file of FSharp.Core itself.

like image 87
Arnavion Avatar answered Oct 19 '22 08:10

Arnavion