C#: Extract indexer when accessing a span with a range.#21877
Open
michaelnebel wants to merge 3 commits into
Open
C#: Extract indexer when accessing a span with a range.#21877michaelnebel wants to merge 3 commits into
michaelnebel wants to merge 3 commits into
Conversation
4eec760 to
9167814
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the C# extractor’s handling of element-access expressions that use range syntax (for example, a[0..3]) when Roslyn lowers them into method calls (for example, Slice(...)). The goal is to preserve the source-level “indexer access” shape by still recording an indexer as the call target, improving database-quality telemetry.
Changes:
- Added extractor special-casing to recover an indexer symbol even when Roslyn binds the element access to a method symbol.
- Updated the Telemetry/DatabaseQuality query test inputs and expected outputs to reflect the improved extraction.
- Added a C# library change note documenting the analysis-impacting extraction improvement.
Show a summary per file
| File | Description |
|---|---|
| csharp/ql/test/query-tests/Telemetry/DatabaseQuality/Quality.cs | Updates test code to treat range element access as properly-targeted indexer calls. |
| csharp/ql/test/query-tests/Telemetry/DatabaseQuality/NoTarget.expected | Removes expected “missing call target” results now that targets are extracted. |
| csharp/ql/test/query-tests/Telemetry/DatabaseQuality/IsNotOkayCall.expected | Eliminates expected “not ok” call-target results for the fixed cases. |
| csharp/ql/lib/change-notes/2026-05-21-spanaccess-range.md | Documents the extraction change as a minor analysis improvement. |
| csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ElementAccess.cs | Implements the new symbol recovery logic for range-based element access. |
Copilot's findings
- Files reviewed: 5/5 changed files
- Comments generated: 1
Comment on lines
+32
to
+45
| if (symbol is IMethodSymbol method) | ||
| { | ||
| var indexers = method | ||
| .ContainingType | ||
| .GetMembers() | ||
| .OfType<IPropertySymbol>() | ||
| .Where(p => p.IsIndexer); | ||
|
|
||
| var intIndexer = indexers | ||
| .Where(i => i.Parameters.Length == 1 && i.Parameters[0].Type.SpecialType == SpecialType.System_Int32) | ||
| .FirstOrDefault(); | ||
|
|
||
| return intIndexer; | ||
| } |
Contributor
Author
There was a problem hiding this comment.
I think it is reasonable to assume that Roslyn only translates a[range] into a method call for integer range expressions (and thus we implicitly want to extract the indexer with an int parameter).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improves the extraction of span range-access expressions (such as
a[0..3]).Roslyn translates
a[0..3]intoa.Slice(0,3)for spans (and something similar for strings). In this PR we introduce some special handling in the extractor for such cases and still considera[0..3]as indexer calls (as this most closely resembles the syntax).