fsharp
fsharp copied to clipboard
Query expressions and --warnon:1182
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.
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".
are there any recommended work-a-rounds besides turning off 1182, or warnaserror for 1182?
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 }
What's interesting is that VFT does recognize them as the same identifier: F12 takes you to the for line in all cases.
Observed in VS 2022, v 17.8.4. I know I'm most probably not adding useful information. Just in case I am.