Skip to content

Commit 7a8d571

Browse files
author
Matt
committed
Fixed bug when matching against literals
would check to make sure the pattern was camelcase as it thought it was the binding of a new value, but it could be a literal, so the fix is to check whether or not the value is implemented at the location of the pattern
1 parent b50d21f commit 7a8d571

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/FSharpLint.Framework/AstInfo.fs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ module AstInfo =
103103

104104
isPublic true false path
105105

106+
/// Is an identifier being used to identify declaration of a value?
106107
let isValue (identifier:LongIdent) (checkFile:CheckFileResults) =
107108
if System.Char.IsUpper(identifier.Head.idText.[0]) && identifier.Length = 1 then
108109
let identifier = identifier.Head
@@ -115,7 +116,15 @@ module AstInfo =
115116
match symbol with
116117
| Some(symbol) ->
117118
match symbol.Symbol with
118-
| :? FSharpMemberFunctionOrValue -> true
119+
| :? FSharpMemberFunctionOrValue as symbol ->
120+
match symbol.ImplementationLocation with
121+
| Some(implLocation) ->
122+
/// If it's implemented elsewhere then it's not what we're looking for,
123+
/// it's probably a literal being matched against.
124+
let isImplementedHere = implLocation.StartRange = identifier.idRange.StartRange
125+
126+
isImplementedHere
127+
| None -> true
119128
| _ -> false
120129
| None -> false
121130
else

tests/FSharpLint.Rules.Tests/TestNameConventionRules.fs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,20 @@ namespace matt.dog.cat"""
366366

367367
Assert.IsTrue(this.ErrorExistsAt(2, 10))
368368

369+
[<Test>]
370+
member this.LiteralPatternMatchExpectNoErrors() =
371+
this.Parse """
372+
module program
373+
[<Literal>]
374+
let Dog = true
375+
376+
let main =
377+
match true with
378+
| Dog -> ()
379+
| _ -> ()"""
380+
381+
Assert.IsFalse(this.ErrorExistsAt(8, 6))
382+
369383
[<Test>]
370384
member this.VariablePatternMatchIsPascalCase() =
371385
this.Parse """
@@ -375,7 +389,7 @@ module program
375389
| Dog -> ()"""
376390

377391
Assert.IsTrue(this.ErrorExistsAt(5, 6))
378-
392+
379393
[<Test>]
380394
member this.VariablePatternMatchIsCamelCase() =
381395
this.Parse """

0 commit comments

Comments
 (0)