-
Notifications
You must be signed in to change notification settings - Fork 653
Description
Is there an existing issue for this?
- I have searched the existing issues
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.OrdinalorStringComparison.OrdinalIgnoreCase. Violations should emit a compiler error. - Span overloads should only be called with
StringComparison.OrdinalorStringComparison.OrdinalIgnoreCasewhen applicable. Violations should emit a compiler warning. - Single-character strings should use the
charoverload ofStartsWith/EndsWith/IndexOf/LastIndexOf. Violations should emit an informational diagnostic. - The rules should also apply to custom types:
J2N.Text.StringBuilderExtensions,OpenStringBuilder, andValueStringBuilder.
Each analyzer should include:
- Unit tests covering all edge cases
- Sample usage in
Lucene.Net.CodeAnalysis.Samplefor 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.