Add analyzer to detect HasFlag() and suggest HasFlagFast() replacement - #199
Merged
Conversation
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add analyzer to detect HasFlag usages and suggest HasFlagFast
Add analyzer to detect HasFlag() and suggest HasFlagFast() replacement
Dec 18, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
This PR implements analyzer NEEG005 to detect HasFlag() method calls on enums decorated with [EnumExtensions] or referenced via [EnumExtensions<T>] attributes, and suggests replacing them with the generated HasFlagFast() extension method for better performance.
Key Changes:
- New
HasFlagAnalyzerandHasFlagCodeFixProviderfollowing existing analyzer patterns - Extracted shared helper methods into
AnalyzerHelpersclass to reduce code duplication between analyzers - Comprehensive test coverage with 17 test cases covering various scenarios
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
src/NetEscapades.EnumGenerators/Diagnostics/HasFlagAnalyzer.cs |
New analyzer detecting HasFlag() calls on qualifying enums at Information severity |
src/NetEscapades.EnumGenerators/Diagnostics/HasFlagCodeFixProvider.cs |
Code fix provider to replace HasFlag() with HasFlagFast(), supports batch fixing |
src/NetEscapades.EnumGenerators/Diagnostics/AnalyzerHelpers.cs |
New helper class extracting common logic for identifying enums with extensions |
src/NetEscapades.EnumGenerators/Diagnostics/ToStringAnalyzer.cs |
Refactored to use shared helper methods from AnalyzerHelpers |
tests/NetEscapades.EnumGenerators.Tests/HasFlagAnalyzerTests.cs |
Comprehensive test coverage with 17 test cases for various scenarios |
tests/NetEscapades.EnumGenerators.IntegrationTests/AnalyzerTests.cs |
Integration test validating NEEG005 diagnostic can be suppressed |
tests/NetEscapades.EnumGenerators.IntegrationTests/.editorconfig |
Added NEEG005 as error-level diagnostic |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
andrewlock
approved these changes
Dec 20, 2025
andrewlock
left a comment
Owner
There was a problem hiding this comment.
Looks good enough to me. Realized we need to handle the case where we need to add using statements etc, but will handle that in a separate PR
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.
Implements analyzer
NEEG005to detectHasFlag()calls on enums with[EnumExtensions]or[EnumExtensions<T>]attributes and suggest replacing with the generatedHasFlagFast()extension method for better performance.Implementation
HasFlag()invocations on qualifying enums at Information severity levelHasFlag()withHasFlagFast(), supports batch fixingToStringAnalyzerandToStringCodeFixProviderExample
Test Coverage
[EnumExtensions<T>], non-decorated enums, and various invocation contextsOriginal prompt
HasFlag()and suggest replacing withHasFlagFast()#188💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.