Skip to content

Add Roslyn Analyzers for String/Span/Char Overloads in Lucene.NET #1211

@NehanPathan

Description

@NehanPathan

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:

  1. String overloads must be called with StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase. Violations should emit a compiler error.
  2. Span overloads should only be called with StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when applicable. Violations should emit a compiler warning.
  3. Single-character strings should use the char overload of StartsWith/EndsWith/IndexOf/LastIndexOf. Violations should emit an informational diagnostic.
  4. 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.


Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions