apache / apache/lucenenet

Create Roslyn analyzer for converting runtime `Enum.ToString()` calls to `nameof(Enum)`

Open
#1,296 1 comment 0 reactions 0 assignees View on GitHub
approved-rule dev-analyzers is:task
Dominant language
C#
Stars
2.4k
Forks
658
Avg merge
3d 5h
Merged PRs (30d)
9

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Task description

When looking through the codebase, I noticed a few places where we are doing:

```c#
field.PutAttribute(legacyKey, LegacyDocValuesType.BYTES_FIXED_DEREF.ToString());
```

The `BYTES_FIXED_DEREF` is a compile-time enum value, so it would both be more readable, and save an initial allocation if we changed it to:

```c#
field.PutAttribute(legacyKey, nameof(LegacyDocValuesType.BYTES_FIXED_DEREF));
```

I had ChatGPT sketch up the code for what this analyzer and code fix would look like:

https://chatgpt.com/share/69f41082-6ccc-8323-8639-9d4b6ff906f3

Note that this won't work on runtime values of variables that are declared `LegacyDocValuesType`, it is only sensible to do this when referencing the enum value directly as in the above example.

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the linked Roslyn analyzer and code-fix sketch, then locate the project's analyzer entry points and test coverage. Validate the direct enum-member example, while ensuring runtime enum variables are excluded; done means the analyzer offers the conversion and its tests cover the expected diagnostic and fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.