Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.16.0 - 2025-06-30

### Added
* StringAnalyzer now supports String.LastIndexOf. [#91](https://github.com/G-Research/fsharp-analyzers/pull/91)

## 0.15.0 - 2025-05-18

### Changed
Expand Down
1 change: 1 addition & 0 deletions docs/analyzers/StringAnalyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ There are multiple analyzers for various string comparison functions:
- String.EndsWith Analyzer
- String.StartsWith Analyzer
- String.IndexOf Analyzer
- String.LastIndexOf Analyzer

## Problem

Expand Down
33 changes: 33 additions & 0 deletions src/FSharp.Analyzers/StringAnalyzer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ let StringStartsWithCode = "GRA-STRING-002"
[<Literal>]
let StringIndexOfCode = "GRA-STRING-003"

[<Literal>]
let StringLastIndexOfCode : string = "GRA-STRING-004"

[<Literal>]
let HelpUri =
"https://g-research.github.io/fsharp-analyzers/analyzers/StringAnalyzer.html"
Expand Down Expand Up @@ -159,3 +162,33 @@ let indexOfCliAnalyzer (ctx : CliContext) : Async<Message list> =
[<EditorAnalyzer(StringIndexOfName, StringIndexOfShortDescription, HelpUri)>]
let indexOfEditorAnalyzer (ctx : EditorContext) : Async<Message list> =
async { return ctx.TypedTree |> Option.map indexOfAnalyze |> Option.defaultValue [] }

let lastIndexOfAnalyze (typedTree : FSharpImplementationFileContents) =
invalidStringFunctionUseAnalyzer
"LastIndexOf"
StringLastIndexOfCode
"The usage of String.LastIndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload."
Severity.Warning
typedTree
(fun args ->
match args with
| [ StringExpr ]
| [ StringExpr ; IntExpr ]
| [ StringExpr ; IntExpr ; IntExpr ] -> true
| _ -> false
)

[<Literal>]
let StringLastIndexOfName = "String.LastIndexOf Analyzer"

[<Literal>]
let StringLastIndexOfShortDescription =
"Verifies the correct usage of System.String.LastIndexOf"

[<CliAnalyzer(StringLastIndexOfName, StringLastIndexOfShortDescription, HelpUri)>]
let lastIndexOfCliAnalyzer (ctx : CliContext) : Async<Message list> =
async { return ctx.TypedTree |> Option.map lastIndexOfAnalyze |> Option.defaultValue [] }

[<EditorAnalyzer(StringLastIndexOfName, StringLastIndexOfShortDescription, HelpUri)>]
let lastIndexOfEditorAnalyzer (ctx : EditorContext) : Async<Message list> =
async { return ctx.TypedTree |> Option.map lastIndexOfAnalyze |> Option.defaultValue [] }
15 changes: 15 additions & 0 deletions src/FSharp.Analyzers/StringAnalyzer.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ val indexOfCliAnalyzer : ctx : CliContext -> Async<Message list>

[<EditorAnalyzer(StringIndexOfName, StringIndexOfShortDescription, HelpUri)>]
val indexOfEditorAnalyzer : ctx : EditorContext -> Async<Message list>

[<Literal>]
val StringLastIndexOfCode : string = "GRA-STRING-004"

[<Literal>]
val StringLastIndexOfName : string = "String.LastIndexOf Analyzer"

[<Literal>]
val StringLastIndexOfShortDescription : string = "Verifies the correct usage of System.String.LastIndexOf"

[<CliAnalyzer(StringLastIndexOfName, StringLastIndexOfShortDescription, HelpUri)>]
val lastIndexOfCliAnalyzer : ctx : CliContext -> Async<Message list>

[<EditorAnalyzer(StringLastIndexOfName, StringLastIndexOfShortDescription, HelpUri)>]
val lastIndexOfEditorAnalyzer : ctx : EditorContext -> Async<Message list>
5 changes: 3 additions & 2 deletions tests/FSharp.Analyzers.Tests/StringAnalyzerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type TestCases() =

interface IEnumerable with
member _.GetEnumerator () : IEnumerator =
[| "endswith" ; "indexof" ; "startswith" |]
[| "endswith" ; "indexof" ; "startswith" ; "lastindexof" |]
|> Seq.collect (fun subFolder ->
let folder = Path.Combine (dataFolder, "string", subFolder)
Directory.EnumerateFiles (folder, "*.fs")
Expand All @@ -36,6 +36,7 @@ let findStringAnalyzerFor (fileName : string) =
| "endswith" -> StringAnalyzer.endsWithCliAnalyzer
| "startswith" -> StringAnalyzer.startsWithCliAnalyzer
| "indexof" -> StringAnalyzer.indexOfCliAnalyzer
| "lastindexof" -> StringAnalyzer.lastIndexOfCliAnalyzer
| unknown -> failwithf $"Unknown subfolder \"%s{unknown}\", please configure analyzer"

[<TestCaseSource(typeof<TestCases>)>]
Expand All @@ -55,7 +56,7 @@ type NegativeTestCases() =

interface IEnumerable with
member _.GetEnumerator () : IEnumerator =
[| "endswith" ; "indexof" ; "startswith" |]
[| "endswith" ; "indexof" ; "startswith" ; "lastindexof" |]
|> Seq.collect (fun subFolder ->
let folder = Path.Combine (dataFolder, "string", subFolder, "negative")
Directory.EnumerateFiles (folder, "*.fs")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module M

"foo".LastIndexOf("p")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-STRING-004 | Warning | (3,0 - 3,22) | The usage of String.LastIndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload. | []
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module M

"foo".LastIndexOf("p", 0 ,1)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-STRING-004 | Warning | (3,0 - 3,28) | The usage of String.LastIndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload. | []
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module M

"foo".LastIndexOf("p", 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-STRING-004 | Warning | (3,0 - 3,25) | The usage of String.LastIndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload. | []
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module M

let f () = "f"
"g".LastIndexOf(f())
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-STRING-004 | Warning | (4,0 - 4,20) | The usage of String.LastIndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload. | []
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module M

module A =
let b = "b"

A.b.LastIndexOf("b")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-STRING-004 | Warning | (6,0 - 6,20) | The usage of String.LastIndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload. | []
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module M

let a = "a"
a.LastIndexOf("a")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-STRING-004 | Warning | (4,0 - 4,18) | The usage of String.LastIndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload. | []
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module M

open System
"foo".LastIndexOf('f',0,1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module M

open System
"foo".LastIndexOf('f',0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module M

open System
"foo".LastIndexOf('f')
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module M

open System
"foo".LastIndexOf("f",0,1,StringComparison.InvariantCulture)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module M

open System
"foo".LastIndexOf("f",0,StringComparison.InvariantCulture)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module M

open System
"foo".LastIndexOf("f",StringComparison.InvariantCulture)