Simplify InitializeUri and remove some unreachable branches#122001
Merged
MihaZupan merged 8 commits intodotnet:mainfrom Nov 28, 2025
Merged
Simplify InitializeUri and remove some unreachable branches#122001MihaZupan merged 8 commits intodotnet:mainfrom
MihaZupan merged 8 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/ncl |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the InitializeUri method to improve readability and remove unreachable code paths without changing behavior. The changes reorganize the initialization logic to make it clearer which conditions are mutually exclusive and to eliminate dead branches.
Key changes:
- Reorganized
InitializeUrito handle errors first, then process valid absolute URIs - Changed
InitializeUrito returnUriFormatException?instead of using anoutparameter - Converted
GetExceptionmethod to use a switch expression withUnreachableExceptionfor the default case
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| UriExt.cs | Refactored InitializeUri method to simplify control flow, removed unreachable branches, and changed exception handling from out parameter to return value |
| UriEnumTypes.cs | Removed unused NonEmptyHost enum value from ParsingError |
| Uri.cs | Converted GetException from a switch statement to a switch expression and changed return type from nullable to non-nullable |
rokonec
approved these changes
Nov 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current initialization logic is written in a way that makes it hard to read, and hides inconsistencise / unreachable logic.
This PR only shuffles things around & removes some unreachable branches without changing the behavior.
The aim is to make future changes that do change behavior actually reviewable.
Some context:
_syntaxto represent the Uri scheme. It's alwaysnullfor relative Uris and set for absolute onesInitializeUriwe try to parse the scheme. This populates bothParsingError errand_syntax. These two are mutually exclusive - iferris set it means that_syntax == nulland vice-versa.I've split this up into smaller commits:
err != ParsingError.Noneup to the top, allowing us to drop the extra nesting of theif (_syntax != null)branch below it (it'll always be true).err != ParsingError.Nonewhen we know it can't beNotAny(Flags.DosPath) && uriKind == UriKind.Relativein one place andInFact(Flags.DosPath) && uriKind == UriKind.Relativein another, while doing the same thing in both cases. I simplified that condition to just check forUriKind.Relative.Relativecase, theuriKind != UriKind.Absolutecheck becomes== UriKind.RelativeOrAbsolute, which makes it clearer what we're doingUriFormatExceptionto the return value instead of anoutparamifandelseand moves it after the branch