Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some code-cleanups by FSharpLint #1492

Merged
merged 3 commits into from
Nov 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Data.Html.Core/HtmlOperations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions src/FSharp.Data.Html.Core/HtmlRuntime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,7 @@ module HtmlRuntime =

let rowLengths =
cells
|> List.map (fun row ->
row
|> List.map (fun (_, col) -> colSpan col)
|> List.fold (+) 0)
|> List.map (fun row -> row |> List.sumBy (snd >> colSpan))

let numberOfColumns = List.max rowLengths

Expand Down
16 changes: 8 additions & 8 deletions src/FSharp.Data.Http/Http.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -1637,7 +1637,7 @@ module internal HttpHelpers =
let remoteStackTraceString =
typeof<exn>.GetField ("_remoteStackTraceString", BindingFlags.Instance ||| BindingFlags.NonPublic)

if remoteStackTraceString <> null then
if not (isNull remoteStackTraceString) then
remoteStackTraceString.SetValue(e, e.StackTrace + Environment.NewLine)
with _ ->
()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Data.Json.Core/JsonValue.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Data.Runtime.Utilities/StructuralInference.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Data.Runtime.Utilities/TextRuntime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TextRuntime =
else
let mutable cache = TextRuntime.cultureInfoCache

if cache = null then
if isNull cache then
cache <- Dictionary<string, CultureInfo>()
TextRuntime.cultureInfoCache <- cache

Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Data.WorldBank.Core/WorldBankRuntime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions src/FSharp.Data.Xml.Core/XmlRuntime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 } |]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions src/FSharp.Data.Xml.Core/XsdInference.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -98,7 +100,10 @@ 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 =
Expand Down Expand Up @@ -303,9 +308,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 }
Expand Down
Loading