Skip to content

Commit 1884542

Browse files
authored
Core(,.Tests): fix hint's (FL0065) false positive
It was detecting named boolean arguments as comparison to `true`. Fixes #492
1 parent d1e9fae commit 1884542

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

src/FSharpLint.Core/Rules/Hints/HintMatcher.fs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,30 +229,35 @@ module private MatchExpression =
229229
match (leftExpr, opExpr) with
230230
| ExpressionUtilities.Identifier([ident], _), ExpressionUtilities.Identifier([opIdent], _) when opIdent.idText = "op_Equality" ->
231231
match arguments.FSharpCheckFileResults with
232-
| Some(checkFile) ->
233-
let checkSymbol () =
234-
let symbolUse =
235-
checkFile.GetSymbolUseAtLocation(
236-
ident.idRange.StartLine, ident.idRange.EndColumn, String.Empty, [ident.idText])
237-
238-
match symbolUse with
239-
| Some(symbolUse) ->
232+
| Some checkFile ->
233+
let symbolUse =
234+
checkFile.GetSymbolUseAtLocation(
235+
ident.idRange.StartLine, ident.idRange.EndColumn, String.Empty, [ident.idText])
236+
237+
match symbolUse with
238+
| Some symbolUse ->
239+
let checkSymbol () =
240240
match symbolUse.Symbol with
241241
| :? FSharpParameter
242242
| :? FSharpField -> false
243243
| :? FSharpMemberOrFunctionOrValue as element -> not element.IsProperty
244244
| _ -> true
245-
| None -> true
246-
checkSymbol
247-
|> List.singleton
248-
|> Match
245+
checkSymbol
246+
|> List.singleton
247+
|> Match
248+
| None ->
249+
// Symbol resolution failed, fall back to breadcrumb checking
250+
match filterParens arguments.Breadcrumbs with
251+
| PossiblyInMethod
252+
| PossiblyInConstructor -> NoMatch
253+
| _ -> Match List.Empty
249254
| None ->
250255
/// Check if in `new` expr or function application (either could be a constructor).
251256
match filterParens arguments.Breadcrumbs with
252257
| PossiblyInMethod
253258
| PossiblyInConstructor -> NoMatch
254-
| _ -> Match(List.Empty)
255-
| _ -> Match(List.Empty)
259+
| _ -> Match List.Empty
260+
| _ -> Match List.Empty
256261

257262
[<TailCall>]
258263
let rec matchHintExpr (continuation: unit -> HintMatch) arguments =
@@ -386,7 +391,10 @@ module private MatchExpression =
386391
matchHintExpr
387392
(fun () ->
388393
matchHintExpr
389-
(fun () -> arguments.SubHint(AstNode.Expression(rightExpr), right) |> matchHintExpr returnEmptyMatch)
394+
(fun () ->
395+
matchHintExpr
396+
(fun () -> notPropertyInitialisationOrNamedParameter arguments leftExpr opExpr)
397+
(arguments.SubHint(AstNode.Expression(rightExpr), right)))
390398
(arguments.SubHint(AstNode.Expression(leftExpr), left)))
391399
(arguments.SubHint(AstNode.Expression(opExpr), op))
392400
| (AstNode.Expression(SynExpr.App(_, _, infixExpr, rightExpr, _)),

tests/FSharpLint.Core.Tests/Rules/Hints/HintMatcher.fs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,41 @@ do
754754

755755
this.AssertNoWarnings()
756756

757+
/// Regression test for: https://github.com/fsprojects/FSharpLint/issues/492
758+
[<Test>]
759+
member this.``Named parameter in atomic static method call should not be treated as infix operation``() =
760+
this.SetConfig(["x = true ===> x"])
761+
762+
this.Parse """
763+
module Goat
764+
765+
type Foo() =
766+
static member Create(keepAssemblyContents: bool) = Foo()
767+
768+
do
769+
let foo = Foo.Create(keepAssemblyContents = true)
770+
()
771+
"""
772+
773+
this.AssertNoWarnings()
774+
775+
/// Regression test for: https://github.com/fsprojects/FSharpLint/issues/492
776+
[<Test>]
777+
member this.``Named parameter in FSharpChecker.Create should not be treated as infix operation``() =
778+
this.SetConfig(["x = true ===> x"])
779+
780+
this.Parse """
781+
module Goat
782+
783+
open FSharp.Compiler.CodeAnalysis
784+
785+
do
786+
let checker = FSharpChecker.Create(keepAssemblyContents = true)
787+
()
788+
"""
789+
790+
this.AssertNoWarnings()
791+
757792
/// Regression test for: https://github.com/fsprojects/FSharpLint/pull/194#issuecomment-268560761
758793
[<Test>]
759794
member this.``Lambdas in hint suggestions must be surrounded with parentheses.``() =

0 commit comments

Comments
 (0)