Skip to content

Commit 4ea1ae8

Browse files
committed
Address remaining problems detected by analyzers.
1 parent 581bf14 commit 4ea1ae8

31 files changed

+207
-181
lines changed

src/Common/StringParsing.fs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ module String =
2323

2424
/// Matches when a string starts with the specified sub-string
2525
let (|StartsWith|_|) (start: string) (text: string) =
26-
if text.StartsWith(start) then
26+
if text.StartsWith(start, StringComparison.Ordinal) then
2727
Some(text.Substring(start.Length))
2828
else
2929
None
3030

3131
/// Matches when a string starts with the specified sub-string
3232
/// The matched string is trimmed from all whitespace.
3333
let (|StartsWithTrim|_|) (start: string) (text: string) =
34-
if text.StartsWith(start) then
34+
if text.StartsWith(start, StringComparison.Ordinal) then
3535
Some(text.Substring(start.Length).Trim())
3636
else
3737
None
@@ -40,8 +40,8 @@ module String =
4040
/// with a given value (and returns the rest of it)
4141
let (|StartsAndEndsWith|_|) (starts: string, ends: string) (s: string) =
4242
if
43-
s.StartsWith(starts)
44-
&& s.EndsWith(ends)
43+
s.StartsWith(starts, StringComparison.Ordinal)
44+
&& s.EndsWith(ends, StringComparison.Ordinal)
4545
&& s.Length >= starts.Length + ends.Length
4646
then
4747
Some(s.Substring(starts.Length, s.Length - starts.Length - ends.Length))
@@ -60,8 +60,8 @@ module String =
6060
/// For example "[aa]bc" is wrapped in [ and ] pair. Returns the wrapped
6161
/// text together with the rest.
6262
let (|StartsWithWrapped|_|) (starts: string, ends: string) (text: string) =
63-
if text.StartsWith(starts) then
64-
let id = text.IndexOf(ends, starts.Length)
63+
if text.StartsWith(starts, StringComparison.Ordinal) then
64+
let id = text.IndexOf(ends, starts.Length, StringComparison.Ordinal)
6565

6666
if id >= 0 then
6767
let wrapped = text.Substring(starts.Length, id - starts.Length)
@@ -79,7 +79,7 @@ module String =
7979
let rec tryEol eolList =
8080
match eolList with
8181
| h: string :: t ->
82-
match text.IndexOf(h) with
82+
match text.IndexOf(h, StringComparison.Ordinal) with
8383
| i when i < 0 -> tryEol t
8484
| i -> text.Substring(i + h.Length)
8585
| _ -> text
@@ -174,15 +174,15 @@ module StringPosition =
174174
StartColumn = n.StartColumn + text.Length - trimmed.Length })
175175

176176
/// Matches when a string starts with any of the specified sub-strings
177-
let (|StartsWithAny|_|) (starts: seq<string>) (text: string, _n: MarkdownRange) =
178-
if starts |> Seq.exists (text.StartsWith) then
177+
let (|StartsWithAny|_|) (starts: string seq) (text: string, _n: MarkdownRange) =
178+
if starts |> Seq.exists (fun s -> text.StartsWith(s, StringComparison.Ordinal)) then
179179
Some()
180180
else
181181
None
182182

