From a6523bbb5acc72b31fc782ee17690474a7e41f56 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Wed, 26 Nov 2025 20:44:11 +0100 Subject: [PATCH 1/8] Simplify InitializeUri and remove some dead branches --- .../System.Private.Uri/src/System/UriExt.cs | 180 ++++++++---------- 1 file changed, 82 insertions(+), 98 deletions(-) diff --git a/src/libraries/System.Private.Uri/src/System/UriExt.cs b/src/libraries/System.Private.Uri/src/System/UriExt.cs index e5fc3f1532dc57..f18a0269780be4 100644 --- a/src/libraries/System.Private.Uri/src/System/UriExt.cs +++ b/src/libraries/System.Private.Uri/src/System/UriExt.cs @@ -46,6 +46,16 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatException? e) { DebugAssertInCtor(); + Debug.Assert((err is ParsingError.None) == (_syntax is not null)); + + bool hasUnicode = false; + + if (IriParsing && CheckForUnicodeOrEscapedUnreserved(_string)) + { + _flags |= Flags.HasUnicode; + hasUnicode = true; + _originalUnicodeString = _string; // original string location changed + } if (err == ParsingError.None) { @@ -80,49 +90,93 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep } } } - else if (err > ParsingError.LastErrorOkayForRelativeUris) + else { - //This is a fatal error based solely on scheme name parsing - _string = null!; // make it be invalid Uri - e = GetException(err); + // If we encountered any parsing errors that indicate this may be a relative Uri, + // and we'll allow relative Uri's, then create one. + if (err <= ParsingError.LastErrorOkayForRelativeUris) + { + e = null; + _flags &= Flags.UserEscaped | Flags.HasUnicode; // the only flags that makes sense for a relative uri + if (hasUnicode) + { + // Iri'ze and then normalize relative uris + _string = EscapeUnescapeIri(_originalUnicodeString, 0, _originalUnicodeString.Length, isQuery: false); + } + } + else + { + // This is a fatal error based solely on scheme name parsing + _string = null!; // make it be invalid Uri + e = GetException(err); + } + return; } - bool hasUnicode = false; + Debug.Assert(err == ParsingError.None && _syntax is not null); - if (IriParsing && CheckForUnicodeOrEscapedUnreserved(_string)) + if (_syntax.IsSimple) { - _flags |= Flags.HasUnicode; - hasUnicode = true; - // switch internal strings - _originalUnicodeString = _string; // original string location changed - } + if ((err = PrivateParseMinimal()) != ParsingError.None) + { + if (uriKind != UriKind.Absolute && err <= ParsingError.LastErrorOkayForRelativeUris) + { + // RFC 3986 Section 5.4.2 - http:(relativeUri) may be considered a valid relative Uri. + _syntax = null!; // convert to relative uri + e = null; + _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri + return; + } + else + e = GetException(err); + } + else if (uriKind == UriKind.Relative) + { + // Here we know that we can create an absolute Uri, but the user has requested only a relative one + e = GetException(ParsingError.CannotCreateRelative); + } + else + e = null; - if (_syntax != null) + if (e is null && hasUnicode) + { + // In this scenario we need to parse the whole string + try + { + EnsureParseRemaining(); + } + catch (UriFormatException ex) + { + e = ex; + } + } + } + else { - if (_syntax.IsSimple) + // offer custom parser to create a parsing context + _syntax = _syntax.InternalOnNewUri(); + + // in case they won't call us + _flags |= Flags.UserDrivenParsing; + + // Ask a registered type to validate this uri + _syntax.InternalValidate(this, out e); + + if (e is null) { - if ((err = PrivateParseMinimal()) != ParsingError.None) + if (err != ParsingError.None || InFact(Flags.ErrorOrParsingRecursion)) { - if (uriKind != UriKind.Absolute && err <= ParsingError.LastErrorOkayForRelativeUris) - { - // RFC 3986 Section 5.4.2 - http:(relativeUri) may be considered a valid relative Uri. - _syntax = null!; // convert to relative uri - e = null; - _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri - return; - } - else - e = GetException(err); + // User parser took over on an invalid Uri + // we use = here to clear all parsing flags for a uri that we think is invalid. + _flags = Flags.UserDrivenParsing | (_flags & Flags.UserEscaped); } else if (uriKind == UriKind.Relative) { - // Here we know that we can create an absolute Uri, but the user has requested only a relative one + // Here we know that custom parser can create an absolute Uri, but the user has requested only a + // relative one e = GetException(ParsingError.CannotCreateRelative); } - else - e = null; - // will return from here if (e is null && hasUnicode) { @@ -137,76 +191,6 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep } } } - else - { - // offer custom parser to create a parsing context - _syntax = _syntax.InternalOnNewUri(); - - // in case they won't call us - _flags |= Flags.UserDrivenParsing; - - // Ask a registered type to validate this uri - _syntax.InternalValidate(this, out e); - - if (e != null) - { - // Can we still take it as a relative Uri? - if (uriKind != UriKind.Absolute && err != ParsingError.None - && err <= ParsingError.LastErrorOkayForRelativeUris) - { - _syntax = null!; // convert it to relative - e = null; - _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri - } - } - else // e == null - { - if (err != ParsingError.None || InFact(Flags.ErrorOrParsingRecursion)) - { - // User parser took over on an invalid Uri - // we use = here to clear all parsing flags for a uri that we think is invalid. - _flags = Flags.UserDrivenParsing | (_flags & Flags.UserEscaped); - } - else if (uriKind == UriKind.Relative) - { - // Here we know that custom parser can create an absolute Uri, but the user has requested only a - // relative one - e = GetException(ParsingError.CannotCreateRelative); - } - - if (e is null && hasUnicode) - { - // In this scenario we need to parse the whole string - try - { - EnsureParseRemaining(); - } - catch (UriFormatException ex) - { - e = ex; - } - } - } - // will return from here - } - } - // If we encountered any parsing errors that indicate this may be a relative Uri, - // and we'll allow relative Uri's, then create one. - else if (err != ParsingError.None && uriKind != UriKind.Absolute - && err <= ParsingError.LastErrorOkayForRelativeUris) - { - e = null; - _flags &= (Flags.UserEscaped | Flags.HasUnicode); // the only flags that makes sense for a relative uri - if (hasUnicode) - { - // Iri'ze and then normalize relative uris - _string = EscapeUnescapeIri(_originalUnicodeString, 0, _originalUnicodeString.Length, isQuery: false); - } - } - else - { - _string = null!; // make it be invalid Uri - e = GetException(err); } } From c73795e1a2d83ebd8d705f5ba35f325dca688238 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Wed, 26 Nov 2025 20:46:05 +0100 Subject: [PATCH 2/8] Simplify decision between relative/absolute for IsImplicitFile --- .../System.Private.Uri/src/System/UriExt.cs | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Private.Uri/src/System/UriExt.cs b/src/libraries/System.Private.Uri/src/System/UriExt.cs index f18a0269780be4..247dc977d79a9f 100644 --- a/src/libraries/System.Private.Uri/src/System/UriExt.cs +++ b/src/libraries/System.Private.Uri/src/System/UriExt.cs @@ -61,32 +61,27 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep { if (IsImplicitFile) { - // V1 compat - // A relative Uri wins over implicit UNC path unless the UNC path is of the form "\\something" and - // uriKind != Absolute - // A relative Uri wins over implicit Unix path unless uriKind == Absolute - if (NotAny(Flags.DosPath) && - uriKind != UriKind.Absolute && - ((uriKind == UriKind.Relative || (_string.Length >= 2 && (_string[0] != '\\' || _string[1] != '\\'))) - || (!OperatingSystem.IsWindows() && InFact(Flags.UnixPath)))) + if (uriKind == UriKind.Relative) { - _syntax = null!; //make it be relative Uri + _syntax = null!; // make it be relative Uri _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri e = null; return; - // Otherwise an absolute file Uri wins when it's of the form "\\something" } - // - // V1 compat issue - // We should support relative Uris of the form c:\bla or c:/bla - // - else if (uriKind == UriKind.Relative && InFact(Flags.DosPath)) + + // V1 compat + // A relative Uri wins over implicit UNC path unless the UNC path is of the form "\\something" and + // uriKind != Absolute + // A relative Uri wins over implicit Unix path unless uriKind == Absolute + if (NotAny(Flags.DosPath) && uriKind == UriKind.RelativeOrAbsolute && + ((_string.Length >= 2 && (_string[0] != '\\' || _string[1] != '\\')) + || (!OperatingSystem.IsWindows() && InFact(Flags.UnixPath)))) { _syntax = null!; //make it be relative Uri _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri e = null; return; - // Otherwise an absolute file Uri wins when it's of the form "c:\something" + // Otherwise an absolute file Uri wins when it's of the form "\\something" } } } From fdcd584e28a1f76d9e969053b72bb313f316709c Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Wed, 26 Nov 2025 20:50:28 +0100 Subject: [PATCH 3/8] Use return value instead of out param for the exception --- .../System.Private.Uri/src/System/UriExt.cs | 84 +++++++++---------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/libraries/System.Private.Uri/src/System/UriExt.cs b/src/libraries/System.Private.Uri/src/System/UriExt.cs index 247dc977d79a9f..66ee062e77796a 100644 --- a/src/libraries/System.Private.Uri/src/System/UriExt.cs +++ b/src/libraries/System.Private.Uri/src/System/UriExt.cs @@ -38,12 +38,12 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre ParsingError err = ParseScheme(_string, ref _flags, ref _syntax!); - InitializeUri(err, uriKind, out UriFormatException? e); + UriFormatException? e = InitializeUri(err, uriKind); if (e != null) throw e; } - private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatException? e) + private UriFormatException? InitializeUri(ParsingError err, UriKind uriKind) { DebugAssertInCtor(); Debug.Assert((err is ParsingError.None) == (_syntax is not null)); @@ -65,8 +65,7 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep { _syntax = null!; // make it be relative Uri _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri - e = null; - return; + return null; } // V1 compat @@ -79,8 +78,7 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep { _syntax = null!; //make it be relative Uri _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri - e = null; - return; + return null; // Otherwise an absolute file Uri wins when it's of the form "\\something" } } @@ -91,22 +89,20 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep // and we'll allow relative Uri's, then create one. if (err <= ParsingError.LastErrorOkayForRelativeUris) { - e = null; _flags &= Flags.UserEscaped | Flags.HasUnicode; // the only flags that makes sense for a relative uri if (hasUnicode) { // Iri'ze and then normalize relative uris _string = EscapeUnescapeIri(_originalUnicodeString, 0, _originalUnicodeString.Length, isQuery: false); } + return null; } else { // This is a fatal error based solely on scheme name parsing _string = null!; // make it be invalid Uri - e = GetException(err); + return GetException(err); } - - return; } Debug.Assert(err == ParsingError.None && _syntax is not null); @@ -119,22 +115,21 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep { // RFC 3986 Section 5.4.2 - http:(relativeUri) may be considered a valid relative Uri. _syntax = null!; // convert to relative uri - e = null; _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri - return; + return null; } else - e = GetException(err); + { + return GetException(err); + } } else if (uriKind == UriKind.Relative) { // Here we know that we can create an absolute Uri, but the user has requested only a relative one - e = GetException(ParsingError.CannotCreateRelative); + return GetException(ParsingError.CannotCreateRelative); } - else - e = null; - if (e is null && hasUnicode) + if (hasUnicode) { // In this scenario we need to parse the whole string try @@ -143,7 +138,7 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep } catch (UriFormatException ex) { - e = ex; + return ex; } } } @@ -156,37 +151,42 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep _flags |= Flags.UserDrivenParsing; // Ask a registered type to validate this uri - _syntax.InternalValidate(this, out e); + _syntax.InternalValidate(this, out UriFormatException? e); - if (e is null) + if (e is not null) { - if (err != ParsingError.None || InFact(Flags.ErrorOrParsingRecursion)) - { - // User parser took over on an invalid Uri - // we use = here to clear all parsing flags for a uri that we think is invalid. - _flags = Flags.UserDrivenParsing | (_flags & Flags.UserEscaped); - } - else if (uriKind == UriKind.Relative) + return e; + } + + if (err != ParsingError.None || InFact(Flags.ErrorOrParsingRecursion)) + { + // User parser took over on an invalid Uri + // we use = here to clear all parsing flags for a uri that we think is invalid. + _flags = Flags.UserDrivenParsing | (_flags & Flags.UserEscaped); + } + else if (uriKind == UriKind.Relative) + { + // Here we know that custom parser can create an absolute Uri, but the user has requested only a + // relative one + return GetException(ParsingError.CannotCreateRelative); + } + + if (hasUnicode) + { + // In this scenario we need to parse the whole string + try { - // Here we know that custom parser can create an absolute Uri, but the user has requested only a - // relative one - e = GetException(ParsingError.CannotCreateRelative); + EnsureParseRemaining(); } - - if (e is null && hasUnicode) + catch (UriFormatException ex) { - // In this scenario we need to parse the whole string - try - { - EnsureParseRemaining(); - } - catch (UriFormatException ex) - { - e = ex; - } + return ex; } } } + + // We have a valid absolute Uri. + return null; } /// SearchValues for all ASCII characters other than % @@ -721,7 +721,7 @@ private Uri(Flags flags, UriParser? uriParser, string uri) // Validate instance using ether built in or a user Parser try { - result.InitializeUri(err, uriKind, out e); + e = result.InitializeUri(err, uriKind); if (e == null) { From 7400e9a98c7397f2ea88592d9edeb42c73dfc655 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Wed, 26 Nov 2025 20:51:41 +0100 Subject: [PATCH 4/8] Remove duplicated logic in both branches --- .../System.Private.Uri/src/System/UriExt.cs | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/libraries/System.Private.Uri/src/System/UriExt.cs b/src/libraries/System.Private.Uri/src/System/UriExt.cs index 66ee062e77796a..8c7632a19c7423 100644 --- a/src/libraries/System.Private.Uri/src/System/UriExt.cs +++ b/src/libraries/System.Private.Uri/src/System/UriExt.cs @@ -123,24 +123,12 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre return GetException(err); } } - else if (uriKind == UriKind.Relative) + + if (uriKind == UriKind.Relative) { // Here we know that we can create an absolute Uri, but the user has requested only a relative one return GetException(ParsingError.CannotCreateRelative); } - - if (hasUnicode) - { - // In this scenario we need to parse the whole string - try - { - EnsureParseRemaining(); - } - catch (UriFormatException ex) - { - return ex; - } - } } else { @@ -170,18 +158,18 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre // relative one return GetException(ParsingError.CannotCreateRelative); } + } - if (hasUnicode) + if (hasUnicode) + { + // In this scenario we need to parse the whole string + try { - // In this scenario we need to parse the whole string - try - { - EnsureParseRemaining(); - } - catch (UriFormatException ex) - { - return ex; - } + EnsureParseRemaining(); + } + catch (UriFormatException ex) + { + return ex; } } From 393f78f7cb5347779d8841797e622060e992821f Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Wed, 26 Nov 2025 20:55:25 +0100 Subject: [PATCH 5/8] Invert condition for early returns --- .../System.Private.Uri/src/System/UriExt.cs | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/libraries/System.Private.Uri/src/System/UriExt.cs b/src/libraries/System.Private.Uri/src/System/UriExt.cs index 8c7632a19c7423..3e47ad27b6185b 100644 --- a/src/libraries/System.Private.Uri/src/System/UriExt.cs +++ b/src/libraries/System.Private.Uri/src/System/UriExt.cs @@ -21,7 +21,7 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre { DebugAssertInCtor(); - if ((int)uriKind < (int)UriKind.RelativeOrAbsolute || (int)uriKind > (int)UriKind.Relative) + if (uriKind is < UriKind.RelativeOrAbsolute or > UriKind.Relative) { throw new ArgumentException(SR.Format(SR.net_uri_InvalidUriKind, uriKind)); } @@ -57,37 +57,11 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre _originalUnicodeString = _string; // original string location changed } - if (err == ParsingError.None) - { - if (IsImplicitFile) - { - if (uriKind == UriKind.Relative) - { - _syntax = null!; // make it be relative Uri - _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri - return null; - } - - // V1 compat - // A relative Uri wins over implicit UNC path unless the UNC path is of the form "\\something" and - // uriKind != Absolute - // A relative Uri wins over implicit Unix path unless uriKind == Absolute - if (NotAny(Flags.DosPath) && uriKind == UriKind.RelativeOrAbsolute && - ((_string.Length >= 2 && (_string[0] != '\\' || _string[1] != '\\')) - || (!OperatingSystem.IsWindows() && InFact(Flags.UnixPath)))) - { - _syntax = null!; //make it be relative Uri - _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri - return null; - // Otherwise an absolute file Uri wins when it's of the form "\\something" - } - } - } - else + if (err != ParsingError.None) { // If we encountered any parsing errors that indicate this may be a relative Uri, // and we'll allow relative Uri's, then create one. - if (err <= ParsingError.LastErrorOkayForRelativeUris) + if (uriKind != UriKind.Absolute && err <= ParsingError.LastErrorOkayForRelativeUris) { _flags &= Flags.UserEscaped | Flags.HasUnicode; // the only flags that makes sense for a relative uri if (hasUnicode) @@ -105,7 +79,31 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre } } - Debug.Assert(err == ParsingError.None && _syntax is not null); + Debug.Assert(_syntax is not null); + + if (IsImplicitFile) + { + if (uriKind == UriKind.Relative) + { + _syntax = null!; // make it be relative Uri + _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri + return null; + } + + // V1 compat + // A relative Uri wins over implicit UNC path unless the UNC path is of the form "\\something" and + // uriKind != Absolute + // A relative Uri wins over implicit Unix path unless uriKind == Absolute + if (NotAny(Flags.DosPath) && uriKind == UriKind.RelativeOrAbsolute && + ((_string.Length >= 2 && (_string[0] != '\\' || _string[1] != '\\')) + || (!OperatingSystem.IsWindows() && InFact(Flags.UnixPath)))) + { + _syntax = null!; //make it be relative Uri + _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri + return null; + // Otherwise an absolute file Uri wins when it's of the form "\\something" + } + } if (_syntax.IsSimple) { From 2f968672967808c1a1e0935fffb12920df4aa465 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Wed, 26 Nov 2025 21:10:22 +0100 Subject: [PATCH 6/8] Simplify GetException helper, remove unused enum entry --- .../System.Private.Uri/src/System/Uri.cs | 51 ++++++++----------- .../src/System/UriEnumTypes.cs | 1 - 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/libraries/System.Private.Uri/src/System/Uri.cs b/src/libraries/System.Private.Uri/src/System/Uri.cs index 5128aaf44bf200..7f5ea9df6966ee 100644 --- a/src/libraries/System.Private.Uri/src/System/Uri.cs +++ b/src/libraries/System.Private.Uri/src/System/Uri.cs @@ -700,41 +700,32 @@ private static void GetCombinedString(Uri baseUri, string relativeStr, } } - private static UriFormatException? GetException(ParsingError err) + private static UriFormatException GetException(ParsingError err) { - switch (err) + Debug.Assert(err != ParsingError.None); + + string message = err switch { - case ParsingError.None: - return null; // Could be OK for Relative Uri - case ParsingError.BadFormat: - return new UriFormatException(SR.net_uri_BadFormat); - case ParsingError.BadScheme: - return new UriFormatException(SR.net_uri_BadScheme); - case ParsingError.BadAuthority: - return new UriFormatException(SR.net_uri_BadAuthority); - case ParsingError.EmptyUriString: - return new UriFormatException(SR.net_uri_EmptyUri); + ParsingError.BadFormat => SR.net_uri_BadFormat, + ParsingError.BadScheme => SR.net_uri_BadScheme, + ParsingError.BadAuthority => SR.net_uri_BadAuthority, + ParsingError.EmptyUriString => SR.net_uri_EmptyUri, + // Fatal - case ParsingError.SchemeLimit: - return new UriFormatException(SR.net_uri_SchemeLimit); - case ParsingError.MustRootedPath: - return new UriFormatException(SR.net_uri_MustRootedPath); + ParsingError.SchemeLimit => SR.net_uri_SchemeLimit, + ParsingError.MustRootedPath => SR.net_uri_MustRootedPath, + // Derived class controllable - case ParsingError.BadHostName: - return new UriFormatException(SR.net_uri_BadHostName); - case ParsingError.NonEmptyHost: //unix-only - return new UriFormatException(SR.net_uri_BadFormat); - case ParsingError.BadPort: - return new UriFormatException(SR.net_uri_BadPort); - case ParsingError.BadAuthorityTerminator: - return new UriFormatException(SR.net_uri_BadAuthorityTerminator); - case ParsingError.CannotCreateRelative: - return new UriFormatException(SR.net_uri_CannotCreateRelative); - default: - break; - } - return new UriFormatException(SR.net_uri_BadFormat); + ParsingError.BadHostName => SR.net_uri_BadHostName, + ParsingError.BadPort => SR.net_uri_BadPort, + ParsingError.BadAuthorityTerminator => SR.net_uri_BadAuthorityTerminator, + ParsingError.CannotCreateRelative => SR.net_uri_CannotCreateRelative, + + _ => throw new UnreachableException() + }; + + return new UriFormatException(message); } public string AbsolutePath diff --git a/src/libraries/System.Private.Uri/src/System/UriEnumTypes.cs b/src/libraries/System.Private.Uri/src/System/UriEnumTypes.cs index 17d9456ba77328..d127dfbf60af9b 100644 --- a/src/libraries/System.Private.Uri/src/System/UriEnumTypes.cs +++ b/src/libraries/System.Private.Uri/src/System/UriEnumTypes.cs @@ -73,7 +73,6 @@ internal enum ParsingError // derived class controlled BadHostName, - NonEmptyHost, // unix only BadPort, BadAuthorityTerminator, From be7b72483da9266535f042eec3399cef00277a74 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Wed, 26 Nov 2025 21:36:21 +0100 Subject: [PATCH 7/8] Remove unnecessary else blocks --- .../System.Private.Uri/src/System/UriExt.cs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Private.Uri/src/System/UriExt.cs b/src/libraries/System.Private.Uri/src/System/UriExt.cs index 3e47ad27b6185b..6612dd9772b87b 100644 --- a/src/libraries/System.Private.Uri/src/System/UriExt.cs +++ b/src/libraries/System.Private.Uri/src/System/UriExt.cs @@ -71,12 +71,10 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre } return null; } - else - { - // This is a fatal error based solely on scheme name parsing - _string = null!; // make it be invalid Uri - return GetException(err); - } + + // This is a fatal error based solely on scheme name parsing + _string = null!; // make it be invalid Uri + return GetException(err); } Debug.Assert(_syntax is not null); @@ -116,10 +114,8 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre _flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri return null; } - else - { - return GetException(err); - } + + return GetException(err); } if (uriKind == UriKind.Relative) From f499b0fa70fa07a56f1b2012f19c2634e3c70903 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Wed, 26 Nov 2025 21:39:40 +0100 Subject: [PATCH 8/8] Remove always-false check --- src/libraries/System.Private.Uri/src/System/UriExt.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.Uri/src/System/UriExt.cs b/src/libraries/System.Private.Uri/src/System/UriExt.cs index 6612dd9772b87b..51dcb3694dc2f0 100644 --- a/src/libraries/System.Private.Uri/src/System/UriExt.cs +++ b/src/libraries/System.Private.Uri/src/System/UriExt.cs @@ -140,7 +140,7 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre return e; } - if (err != ParsingError.None || InFact(Flags.ErrorOrParsingRecursion)) + if (InFact(Flags.ErrorOrParsingRecursion)) { // User parser took over on an invalid Uri // we use = here to clear all parsing flags for a uri that we think is invalid.