Replace Enum.HasFlag with bitwise operations in WebSockets#127137
Merged
rzikm merged 1 commit intodotnet:mainfrom Apr 20, 2026
Merged
Replace Enum.HasFlag with bitwise operations in WebSockets#127137rzikm merged 1 commit intodotnet:mainfrom
rzikm merged 1 commit intodotnet:mainfrom
Conversation
HasFlag depends on JIT pattern recognition to match the bitwise-AND idiom used throughout the rest of dotnet/runtime. These replacements bring the WebSockets code in line with the house style — for example, WebSocketStateHelper.ThrowIfInvalidState already uses the bitwise form a few lines below the changed IsValidSendState.
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
Contributor
Author
|
@dotnet-policy-service agree |
This was referenced Apr 20, 2026
Open
Member
|
Thanks for the contribution! |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Replaces four
Enum.HasFlagcalls inSystem.Net.WebSocketswith the bitwise(value & flag) != 0idiom usedthroughout the rest of the repo.
This follows the explicit guidance from @stephentoub in a prior review on this same file (#107662):
See the original review comment.
That PR removed one
HasFlagusage inWebSocketStateHelper.cs; this PR finishes the job by replacing the remainingoccurrences across the library.
It also brings internal consistency to
WebSocketStateHelper.cs.ThrowIfInvalidStatealready uses the bitwise form(state & validStates) == 0a few lines below the previousIsValidSendStateimplementation.No behavior changes.
Changes
WebSocketStateHelper.csIsValidSendState, now matches the bitwise form already used byThrowIfInvalidStateManagedWebSocket.csendOfMessage/disableCompressionchecks inSendAsyncWebSocket.csSendAsyncvirtual overload'sendOfMessagecheck