183183
/// Matches when a string starts with the specified sub-string
184184
let (|StartsWith|_|) (start: string) (text: string, n: MarkdownRange) =
185-
if text.StartsWith(start) then
185+
if text.StartsWith(start, StringComparison.Ordinal) then
186186
Some(
187187
text.Substring(start.Length),
188188
{ n with
@@ -194,7 +194,7 @@ module StringPosition =
194194
/// Matches when a string starts with the specified sub-string
195195
/// The matched string is trimmed from all whitespace.
196196
let (|StartsWithTrim|_|) (start: string) (text: string, n: MarkdownRange) =
197-
if text.StartsWith(start) then
197+
if text.StartsWith(start, StringComparison.Ordinal) then
198198
Some(
199199
text.Substring(start.Length).Trim(),
200200
{ n with
@@ -207,7 +207,7 @@ module StringPosition =
207207
/// The matched string is trimmed from all whitespace.
208208
let (|StartsWithNTimesTrimIgnoreStartWhitespace|_|) (start: string) (text: string, _n: MarkdownRange) =
209209
if text.Contains(start) then
210-
let beforeStart = text.Substring(0, text.IndexOf(start))
210+
let beforeStart = text.Substring(0, text.IndexOf(start, StringComparison.Ordinal))
211211

212212
if String.IsNullOrWhiteSpace(beforeStart) then
213213
let startAndRest = text.Substring(beforeStart.Length)
@@ -232,8 +232,8 @@ module StringPosition =
232232
/// with a given value (and returns the rest of it)
233233
let (|StartsAndEndsWith|_|) (starts: string, ends: string) (s: string, n: MarkdownRange) =
234234
if
235-
s.StartsWith(starts)
236-
&& s.EndsWith(ends)
235+
s.StartsWith(starts, StringComparison.Ordinal)
236+
&& s.EndsWith(ends, StringComparison.Ordinal)
237237
&& s.Length >= starts.Length + ends.Length
238238
then
239239
Some(
@@ -276,8 +276,8 @@ module StringPosition =
276276
/// For example "[aa]bc" is wrapped in [ and ] pair. Returns the wrapped
277277
/// text together with the rest.
278278
let (|StartsWithWrapped|_|) (starts: string, ends: string) (text: string, n: MarkdownRange) =
279-
if text.StartsWith(starts) then
280-
let id = text.IndexOf(ends, starts.Length)
279+
if text.StartsWith(starts, StringComparison.Ordinal) then
280+
let id = text.IndexOf(ends, starts.Length, StringComparison.Ordinal)
281281

282282
if id >= 0 then
283283
let wrapped = text.Substring(starts.Length, id - starts.Length)
@@ -363,7 +363,8 @@ module Lines =
363363
let (|TakeStartingWithOrBlank|_|) (start: string) (input: string list) =
364364
match
365365
input
366-
|> List.partitionWhile (fun s -> String.IsNullOrWhiteSpace s || s.StartsWith(start))
366+
|> List.partitionWhile (fun s ->
367+
String.IsNullOrWhiteSpace s || s.StartsWith(start, StringComparison.Ordinal))
367368
with
368369
| matching, rest when matching <> [] -> Some(matching, rest)
369370
| _ -> None
@@ -405,7 +406,7 @@ module Lines =
405406
|> List.map (fun (StringPosition.TrimStart s) -> s)
406407
// Now remove all additional spaces at the end, but keep two spaces if existent
407408
|> List.map (fun (s, n) ->
408-
let endsWithTwoSpaces = s.EndsWith(" ")
409+
let endsWithTwoSpaces = s.EndsWith(" ", StringComparison.Ordinal)
409410

410411
let trimmed = s.TrimEnd([| ' ' |]) + if endsWithTwoSpaces then " " else ""
411412

src/Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot;</FSharpAnalyzersOtherFlags>
44
<FSharpAnalyzersOtherFlags>$(FSharpAnalyzersOtherFlags) --analyzers-path &quot;$(PkgIonide_Analyzers)/analyzers/dotnet/fs&quot;</FSharpAnalyzersOtherFlags>
55
<FSharpAnalyzersOtherFlags>$(FSharpAnalyzersOtherFlags) --exclude-analyzer PartialAppAnalyzer -c Release</FSharpAnalyzersOtherFlags>
6-
<FSharpAnalyzersOtherFlags>$(FSharpAnalyzersOtherFlags) --verbose --code-root ../.. --report &quot;../../reports/$(MSBuildProjectName)-$(TargetFramework).sarif&quot;</FSharpAnalyzersOtherFlags>
6+
<FSharpAnalyzersOtherFlags>$(FSharpAnalyzersOtherFlags) --code-root ../.. --report &quot;../../reports/$(MSBuildProjectName)-$(TargetFramework).sarif&quot;</FSharpAnalyzersOtherFlags>
77
</PropertyGroup>
88
</Project>

src/FSharp.Formatting.ApiDocs/GenerateHtml.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type HtmlRender(model: ApiDocModel, ?menuTemplateFolder: string) =
5858
] ]
5959

6060
let removeParen (memberName: string) =
61-
let firstParen = memberName.IndexOf("(")
61+
let firstParen = memberName.IndexOf('(')
6262

6363
if firstParen > 0 then
6464
memberName.Substring(0, firstParen)
@@ -81,7 +81,7 @@ type HtmlRender(model: ApiDocModel, ?menuTemplateFolder: string) =
8181

8282
// Copy XML sig for use in `cref` markdown
8383
let copyXmlSigIconMarkdown (xmlDocSig: string) =
84-
if xmlDocSig.StartsWith("`") || xmlDocSig.EndsWith("`") then
84+
if xmlDocSig.StartsWith('`') || xmlDocSig.EndsWith('`') then
8585
div [] []
8686
else
8787
let delim =

src/FSharp.Formatting.ApiDocs/GenerateModel.fs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ type ApiDocAttribute(name, fullName, constructorArguments, namedConstructorArgum
173173
member x.ObsoleteMessage =
174174
let tryFindObsoleteMessage =
175175
x.ConstructorArguments
176-
|> Seq.tryFind (fun x -> x :? string)
176+
|> List.tryFind (fun x -> x :? string)
177177
|> Option.map string
178178
|> Option.defaultValue ""
179179

@@ -186,7 +186,7 @@ type ApiDocAttribute(name, fullName, constructorArguments, namedConstructorArgum
186186
member x.CustomOperationName =
187187
let tryFindCustomOperation =
188188
x.ConstructorArguments
189-
|> Seq.tryFind (fun x -> x :? string)
189+
|> List.tryFind (fun x -> x :? string)
190190
|> Option.map string
191191
|> Option.defaultValue ""
192192

@@ -200,7 +200,10 @@ type ApiDocAttribute(name, fullName, constructorArguments, namedConstructorArgum
200200
let dropSuffix (s: string) (t: string) = s.[0 .. s.Length - t.Length - 1]
201201

202202
let attributeName =
203-
if removeAttributeSuffix && attributeName.EndsWith "Attribute" then
203+
if
204+
removeAttributeSuffix
205+
&& attributeName.EndsWith("Attribute", StringComparison.Ordinal)
206+
then
204207
dropSuffix attributeName "Attribute"
205208
else
206209
attributeName
@@ -368,7 +371,7 @@ type ApiDocMember
368371
printfn "%s(%d,%d): error: duplicate id for example '%s'" m.FileName m.StartLine m.StartColumn id
369372

370373
for (id, _count) in knownExampleIds do
371-
if id.StartsWith "example-" then
374+
if id.StartsWith("example-", StringComparison.Ordinal) then
372375
let potentialInteger = id.["example-".Length ..]
373376

374377
match System.Int32.TryParse potentialInteger with
@@ -482,13 +485,13 @@ type ApiDocMember
482485
member x.Symbol: FSharpSymbol = symbol
483486

484487
/// Gets a value indicating whether this member is obsolete
485-
member x.IsObsolete = x.Attributes |> Seq.exists (fun a -> a.IsObsoleteAttribute)
488+
member x.IsObsolete = x.Attributes |> List.exists (fun a -> a.IsObsoleteAttribute)
486489

487490
/// Returns the obsolete message, when this member is obsolete. When its not or no message was specified, an empty string is returned
488491
member x.ObsoleteMessage = ApiDocAttribute.TryGetObsoleteMessage(x.Attributes)
489492

490493
member x.IsRequireQualifiedAccessAttribute =
491-
x.Attributes |> Seq.exists (fun a -> a.IsRequireQualifiedAccessAttribute)
494+
x.Attributes |> List.exists (fun a -> a.IsRequireQualifiedAccessAttribute)
492495

493496
/// Returns the custom operation name, when this attribute is the CustomOperationAttribute.
494497
member x.CustomOperationName = ApiDocAttribute.TryGetCustomOperationName(x.Attributes)
@@ -602,7 +605,7 @@ type ApiDocEntity
602605
member x.StaticMembers: ApiDocMember list = stat
603606

604607
/// Gets a value indicating whether this member is obsolete
605-
member x.IsObsolete = x.Attributes |> Seq.exists (fun a -> a.IsObsoleteAttribute)
608+
member x.IsObsolete = x.Attributes |> List.exists (fun a -> a.IsObsoleteAttribute)
606609

607610
/// Returns the obsolete message, when this member is obsolete. When its not or no message was specified, an empty string is returned
608611
member x.ObsoleteMessage = ApiDocAttribute.TryGetObsoleteMessage(x.Attributes)
@@ -782,7 +785,8 @@ type internal CrossReferenceResolver(root, collectionName, qualify, extensions)
782785

783786
let nameGen (name: string) =
784787
let nice =
785-
(toReplace |> Seq.fold (fun (s: string) (inv, repl) -> s.Replace(inv, repl)) name)
788+
(toReplace
789+
|> List.fold (fun (s: string) (inv, repl) -> s.Replace(inv, repl)) name)
786790
.ToLower()
787791

788792
let found =
@@ -837,7 +841,7 @@ type internal CrossReferenceResolver(root, collectionName, qualify, extensions)
837841
failwithf "The entity %s was not registered before!" (sprintf "%s.%s" entity.AccessPath entity.CompiledName)
838842

839843
let removeParen (memberName: string) =
840-
let firstParen = memberName.IndexOf("(")
844+
let firstParen = memberName.IndexOf('(')
841845

842846
if firstParen > 0 then
843847
memberName.Substring(0, firstParen)
@@ -874,7 +878,7 @@ type internal CrossReferenceResolver(root, collectionName, qualify, extensions)
874878
let noNamespaceParts =
875879
if hasModuleSuffix then
876880
match noNamespaceParts with
877-
| h :: t when h.EndsWith("Module") -> h.[0 .. h.Length - 7] :: t
881+
| h :: t when h.EndsWith("Module", StringComparison.Ordinal) -> h.[0 .. h.Length - 7] :: t
878882
| s -> s
879883
else
880884
noNamespaceParts
@@ -890,7 +894,10 @@ type internal CrossReferenceResolver(root, collectionName, qualify, extensions)
890894
noGenerics
891895

892896
let externalDocsLink isMember simple (typeName: string) (fullName: string) =
893-
if fullName.StartsWith "FSharp." || fullName.StartsWith "Microsoft.FSharp." then
897+
if
898+
fullName.StartsWith("FSharp.", StringComparison.Ordinal)
899+
|| fullName.StartsWith("Microsoft.FSharp.", StringComparison.Ordinal)
900+
then
894901
let noParen = removeParen typeName
895902

896903
let docs =
@@ -1051,9 +1058,9 @@ type internal CrossReferenceResolver(root, collectionName, qualify, extensions)
10511058

10521059
match cref with
10531060
// Type
1054-
| _ when cref.StartsWith("T:") -> Some(resolveCrossReferenceForTypeByXmlSig cref)
1061+
| _ when cref.StartsWith("T:", StringComparison.Ordinal) -> Some(resolveCrossReferenceForTypeByXmlSig cref)
10551062
// Compiler was unable to resolve!
1056-
| _ when cref.StartsWith("!:") ->
1063+
| _ when cref.StartsWith("!:", StringComparison.Ordinal) ->
10571064
Log.warnf "Compiler was unable to resolve %s" cref
10581065
None
10591066
// ApiDocMember
@@ -1165,7 +1172,7 @@ module internal TypeFormatter =
11651172
match args with
11661173
| [] -> typeName
11671174
| [ arg ] ->
1168-
if tcref.DisplayName.StartsWith "[" then
1175+
if tcref.DisplayName.StartsWith '[' then
11691176
span [] [ formatTypeWithPrecAsHtml ctx 2 arg; !!tcref.DisplayName ]
11701177
else
11711178
span [] [ formatTypeWithPrecAsHtml ctx 2 arg; !! "&#32;"; typeName ]
@@ -1581,7 +1588,7 @@ module internal SymbolReader =
15811588
let readUnionCase (ctx: ReadingContext) (_typ: FSharpEntity) (case: FSharpUnionCase) =
15821589

15831590
let formatFieldUsage (field: FSharpField) =
1584-
if field.Name.StartsWith("Item") then
1591+
if field.Name.StartsWith("Item", StringComparison.Ordinal) then
15851592
formatTypeAsHtml ctx.UrlMap field.FieldType
15861593
else
15871594
!!field.Name
@@ -1993,7 +2000,7 @@ module internal SymbolReader =
19932000
let remarks =
19942001
let remarkNodes = doc.Elements(XName.Get "remarks") |> Seq.toList
19952002

1996-
if Seq.length remarkNodes > 0 then
2003+
if List.length remarkNodes > 0 then
19972004
let html = new StringBuilder()
19982005

19992006
for (id, e) in List.indexed remarkNodes do
@@ -2036,7 +2043,11 @@ module internal SymbolReader =
20362043
// FSharp.Core cref listings don't start with "T:", see https://github.com/dotnet/fsharp/issues/9805
20372044
let cname = cref.Value
20382045

2039-
let cname = if cname.StartsWith("T:") then cname else "T:" + cname // FSharp.Core exception listings don't start with "T:"
2046+
let cname =
2047+
if cname.StartsWith("T:", StringComparison.Ordinal) then
2048+
cname
2049+
else
2050+
"T:" + cname // FSharp.Core exception listings don't start with "T:"
20402051

20412052
match urlMap.ResolveCref cname with
20422053
| Some reference ->
@@ -2095,7 +2106,7 @@ module internal SymbolReader =
20952106

20962107
match lst with
20972108
| [ x ] -> rawData.[n] <- x.Value
2098-
| lst -> lst |> Seq.iteri (fun id el -> rawData.[n + "-" + string id] <- el.Value))
2109+
| lst -> lst |> List.iteri (fun id el -> rawData.[n + "-" + string id] <- el.Value))
20992110

21002111
let rawData = rawData |> Seq.toList
21012112

@@ -2756,9 +2767,9 @@ module internal SymbolReader =
27562767
// ----------------------------------------------------------------------------------------------
27572768

27582769
let stripMicrosoft (str: string) =
2759-
if str.StartsWith("Microsoft.") then
2770+
if str.StartsWith("Microsoft.", StringComparison.Ordinal) then
27602771
str.["Microsoft.".Length ..]
2761-
elif str.StartsWith("microsoft-") then
2772+
elif str.StartsWith("microsoft-", StringComparison.Ordinal) then
27622773
str.["microsoft-".Length ..]
27632774
else
27642775
str

src/FSharp.Formatting.CodeFormat/CodeFormat.fs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace FSharp.Formatting.CodeFormat
77

88
open System
9-
open System.Diagnostics
109

1110
/// <summary>
1211
/// Represents an individual formatted snippet with title as key
@@ -28,7 +27,7 @@ type FormattedSnippet(key: string, content: string) =
2827

2928

3029
/// Represents formatted snippets
31-
type FormattedContent(snippets: FormattedSnippet[], tips: string) =
30+
type FormattedContent(snippets: FormattedSnippet array, tips: string) =
3231
/// Returns the processed snippets as an array
3332
member x.Snippets = snippets
3433

0 commit comments

Comments
 (0)