Skip to content

Add analyzer to detect HasFlag() and suggest HasFlagFast() replacement - #199

Merged
andrewlock merged 6 commits into
mainfrom
copilot/add-analyzer-for-hasflag
Dec 20, 2025
Merged

Add analyzer to detect HasFlag() and suggest HasFlagFast() replacement#199
andrewlock merged 6 commits into
mainfrom
copilot/add-analyzer-for-hasflag

Conversation

Copilot AI commented Dec 18, 2025

Copy link
Copy Markdown
Contributor

Implements analyzer NEEG005 to detect HasFlag() calls on enums with [EnumExtensions] or [EnumExtensions<T>] attributes and suggest replacing with the generated HasFlagFast() extension method for better performance.

Implementation

  • HasFlagAnalyzer: Detects HasFlag() invocations on qualifying enums at Information severity level
  • HasFlagCodeFixProvider: Replaces HasFlag() with HasFlagFast(), supports batch fixing
  • Follows existing patterns from ToStringAnalyzer and ToStringCodeFixProvider

Example

[EnumExtensions]
[Flags]
public enum FilePermissions 
{
    Read = 1,
    Write = 2,
    Execute = 4
}

// Before
if (permissions.HasFlag(FilePermissions.Read))  // NEEG005: Use HasFlagFast()

// After fix
if (permissions.HasFlagFast(FilePermissions.Read))

Test Coverage

  • 17 test cases covering decorated enums, external enums via [EnumExtensions<T>], non-decorated enums, and various invocation contexts
  • Verified no regressions in existing analyzer tests
Original prompt

This section details on the original issue you should resolve

<issue_title>Create analyzer to detect usages of HasFlag() and suggest replacing with HasFlagFast()</issue_title>
<issue_description>- Only enums which are decorated with [EnumExtensions] or referenced in [EnumExtensions<T> should be considered

  • The analyzer should be written using the same style as existing analyzers, for example the ToStringAnalyzer.
  • A code fixer should be provided, and should similarly have tests
  • The analyzer should be written as Information level
  • The suggestion/fix should use the extension implementation of HasFlagFast()</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 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.

Copilot AI and others added 2 commits December 18, 2025 22:18
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
Copilot AI requested a review from andrewlock December 18, 2025 22:25
@github-actions

github-actions Bot commented Dec 19, 2025

Copy link
Copy Markdown

Test Results

    180 files  ±  0      180 suites  ±0   1h 36m 0s ⏱️ + 23m 25s
 21 728 tests + 17   21 724 ✅ + 17   4 💤 ±0  0 ❌ ±0 
386 844 runs  +378  386 772 ✅ +378  72 💤 ±0  0 ❌ ±0 

Results for commit b855c8c. ± Comparison against base commit a468975.

♻️ This comment has been updated with latest results.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 HasFlagAnalyzer and HasFlagCodeFixProvider following existing analyzer patterns
  • Extracted shared helper methods into AnalyzerHelpers class 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.

Comment thread src/NetEscapades.EnumGenerators/Diagnostics/HasFlagAnalyzer.cs Outdated
Comment thread src/NetEscapades.EnumGenerators/Diagnostics/AnalyzerHelpers.cs Outdated
Comment thread src/NetEscapades.EnumGenerators/Diagnostics/AnalyzerHelpers.cs Outdated
Comment thread src/NetEscapades.EnumGenerators/Diagnostics/AnalyzerHelpers.cs Outdated
Comment thread src/NetEscapades.EnumGenerators/Diagnostics/AnalyzerHelpers.cs

@andrewlock andrewlock left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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

@andrewlock
andrewlock marked this pull request as ready for review December 20, 2025 12:00
@andrewlock
andrewlock merged commit 5cc8b1a into main Dec 20, 2025
5 checks passed
@andrewlock
andrewlock deleted the copilot/add-analyzer-for-hasflag branch December 20, 2025 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create analyzer to detect usages of HasFlag() and suggest replacing with HasFlagFast()

3 participants