-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Simplify InitializeUri and remove some unreachable branches #122001
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
Conversation
|
Tagging subscribers to this area: @dotnet/ncl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
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