diff --git a/src/libraries/System.Private.Uri/src/System/UriExt.cs b/src/libraries/System.Private.Uri/src/System/UriExt.cs index 51dcb3694dc2f0..790ca2dd7104eb 100644 --- a/src/libraries/System.Private.Uri/src/System/UriExt.cs +++ b/src/libraries/System.Private.Uri/src/System/UriExt.cs @@ -223,15 +223,9 @@ private static bool CheckForUnicodeOrEscapedUnreserved(string data) // public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttribute.Uri, "uriKind")] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result) { - if (uriString is null) - { - result = null; - return false; - } - UriFormatException? e = null; - result = CreateHelper(uriString, false, uriKind, ref e); + result = CreateHelper(uriString, false, uriKind); result?.DebugSetLeftCtor(); - return e is null && result != null; + return result is not null; } /// @@ -243,15 +237,9 @@ public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttrib /// if the was successfully created; otherwise, . public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttribute.Uri)] string? uriString, in UriCreationOptions creationOptions, [NotNullWhen(true)] out Uri? result) { - if (uriString is null) - { - result = null; - return false; - } - UriFormatException? e = null; - result = CreateHelper(uriString, false, UriKind.Absolute, ref e, in creationOptions); + result = CreateHelper(uriString, false, UriKind.Absolute, in creationOptions); result?.DebugSetLeftCtor(); - return e is null && result != null; + return result is not null; } public static bool TryCreate(Uri? baseUri, string? relativeUri, [NotNullWhen(true)] out Uri? result) @@ -264,6 +252,7 @@ public static bool TryCreate(Uri? baseUri, string? relativeUri, [NotNullWhen(tru result = relativeLink; return true; } + result = null; return false; } @@ -278,7 +267,6 @@ public static bool TryCreate(Uri? baseUri, Uri? relativeUri, [NotNullWhen(true)] if (baseUri.IsNotAbsoluteUri) return false; - UriFormatException? e = null; string? newUriString = null; bool dontEscape; @@ -290,16 +278,17 @@ public static bool TryCreate(Uri? baseUri, Uri? relativeUri, [NotNullWhen(true)] else { dontEscape = false; - newUriString = baseUri.Syntax.InternalResolve(baseUri, relativeUri, out e); + newUriString = baseUri.Syntax.InternalResolve(baseUri, relativeUri, out UriFormatException? e); if (e != null) return false; } - result ??= CreateHelper(newUriString!, dontEscape, UriKind.Absolute, ref e); + result ??= CreateHelper(newUriString!, dontEscape, UriKind.Absolute); + Debug.Assert(result is null || result.IsAbsoluteUri); result?.DebugSetLeftCtor(); - return e is null && result != null && result.IsAbsoluteUri; + return result is not null; } public string GetComponents(UriComponents components, UriFormat format) @@ -669,9 +658,14 @@ private Uri(Flags flags, UriParser? uriParser, string uri) // // a Uri.TryCreate() method goes through here. // - internal static Uri? CreateHelper(string uriString, bool dontEscape, UriKind uriKind, ref UriFormatException? e, in UriCreationOptions creationOptions = default) + internal static Uri? CreateHelper(string? uriString, bool dontEscape, UriKind uriKind, in UriCreationOptions creationOptions = default) { - if ((int)uriKind < (int)UriKind.RelativeOrAbsolute || (int)uriKind > (int)UriKind.Relative) + if (uriString is null) + { + return null; + } + + if (uriKind is < UriKind.RelativeOrAbsolute or > UriKind.Relative) { throw new ArgumentException(SR.Format(SR.net_uri_InvalidUriKind, uriKind)); } @@ -703,7 +697,7 @@ private Uri(Flags flags, UriParser? uriParser, string uri) // Validate instance using ether built in or a user Parser try { - e = result.InitializeUri(err, uriKind); + UriFormatException? e = result.InitializeUri(err, uriKind); if (e == null) { @@ -713,10 +707,9 @@ private Uri(Flags flags, UriParser? uriParser, string uri) return null; } - catch (UriFormatException ee) + catch (UriFormatException) { - Debug.Assert(!syntax!.IsSimple, "A UriPraser threw on InitializeAndValidate."); - e = ee; + Debug.Assert(!syntax.IsSimple, "A UriPraser threw on InitializeAndValidate."); // A precaution since custom Parser should never throw in this case. return null; } @@ -934,12 +927,12 @@ internal bool IsBaseOfHelper(Uri uriLink) if (uriLink is null) { - UriFormatException? e = null; - - uriLink = CreateHelper(newUriString!, dontEscape, UriKind.Absolute, ref e)!; + uriLink = CreateHelper(newUriString!, dontEscape, UriKind.Absolute)!; - if (e != null) + if (uriLink is null) + { return false; + } } }