Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Currently, calls to StartsWith(), EndsWith(), IndexOf(), and LastIndexOf() on string, Span<char>, ReadOnlySpan<char>, Memory<char>, and ReadOnlyMemory<char> may be made without explicitly specifying StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase. This can lead to subtle bugs due to culture-sensitive comparisons. Additionally, single-character string arguments are sometimes used instead of char overloads, which is less efficient. The project also has custom types like J2N.Text.StringBuilderExtensions, OpenStringBuilder, and ValueStringBuilder that require similar enforcement.
Describe the solution you'd like
Create new Roslyn analyzers for Lucene.NET that enforce the following rules:
- String overloads must be called with
StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase. Violations should emit a compiler error.
- Span overloads should only be called with
StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when applicable. Violations should emit a compiler warning.
- Single-character strings should use the
char overload of StartsWith/EndsWith/IndexOf/LastIndexOf. Violations should emit an informational diagnostic.
- The rules should also apply to custom types:
J2N.Text.StringBuilderExtensions, OpenStringBuilder, and ValueStringBuilder.
Each analyzer should include:
- Unit tests covering all edge cases
- Sample usage in
Lucene.Net.CodeAnalysis.Sample for IDE/live testing
- Proper diagnostic ID assignment in
DiagnosticCategoryAndIdRanges.txt
- Corresponding entries in
AnalyzerReleases.Unshipped.md
Code fixes:
Where applicable, implement Roslyn code fix providers so the IDE (VS/VS Code) can automatically suggest fixes (lightbulb) when violations are detected.
Alternative solutions considered:
- Existing analyzers (SonarCloud, Microsoft) are insufficient as they do not handle all custom types or escaped characters in single-character strings.
Additional context
This task aligns with Lucene.NET’s goal to enforce safe, consistent, and performant API usage while following .NET best practices. Proper analyzers and code fixes will help contributors avoid subtle bugs and improve code quality.
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Currently, calls to
StartsWith(),EndsWith(),IndexOf(), andLastIndexOf()onstring,Span<char>,ReadOnlySpan<char>,Memory<char>, andReadOnlyMemory<char>may be made without explicitly specifyingStringComparison.OrdinalorStringComparison.OrdinalIgnoreCase. This can lead to subtle bugs due to culture-sensitive comparisons. Additionally, single-character string arguments are sometimes used instead ofcharoverloads, which is less efficient. The project also has custom types likeJ2N.Text.StringBuilderExtensions,OpenStringBuilder, andValueStringBuilderthat require similar enforcement.Describe the solution you'd like
Create new Roslyn analyzers for Lucene.NET that enforce the following rules:
StringComparison.OrdinalorStringComparison.OrdinalIgnoreCase. Violations should emit a compiler error.StringComparison.OrdinalorStringComparison.OrdinalIgnoreCasewhen applicable. Violations should emit a compiler warning.charoverload ofStartsWith/EndsWith/IndexOf/LastIndexOf. Violations should emit an informational diagnostic.J2N.Text.StringBuilderExtensions,OpenStringBuilder, andValueStringBuilder.Each analyzer should include:
Lucene.Net.CodeAnalysis.Samplefor IDE/live testingDiagnosticCategoryAndIdRanges.txtAnalyzerReleases.Unshipped.mdCode fixes:
Where applicable, implement Roslyn code fix providers so the IDE (VS/VS Code) can automatically suggest fixes (lightbulb) when violations are detected.
Alternative solutions considered:
Additional context
This task aligns with Lucene.NET’s goal to enforce safe, consistent, and performant API usage while following .NET best practices. Proper analyzers and code fixes will help contributors avoid subtle bugs and improve code quality.