Skip to content

Commit 01c9bc9

Browse files
authored
Merge PR #697 from Mersho/FalseNegativeAvoidSinglePipeOperator
Fix AvoidSinglePipeOperator false negatives (in all inner expressions).
2 parents 08ceae7 + 8a93684 commit 01c9bc9

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ let runner (args: AstNodeRuleParams) =
1717
TypeChecks = List.Empty
1818
} |> Array.singleton
1919

20-
let checkExpr (expr: SynExpr) =
20+
let rec checkExpr (expr: SynExpr) (parentList: AstNode list): WarningDetails array =
21+
let checkParentPiped (expr: AstNode) =
22+
match expr with
23+
| AstNode.Expression(SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, _argExpr, _range)) ->
24+
checkExpr funcExpr [] |> Seq.isEmpty
25+
| _ -> false
26+
2127
match expr with
2228
| SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, argExpr, _range) ->
2329
match funcExpr with
@@ -29,7 +35,14 @@ let runner (args: AstNodeRuleParams) =
2935
| SynExpr.IfThenElse _ ->
3036
Array.empty
3137
| _ ->
32-
errors ident.idRange
38+
let isParentPiped =
39+
match parentList with
40+
| head::_ -> checkParentPiped head
41+
| [] -> false
42+
if isParentPiped then
43+
Array.empty
44+
else
45+
errors ident.idRange
3346
else
3447
Array.empty
3548
| _ ->
@@ -39,14 +52,8 @@ let runner (args: AstNodeRuleParams) =
3952

4053
let error =
4154
match args.AstNode with
42-
| AstNode.Binding (SynBinding(_synAcc, _synBinding, _mustInline, _isMut, _synAttribs, _preXmlDoc, _synValData, _headPat, _synBindingRet, synExpr, _range, _debugPointAtBinding, _)) ->
43-
match synExpr with
44-
| SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, _argExpr, _range) ->
45-
checkExpr funcExpr
46-
| SynExpr.IfThenElse(_, SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, _argExpr, _range), _, _, _, _, _) ->
47-
checkExpr funcExpr
48-
| _ ->
49-
Array.empty
55+
| AstNode.Expression(SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, _argExpr, _range)) ->
56+
checkExpr funcExpr (args.GetParents args.NodeIndex)
5057
| _ ->
5158
Array.empty
5259

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ let foo param =
106106
Assert.True this.NoErrorsExist
107107

108108
[<Test>]
109-
member this.``Use pipe operator once on record``() =
109+
member this.``Use pipe operator once on record within a if statement``() =
110110
this.Parse """
111111
type Person =
112112
{
@@ -123,7 +123,7 @@ let someFunc someParam =
123123
Assert.IsTrue this.ErrorsExist
124124

125125
[<Test>]
126-
member this.``Use pipe operator twice on record``() =
126+
member this.``Use pipe operator twice on record within a if statement``() =
127127
this.Parse """
128128
type Person =
129129
{
@@ -140,3 +140,62 @@ let someFunc someParam =
140140
"""
141141

142142
Assert.IsTrue this.NoErrorsExist
143+
144+
[<Test>]
145+
member this.``Use pipe operator once inside of an array``() =
146+
this.Parse """
147+
let someFunc () =
148+
[| "Foo" |> String.replicate 2 |]
149+
"""
150+
151+
Assert.IsTrue this.ErrorsExist
152+
153+
[<Test>]
154+
member this.``Use pipe operator once on record within a nested if statement``() =
155+
this.Parse """
156+
type Person =
157+
{
158+
FirstName: string
159+
}
160+
161+
let someFunc someParam barParam =
162+
if someParam then
163+
if barParam then
164+
{ FirstName = "Bar" } |> someOtherFunc
165+
else
166+
Array.empty
167+
else
168+
Array.empty
169+
"""
170+
171+
Assert.IsTrue this.ErrorsExist
172+
173+
[<Test>]
174+
member this.``Use pipe operator once without binding``() =
175+
this.Parse """
176+
module Foo
177+
178+
-1.0 |> printf "%d"
179+
"""
180+
181+
Assert.IsTrue this.ErrorsExist
182+
183+
[<Test>]
184+
member this.``Use pipe operator twice without binding``() =
185+
this.Parse """
186+
module Foo
187+
188+
-1.0 |> printf "%d" |> ignore
189+
"""
190+
191+
Assert.IsTrue this.NoErrorsExist
192+
193+
[<Test>]
194+
member this.``Use pipe operator thrice without binding``() =
195+
this.Parse """
196+
module Foo
197+
198+
-1.0 |> printf "%d" |> ignore |> someOtherFunc
199+
"""
200+
201+
Assert.IsTrue this.NoErrorsExist

0 commit comments

Comments
 (0)