Skip to content

Commit 6d922f4

Browse files
authored
Merge PR #689 from Mersho/FavourStaticEmptyFieldsFalseNegative
Fix FavourStaticEmptyFields false negatives.
2 parents acd40c5 + aa2c802 commit 6d922f4

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/FSharpLint.Core/Rules/Conventions/FavourStaticEmptyFields.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ let private runner (args: AstNodeRuleParams) =
3939
let emptyLiteralType =
4040
if isArray then EmptyArrayLiteral else EmptyListLiteral
4141
generateError range emptyLiteralType
42+
| AstNode.Expression(SynExpr.Record(_, _, synExprRecordField, _)) ->
43+
synExprRecordField
44+
|> List.map (fun field ->
45+
match field with
46+
| SynExprRecordField(_, _, expr, _) ->
47+
match expr with
48+
| Some(SynExpr.ArrayOrList(isArray, [], range)) ->
49+
let emptyLiteralType = if isArray then EmptyArrayLiteral else EmptyListLiteral
50+
generateError range emptyLiteralType
51+
| Some(SynExpr.Const (SynConst.String ("", _, range), _)) ->
52+
generateError range EmptyStringLiteral
53+
| _ -> Array.empty)
54+
|> Array.concat
4255
| _ -> Array.empty
4356

4457

tests/FSharpLint.Core.Tests/Rules/Conventions/FavourStaticEmptyFields.fs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,33 @@ let foo a =
7777
Assert.IsTrue this.ErrorsExist
7878
Assert.IsTrue (this.ErrorMsg.Contains "Array.empty")
7979

80+
[<Test>]
81+
member this.FavourStaticEmptyFieldsShouldProduceError10() =
82+
this.Parse """
83+
type Person =
84+
{
85+
FirstName: string
86+
Nicknames: List<string>
87+
}
88+
89+
{ FirstName = "Foo"; Nicknames = [] } |> ignore"""
90+
91+
Assert.IsTrue this.ErrorsExist
92+
Assert.IsTrue (this.ErrorMsg.Contains "List.Empty")
93+
94+
[<Test>]
95+
member this.FavourStaticEmptyFieldsShouldProduceError11() =
96+
this.Parse """
97+
type Person =
98+
{
99+
FirstName: string
100+
}
101+
102+
{ FirstName = "" } |> ignore"""
103+
104+
Assert.IsTrue this.ErrorsExist
105+
Assert.IsTrue (this.ErrorMsg.Contains "String.Empty")
106+
80107
[<Test>]
81108
member this.FavourStaticEmptyFieldsShouldNotProduceError1() =
82109
this.Parse "let bar = String.Empty"

0 commit comments

Comments
 (0)