Search code examples
unit-testingf#compiler-warningsreadabilityxunit

Suppress F# Compiler Warning: Possible incorrect indentation: this token is offside of context


I have some xunit tests I would like to layout as follows for readability:

[<Fact>] let ``Has Hash Code 2``()          = target.GetHashCode().ShouldBe 2
[<Fact>] let ``ToString yields two``()      = target.ToString().ShouldBe "two"
[<Fact>] let ``Has underlysing type int``() = target.GetUnderlyingType().ShouldBe typeof<int>

I'm getting a compiler warning on the let statements: "Possible incorrect indentation: this token is offside of context started at position %s. Try indenting this token further or using standard formatting conventions."

I tried #nowarn "lexfltTokenIsOffsideOfContextStartedEarlier" but that just generated another compiler warning "Invalid warning number".

There isn't a warning number listed for this error in https://github.com/fsharp/fsharp/blob/057dbf77df7355321c3c18137c2d552bdfa1272b/src/fsharp/FSComp.txt

Is there a way to suppress this warning?


Solution

  • I apologize for answering in a style, "you don't need it", but this seems to be the case. Also, this question apparently becomes a FAQ for those who write Unit Tests in F#. Yet another case is for [<Literal>]'s.

    Anyway, disabling warnings on a project- or even file-level seems to be a bad idea, unless you have a good reason to do that.

    As far as I understood, you'd like to have the statements to be one-liners (e.g., [<Fact>] did not occupy an entire line). There are two ways to accomplish that in VS2013:

    1. Place the attribute inside let:

      let [<Fact>] ``Has Hash Code 2``()          = target.GetHashCode().ShouldBe 2
      
    2. Write a double-semicolon after the declaration:

      [<Fact>] let ``Has Hash Code 2``()          = target.GetHashCode().ShouldBe 2;;