diff --git a/CHANGELOG.md b/CHANGELOG.md index f7192ce..98ad340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.9.3 - 2024-02-16 + +### Fixed +* Fixed a false positive of LoggingTemplateMissingValuesAnalyzer. [#78](https://github.com/G-Research/fsharp-analyzers/issues/78) + ## 0.9.2 - 2024-02-16 ### Fixed diff --git a/src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs b/src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs index 758a1f5..b0b1970 100644 --- a/src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs +++ b/src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs @@ -58,7 +58,7 @@ let analyze (typedTree : FSharpImplementationFileContents) = "Microsoft.Extensions.Logging.LoggerExtensions.LogWarning" ] - let pattern = @"(?{+)[a-zA-Z0-9_-]*(?}+)" + let pattern = @"(?{+)[^{}]*(?}+)" let regex = Regex pattern let walker = @@ -86,10 +86,15 @@ let analyze (typedTree : FSharpImplementationFileContents) = match logString with | Some s -> - let matches = regex.Matches s + let matches = + regex.Matches s + |> Seq.filter (fun matchItem -> + matchItem.Groups["opening"].Value.Length = matchItem.Groups["closing"].Value.Length + ) + |> Seq.toArray let escapedMatches = - Seq.sumBy + Array.sumBy (fun (matchItem : Match) -> let opening = matchItem.Groups["opening"] let closing = matchItem.Groups["closing"] @@ -98,7 +103,7 @@ let analyze (typedTree : FSharpImplementationFileContents) = ) matches - matches.Count - escapedMatches + matches.Length - escapedMatches | None -> 0 if diff --git a/tests/FSharp.Analyzers.Tests/data/loggingtemplatemissingvalues/negative/No warnings for unbalanced braces.fs b/tests/FSharp.Analyzers.Tests/data/loggingtemplatemissingvalues/negative/No warnings for unbalanced braces.fs new file mode 100644 index 0000000..1c7339d --- /dev/null +++ b/tests/FSharp.Analyzers.Tests/data/loggingtemplatemissingvalues/negative/No warnings for unbalanced braces.fs @@ -0,0 +1,9 @@ +module M + + open Microsoft.Extensions.Logging + + let testlog () = + use factory = LoggerFactory.Create(fun b -> b.AddConsole() |> ignore) + let logger: ILogger = factory.CreateLogger("Program") + + logger.LogDebug("Blah {xxx}} foo {yyy}", 23) diff --git a/tests/FSharp.Analyzers.Tests/data/loggingtemplatemissingvalues/negative/No warnings for unusual template names2.fs b/tests/FSharp.Analyzers.Tests/data/loggingtemplatemissingvalues/negative/No warnings for unusual template names2.fs new file mode 100644 index 0000000..71b5ffe --- /dev/null +++ b/tests/FSharp.Analyzers.Tests/data/loggingtemplatemissingvalues/negative/No warnings for unusual template names2.fs @@ -0,0 +1,11 @@ +module M + + open Microsoft.Extensions.Logging + + let testlog () = + use factory = LoggerFactory.Create(fun b -> b.AddConsole() |> ignore) + let logger: ILogger = factory.CreateLogger("Program") + let someString = "someString" + let someOtherString = "someOtherString" + + logger.LogDebug("Blah {s_thing:l} foo {s_other_thing:l}", someString, someOtherString)