Skip to content

Replace Enum.HasFlag with bitwise operations in WebSockets#127137

Merged
rzikm merged 1 commit intodotnet:mainfrom
cittaz:cleanup/websocket-hasflag
Apr 20, 2026
Merged

Replace Enum.HasFlag with bitwise operations in WebSockets#127137
rzikm merged 1 commit intodotnet:mainfrom
cittaz:cleanup/websocket-hasflag

Conversation

@cittaz
Copy link
Copy Markdown
Contributor

@cittaz cittaz commented Apr 20, 2026

Summary

Replaces four Enum.HasFlag calls in System.Net.WebSockets with the bitwise (value & flag) != 0 idiom used
throughout the rest of the repo.

This follows the explicit guidance from @stephentoub in a prior review on this same file (#107662):

We generally prefer to avoid using HasFlag, even though the JIT does optimize away the boxing in tier 1.

See the original review comment.

That PR removed one HasFlag usage in WebSocketStateHelper.cs; this PR finishes the job by replacing the remaining
occurrences across the library.

It also brings internal consistency to WebSocketStateHelper.cs.
ThrowIfInvalidState already uses the bitwise form (state & validStates) == 0 a few lines below the previous IsValidSendState implementation.

No behavior changes.

Changes

File Change
WebSocketStateHelper.cs IsValidSendState, now matches the bitwise form already used by ThrowIfInvalidState
in the same file
ManagedWebSocket.cs endOfMessage / disableCompression checks in SendAsync
WebSocket.cs SendAsync virtual overload's endOfMessage check

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.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Apr 20, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Member

@rzikm rzikm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@cittaz
Copy link
Copy Markdown
Contributor Author

cittaz commented Apr 20, 2026

@dotnet-policy-service agree

@rzikm
Copy link
Copy Markdown
Member

rzikm commented Apr 20, 2026

Thanks for the contribution!

@rzikm rzikm merged commit 3d73a08 into dotnet:main Apr 20, 2026
98 of 104 checks passed
@cittaz cittaz deleted the cleanup/websocket-hasflag branch April 20, 2026 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Net community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants