Skip to content

Commit

Permalink
Add positive test for String.IndexOf.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Oct 4, 2023
1 parent 2773f1e commit 8e60815
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# Always use lf for F# files
*.fs text eol=lf
*.fsx text eol=lf
*.fsi text eol=lf
*.fsi text eol=lf
*.expected text eol=lf
3 changes: 3 additions & 0 deletions src/FSharp.Analyzers/Codes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ let StringEndsWith = "GRA-001"

[<Literal>]
let StringStartsWith = "GRA-002"

[<Literal>]
let StringIndexOf = "GRA-003"
22 changes: 19 additions & 3 deletions src/FSharp.Analyzers/StringAnalyzers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ module StringAnalyzers =
else
fsharpType.GenericArguments |> Seq.map visit |> String.concat " -> "

visit mfv.FullType = expectedSignature
let actualSignature = visit mfv.FullType
actualSignature = expectedSignature

[<CliAnalyzer>]
[<CliAnalyzer "String.EndsWith Analyzer">]
let endsWithAnalyzer (ctx : CliContext) : Async<Message list> =
invalidStringFunctionUseAnalyzer
"EndsWith"
Expand All @@ -157,7 +158,7 @@ module StringAnalyzers =
| _ -> false)
(hasMatchingSignature "System.String -> System.Boolean")

[<CliAnalyzer>]
[<CliAnalyzer "String.StartsWith Analyzer">]
let startsWithAnalyzer (ctx : CliContext) : Async<Message list> =
invalidStringFunctionUseAnalyzer
"StartsWith"
Expand All @@ -171,3 +172,18 @@ module StringAnalyzers =
| SingleStringArgumentExpr _ -> true
| _ -> false)
(hasMatchingSignature "System.String -> System.Boolean")

[<CliAnalyzer "String.IndexOf Analyzer">]
let indexOfAnalyzer (ctx : CliContext) : Async<Message list> =
invalidStringFunctionUseAnalyzer
"IndexOf"
Codes.StringIndexOf
"The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload."
Warning
ctx.SourceText
ctx.ParseFileResults.ParseTree
ctx.CheckFileResults
(function
| SingleStringArgumentExpr _ -> true
| _ -> false)
(hasMatchingSignature "System.String -> System.Int32")
7 changes: 5 additions & 2 deletions src/FSharp.Analyzers/StringAnalyzers.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ namespace ``G-Research``.FSharp.Analyzers
open FSharp.Analyzers.SDK

module StringAnalyzers =
[<CliAnalyzer>]
[<CliAnalyzer "String.EndsWith Analyzer">]
val endsWithAnalyzer : ctx : CliContext -> Async<Message list>

[<CliAnalyzer>]
[<CliAnalyzer "String.StartsWith Analyzer">]
val startsWithAnalyzer : ctx : CliContext -> Async<Message list>

[<CliAnalyzer "String.IndexOf Analyzer">]
val indexOfAnalyzer : ctx : CliContext -> Async<Message list>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foo".IndexOf("p")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-003 | Warning | (1,6 - 1,13) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
2 changes: 2 additions & 0 deletions tests/data/string/indexof/Function call - Single Argument.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let f () = "f"
"g".IndexOf(f())
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-003 | Warning | (2,4 - 2,11) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
4 changes: 4 additions & 0 deletions tests/data/string/indexof/Prefixed Value - Single Argument.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module A =
let b = "b"

A.b.IndexOf("b")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-003 | Warning | (4,4 - 4,11) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.
2 changes: 2 additions & 0 deletions tests/data/string/indexof/Value - Single Argument.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let a = "a"
a.IndexOf("a")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRA-003 | Warning | (2,2 - 2,9) | The usage of String.IndexOf with a single string argument is discouraged. Signal your intention explicitly by calling an overload.

0 comments on commit 8e60815

Please sign in to comment.