fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Query expressions and --warnon:1182

Open Tarmil opened this issue 10 years ago • 5 comments

In query expressions, variables bound using for are not recognized as used if they are not selected, even if they are otherwise used for example in a where or a let.

Example:

// Expected: no warning
// Actual:   warning FS1182: The value 'x' is unused
query { for x in [1;2;3] do
        where (x > 2)
        select 1 }

// Expected: no warning
// Actual:   warning FS1182: The value 'x' is unused
query { for x in [1;2;3] do
        let y = x
        select y }

// Expected: no warning
// Actual:   no warning
query { for x in [1;2;3] do
        where (x > 2)
        select x }

Tested on F# 3.1.2 and 4.0-RC.

Tarmil avatar May 06 '15 20:05 Tarmil

Yes, I do recall this, it's good to have it recorded (and yes, this was one of the reasons /warn:1182 was made opt-in)

It is (unsurprisingly) related to the way computation expressions are de-sugared, which makes duplicates of the "x".

dsyme avatar May 07 '15 18:05 dsyme

are there any recommended work-a-rounds besides turning off 1182, or warnaserror for 1182?

ImaginaryDevelopment avatar May 22 '15 19:05 ImaginaryDevelopment

You can use standard convention of prefixing the identifier with _, which tells the compiler to ignore that identifier when computing 1182.

query { for _x in [1;2;3] do
        where (_x > 2)
        select 1 }

latkin avatar May 22 '15 20:05 latkin

What's interesting is that VFT does recognize them as the same identifier: F12 takes you to the for line in all cases.

Tarmil avatar May 23 '15 10:05 Tarmil

Observed in VS 2022, v 17.8.4. I know I'm most probably not adding useful information. Just in case I am.

BentTranberg avatar Jan 16 '24 08:01 BentTranberg