Skip to content

Query expressions and --warnon:1182 #422

Closed

Description

@Tarmil

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.

Activity

  1. dsyme commented on May 7, 2015

    @dsyme
    Contributor

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

  2. ImaginaryDevelopment commented on May 22, 2015

    @ImaginaryDevelopment

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

  3. latkin commented on May 22, 2015

    @latkin
    Contributor

    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 }
  4. Tarmil commented on May 23, 2015

    @Tarmil
    ContributorAuthor

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

  5. added
    Impact-Low
    on Jan 9, 2016
  6. added
    Impact-Medium
    and removed
    Impact-Low
    on Aug 8, 2017
  7. added
    Impact-Low
    and removed
    Impact-Medium
    on Nov 17, 2017
  8. added this to the Unknown milestone on Aug 25, 2018
  9. removed this from the Unknown / not bug milestone on May 23, 2019
  10. 8 remaining items

  11. BentTranberg commented on Jan 16, 2024

    @BentTranberg

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

    edit on 14th Nov 2025:

    Observed in VS 2022, v 17.14.20, as shown here, when using linq2db :
    (also see it in Insiders, as of today)

    let getPackagesInJob (jobId: JobId) : EntPakke list =
        let q = query {
            for pakke in dbTre.Pakke do // <- FS1182 The value 'pakke' is unused / in "for pakke"
            join register in dbTre.Register on (pakke.RegisterId = register.Id) // <- FS1182 The value 'register' is unused / in "join register"
            where (register.JobId = jobId)
            select pakke
        }
        q.ToListAsync().Result |> Seq.toList
    

    Example of a query that does not provoke this problem:

    let getPackage (id: PakkeId) : EntPakke option =
        let q = query {
            for pakke in dbTre.Pakke do
            where (pakke.Id = id)
            select pakke
            }
        q.SingleOrDefaultAsync().Result |> Option.ofObj
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-QueriesBugImpact-Low

    Type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions