From 866d14ab00fc59611522525009d88663506f1a8c Mon Sep 17 00:00:00 2001 From: Tuomas Hietanen Date: Wed, 8 Nov 2023 15:05:32 +0000 Subject: [PATCH] some code-cleanups by FSharpLint --- .../AssemblyResolver.fs | 2 +- .../CommonProviderImplementation/Helpers.fs | 2 +- .../QuotationBuilder.fs | 4 ++-- src/FSharp.Data.Html.Core/HtmlOperations.fs | 2 +- src/FSharp.Data.Html.Core/HtmlRuntime.fs | 3 +-- src/FSharp.Data.Http/Http.fs | 16 +++++++-------- src/FSharp.Data.Json.Core/JsonValue.fs | 4 ++-- .../StructuralInference.fs | 4 ++-- .../TextRuntime.fs | 2 +- .../WorldBankRuntime.fs | 4 ++-- src/FSharp.Data.Xml.Core/XmlRuntime.fs | 20 +++++++++---------- src/FSharp.Data.Xml.Core/XsdInference.fs | 14 +++++++------ 12 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/FSharp.Data.DesignTime/CommonProviderImplementation/AssemblyResolver.fs b/src/FSharp.Data.DesignTime/CommonProviderImplementation/AssemblyResolver.fs index 74b15a848..3920e16bb 100644 --- a/src/FSharp.Data.DesignTime/CommonProviderImplementation/AssemblyResolver.fs +++ b/src/FSharp.Data.DesignTime/CommonProviderImplementation/AssemblyResolver.fs @@ -20,7 +20,7 @@ let init () = if not initialized then initialized <- true - if WebRequest.DefaultWebProxy <> null then + if not(isNull WebRequest.DefaultWebProxy) then WebRequest.DefaultWebProxy.Credentials <- CredentialCache.DefaultNetworkCredentials ProvidedTypes.ProvidedTypeDefinition.Logger diff --git a/src/FSharp.Data.DesignTime/CommonProviderImplementation/Helpers.fs b/src/FSharp.Data.DesignTime/CommonProviderImplementation/Helpers.fs index 4b14779ca..f3a89d861 100644 --- a/src/FSharp.Data.DesignTime/CommonProviderImplementation/Helpers.fs +++ b/src/FSharp.Data.DesignTime/CommonProviderImplementation/Helpers.fs @@ -296,7 +296,7 @@ module internal ProviderHelpers = while max > 0 do let line = reader.ReadLine() - if line = null then + if isNull line then max <- 0 else line |> sb.AppendLine |> ignore diff --git a/src/FSharp.Data.DesignTime/CommonProviderImplementation/QuotationBuilder.fs b/src/FSharp.Data.DesignTime/CommonProviderImplementation/QuotationBuilder.fs index e9e38f113..eba9c7b9d 100644 --- a/src/FSharp.Data.DesignTime/CommonProviderImplementation/QuotationBuilder.fs +++ b/src/FSharp.Data.DesignTime/CommonProviderImplementation/QuotationBuilder.fs @@ -1,4 +1,4 @@ -// Copyright 2011-2015, Tomas Petricek (http://tomasp.net), Gustavo Guerra (http://functionalflow.co.uk), and other contributors +// Copyright 2011-2015, Tomas Petricek (http://tomasp.net), Gustavo Guerra (http://functionalflow.co.uk), and other contributors // Licensed under the Apache License, Version 2.0, see LICENSE.md in this project // // Utilities for building F# quotations without quotation literals @@ -61,7 +61,7 @@ let (?) (typ: Type) (operation: string) (args1: 'T) (args2: 'U) : Expr = match typ.GetMember(operation, MemberTypes.All, flags) with | [| :? MethodInfo as mi |] -> let mi = - if tyargs = [] then + if List.isEmpty tyargs then mi else mi.MakeGenericMethod(tyargs |> Array.ofList) diff --git a/src/FSharp.Data.Html.Core/HtmlOperations.fs b/src/FSharp.Data.Html.Core/HtmlOperations.fs index df79bd85b..1b8df6c7a 100644 --- a/src/FSharp.Data.Html.Core/HtmlOperations.fs +++ b/src/FSharp.Data.Html.Core/HtmlOperations.fs @@ -326,7 +326,7 @@ module HtmlNode = let rec selectElements' level acc source = // if we already have an empty list, terminate early - if acc = [] then + if List.isEmpty acc then [] else diff --git a/src/FSharp.Data.Html.Core/HtmlRuntime.fs b/src/FSharp.Data.Html.Core/HtmlRuntime.fs index a65de5aa8..a9f46fdd2 100644 --- a/src/FSharp.Data.Html.Core/HtmlRuntime.fs +++ b/src/FSharp.Data.Html.Core/HtmlRuntime.fs @@ -269,8 +269,7 @@ module HtmlRuntime = cells |> List.map (fun row -> row - |> List.map (fun (_, col) -> colSpan col) - |> List.fold (+) 0) + |> List.sumBy (snd >> colSpan)) let numberOfColumns = List.max rowLengths diff --git a/src/FSharp.Data.Http/Http.fs b/src/FSharp.Data.Http/Http.fs index 039eec399..d2904e95c 100644 --- a/src/FSharp.Data.Http/Http.fs +++ b/src/FSharp.Data.Http/Http.fs @@ -1620,7 +1620,7 @@ module internal HttpHelpers = let runningOnMono = try - System.Type.GetType("Mono.Runtime") <> null + not(isNull (System.Type.GetType "Mono.Runtime")) with e -> false @@ -1637,7 +1637,7 @@ module internal HttpHelpers = let remoteStackTraceString = typeof.GetField ("_remoteStackTraceString", BindingFlags.Instance ||| BindingFlags.NonPublic) - if remoteStackTraceString <> null then + if not (isNull remoteStackTraceString) then remoteStackTraceString.SetValue(e, e.StackTrace + Environment.NewLine) with _ -> () @@ -1651,7 +1651,7 @@ module internal HttpHelpers = with // If an exception happens, augment the message with the response | :? WebException as exn -> - if exn.Response = null then reraisePreserveStackTrace exn + if isNull exn.Response then reraisePreserveStackTrace exn let responseExn = try @@ -1749,12 +1749,12 @@ module internal HttpHelpers = | "pragma" -> req.Headers.[HeaderEnum.Pragma] <- value | "range" -> if not (value.StartsWith("bytes=")) then - failwith "Invalid value for the Range header" + failwithf "Invalid value for the Range header (%O)" value let bytes = value.Substring("bytes=".Length).Split('-') if bytes.Length <> 2 then - failwith "Invalid value for the Range header" + failwithf "Invalid value for the Range header (%O)" bytes req.AddRange(int64 bytes.[0], int64 bytes.[1]) | "proxy-authorization" -> req.Headers.[HeaderEnum.ProxyAuthorization] <- value @@ -1803,7 +1803,7 @@ module internal HttpHelpers = try return! getResponseAsync req with :? WebException as exc -> - if exc.Response <> null then + if not (isNull exc.Response) then return exc.Response else reraisePreserveStackTrace exc @@ -2164,7 +2164,7 @@ type Http private () = (defaultArg silentCookieErrors false) let contentType = - if resp.ContentType = null then + if isNull resp.ContentType then "application/octet-stream" else resp.ContentType @@ -2174,7 +2174,7 @@ type Http private () = | :? HttpWebResponse as resp -> int resp.StatusCode, resp.CharacterSet | _ -> 0, "" - let characterSet = if characterSet = null then "" else characterSet + let characterSet = if isNull characterSet then "" else characterSet let stream = resp.GetResponseStream() diff --git a/src/FSharp.Data.Json.Core/JsonValue.fs b/src/FSharp.Data.Json.Core/JsonValue.fs index 25378592a..02ccc7527 100644 --- a/src/FSharp.Data.Json.Core/JsonValue.fs +++ b/src/FSharp.Data.Json.Core/JsonValue.fs @@ -300,8 +300,8 @@ type private JsonParser(jsonText: string) = ensure (i + 9 < s.Length) let unicodeChar (s: string) = - if s.Length <> 8 then failwith "unicodeChar" - if s.[0..1] <> "00" then failwith "unicodeChar" + if s.Length <> 8 then failwithf "unicodeChar (%O)" s + if s.[0..1] <> "00" then failwithf "unicodeChar (%O)" s UnicodeHelper.getUnicodeSurrogatePair <| System.UInt32.Parse(s, NumberStyles.HexNumber) diff --git a/src/FSharp.Data.Runtime.Utilities/StructuralInference.fs b/src/FSharp.Data.Runtime.Utilities/StructuralInference.fs index b0ccd4332..e431e83db 100644 --- a/src/FSharp.Data.Runtime.Utilities/StructuralInference.fs +++ b/src/FSharp.Data.Runtime.Utilities/StructuralInference.fs @@ -411,7 +411,7 @@ let parseUnitOfMeasure (provider: IUnitsOfMeasureProvider) (str: string) = let baseUnitStr = str.[.. str.Length - suffix.Length - 1] let baseUnit = provider.SI baseUnitStr - if baseUnit = null then + if isNull baseUnit then None else baseUnit |> trans provider |> Some @@ -422,7 +422,7 @@ let parseUnitOfMeasure (provider: IUnitsOfMeasureProvider) (str: string) = | Some _ -> unit | None -> let unit = provider.SI str - if unit = null then None else Some unit + if isNull unit then None else Some unit /// The inferred types may be set explicitly via inline schemas. /// This table specifies the mapping from (the names that users can use) to (the types used). diff --git a/src/FSharp.Data.Runtime.Utilities/TextRuntime.fs b/src/FSharp.Data.Runtime.Utilities/TextRuntime.fs index 178cac246..2f3b98ade 100644 --- a/src/FSharp.Data.Runtime.Utilities/TextRuntime.fs +++ b/src/FSharp.Data.Runtime.Utilities/TextRuntime.fs @@ -20,7 +20,7 @@ type TextRuntime = else let mutable cache = TextRuntime.cultureInfoCache - if cache = null then + if isNull cache then cache <- Dictionary() TextRuntime.cultureInfoCache <- cache diff --git a/src/FSharp.Data.WorldBank.Core/WorldBankRuntime.fs b/src/FSharp.Data.WorldBank.Core/WorldBankRuntime.fs index 766e1fc1b..2581fb026 100644 --- a/src/FSharp.Data.WorldBank.Core/WorldBankRuntime.fs +++ b/src/FSharp.Data.WorldBank.Core/WorldBankRuntime.fs @@ -73,7 +73,7 @@ module Implementation = Debug.WriteLine( sprintf "[WorldBank] got text: %s" - (if doc = null then "null" + (if isNull doc then "null" elif doc.Length > 50 then doc.[0..49] + "..." else doc) ) @@ -122,7 +122,7 @@ module Implementation = let name = ind?name.AsString().Trim([| '"' |]).Trim() let sourceName = ind?source?value.AsString() - if sources = [] + if List.isEmpty sources || sources |> List.exists (fun source -> String.Compare(source, sourceName, StringComparison.OrdinalIgnoreCase) = 0) then diff --git a/src/FSharp.Data.Xml.Core/XmlRuntime.fs b/src/FSharp.Data.Xml.Core/XmlRuntime.fs index fef40a02e..9e31998c2 100644 --- a/src/FSharp.Data.Xml.Core/XmlRuntime.fs +++ b/src/FSharp.Data.Xml.Core/XmlRuntime.fs @@ -103,7 +103,7 @@ type XmlRuntime = static member TryGetAttribute(xml: XmlElement, nameWithNS) = let attr = xml.XElement.Attribute(XName.Get(nameWithNS)) - if attr = null then None else Some attr.Value + if isNull attr then None else Some attr.Value // Operations that obtain children - depending on the inference, we may // want to get an array, option (if it may or may not be there) or @@ -114,12 +114,12 @@ type XmlRuntime = let mutable current = value.XElement for i = 0 to namesWithNS.Length - 2 do - if current <> null then + if not(isNull current) then current <- current.Element(XName.Get namesWithNS.[i]) let value = current - if value = null then + if isNull value then [||] else [| for c in value.Elements(XName.Get namesWithNS.[namesWithNS.Length - 1]) -> { XElement = c } |] @@ -193,7 +193,7 @@ type XmlRuntime = match v with | :? XmlElement as v -> let xElement = - if v.XElement.Parent = null then + if isNull v.XElement.Parent then v.XElement else // clone, as element is connected to previous parent @@ -248,7 +248,7 @@ type XmlRuntime = ||> Array.fold (fun parent nameWithNS -> let xname = XName.Get nameWithNS - if parent = null then + if isNull parent then XElement xname else let element = @@ -257,7 +257,7 @@ type XmlRuntime = else parent.Element(xname) - if element = null then + if isNull element then let element = XElement xname parent.Add element element @@ -271,7 +271,7 @@ type XmlRuntime = match toXmlContent value with | [||] -> () - | [| v |] when v :? string && element.Attribute(xname) = null -> element.SetAttributeValue(xname, v) + | [| v |] when v :? string && isNull (element.Attribute xname) -> element.SetAttributeValue(xname, v) | _ -> failwithf "Unexpected attribute value: %A" value let parents = System.Collections.Generic.Dictionary() @@ -296,7 +296,7 @@ type XmlRuntime = Seq.skip 1 parentNames |> Seq.mapi (fun x i -> x, i)) ||> Seq.fold (fun element ((_, nameWithNS) as key) -> - if element.Parent = null then + if isNull element.Parent then let parent = match parents.TryGetValue key with | true, parent -> parent @@ -313,7 +313,7 @@ type XmlRuntime = element.Parent) - if v.Parent = null then element.Add v + if isNull v.Parent then element.Add v | :? string as v -> let child = createElement element nameWithNS child.Value <- v @@ -345,7 +345,7 @@ module XmlSchema = let useResolutionFolder (baseUri: System.Uri) = resolutionFolder <> "" - && (baseUri = null || baseUri.OriginalString = "") + && (isNull baseUri || baseUri.OriginalString = "") let getEncoding xmlText = // peek encoding definition let settings = XmlReaderSettings(ConformanceLevel = ConformanceLevel.Fragment) diff --git a/src/FSharp.Data.Xml.Core/XsdInference.fs b/src/FSharp.Data.Xml.Core/XsdInference.fs index 81fc8c739..90f76ed37 100644 --- a/src/FSharp.Data.Xml.Core/XsdInference.fs +++ b/src/FSharp.Data.Xml.Core/XsdInference.fs @@ -86,8 +86,10 @@ module XsdParsing = let items = System.Collections.Generic.HashSet() let rec collect elm = - if subst.ContainsKey elm then - for x in subst.Item elm do + match subst.TryGetValue elm with + | false, _ -> () + | true, substVal -> + for x in substVal do if items.Add x then collect x collect elm @@ -98,7 +100,7 @@ module XsdParsing = |> Seq.map (fun x -> x, collectSubst x) |> dict - fun elm -> if subst'.ContainsKey elm then subst'.Item elm else [] + fun elm -> match subst'.TryGetValue elm with | true, elVal -> elVal | false, _ -> [] let elements = @@ -303,9 +305,9 @@ module internal XsdInference = body :: attrs | ComplexContent xsdParticle -> let body = - if ctx.ContainsKey cty then - ctx.Item cty - else + match ctx.TryGetValue cty with + | true, ctVal -> ctVal + | false, _ -> let result = { InferedProperty.Name = "" Type = InferedType.Top }