-
Notifications
You must be signed in to change notification settings - Fork 2k
C#: Extract indexer when accessing a span with a range. #21877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
michaelnebel
wants to merge
10
commits into
github:main
Choose a base branch
from
michaelnebel:csharp/spanaccessrange
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−11
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7771ee5
C#: Extract the indexer as the call target when using range expressio…
michaelnebel fd24d9d
C#: Update DB quality expected test output.
michaelnebel 34f69f2
C#: Add change-note.
michaelnebel 4231ffc
C#: Extract Slice and Substring operations and synthesize the call ar…
michaelnebel 8d0575c
C#: Add test case and expected output.
michaelnebel bd1392e
C#: Update change note.
michaelnebel 48b96a8
C#: Add more testcases and update expected output.
michaelnebel dc37e6f
C#: Add case for open ended range.
michaelnebel 5e0baa4
C#: Add test case.
michaelnebel da699b9
C#: Address other CoPilot review comments.
michaelnebel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Improved extraction of range-access expressions on spans and strings (for example, `a[0..3]`). These expressions are now extracted as `Slice` (span) or `Substring` (string) calls. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System; | ||
|
|
||
| public class C | ||
| { | ||
| public void M(int a, int b) | ||
| { | ||
| var s = "hello world"; | ||
| var sub1 = s[1..a]; | ||
| var sub2 = s[..2]; | ||
| var sub3 = s[3..]; | ||
| var sub4 = s[..^4]; | ||
| var sub5 = s[a..^b]; | ||
| var sub6 = s[..]; | ||
|
|
||
| Span<int> sp = null; | ||
| var slice1 = sp[5..a]; | ||
| var slice2 = sp[..6]; | ||
| var slice3 = sp[7..]; | ||
| var slice4 = sp[..^8]; | ||
| var slice5 = sp[a..^b]; | ||
| var slice6 = sp[..]; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| methodCalls | ||
| | Slice.cs:8:20:8:26 | call to method Substring | Substring(int, int) | 0 | 1 | | ||
| | Slice.cs:8:20:8:26 | call to method Substring | Substring(int, int) | 1 | access to parameter a - 1 | | ||
| | Slice.cs:9:20:9:25 | call to method Substring | Substring(int, int) | 0 | 0 | | ||
| | Slice.cs:9:20:9:25 | call to method Substring | Substring(int, int) | 1 | 2 | | ||
| | Slice.cs:10:20:10:25 | call to method Substring | Substring(int, int) | 0 | 3 | | ||
| | Slice.cs:10:20:10:25 | call to method Substring | Substring(int, int) | 1 | access to property Length - 3 | | ||
| | Slice.cs:11:20:11:26 | call to method Substring | Substring(int, int) | 0 | 0 | | ||
| | Slice.cs:11:20:11:26 | call to method Substring | Substring(int, int) | 1 | access to property Length - 4 | | ||
| | Slice.cs:12:20:12:27 | call to method Substring | Substring(int, int) | 0 | access to parameter a | | ||
| | Slice.cs:12:20:12:27 | call to method Substring | Substring(int, int) | 1 | access to property Length - access to parameter b - access to parameter a | | ||
| | Slice.cs:13:20:13:24 | call to method Substring | Substring(int, int) | 0 | 0 | | ||
| | Slice.cs:13:20:13:24 | call to method Substring | Substring(int, int) | 1 | access to property Length | | ||
| | Slice.cs:16:22:16:29 | call to method Slice | Slice(int, int) | 0 | 5 | | ||
| | Slice.cs:16:22:16:29 | call to method Slice | Slice(int, int) | 1 | access to parameter a - 5 | | ||
| | Slice.cs:17:22:17:28 | call to method Slice | Slice(int, int) | 0 | 0 | | ||
| | Slice.cs:17:22:17:28 | call to method Slice | Slice(int, int) | 1 | 6 | | ||
| | Slice.cs:18:22:18:28 | call to method Slice | Slice(int, int) | 0 | 7 | | ||
| | Slice.cs:18:22:18:28 | call to method Slice | Slice(int, int) | 1 | access to property Length - 7 | | ||
| | Slice.cs:19:22:19:29 | call to method Slice | Slice(int, int) | 0 | 0 | | ||
| | Slice.cs:19:22:19:29 | call to method Slice | Slice(int, int) | 1 | access to property Length - 8 | | ||
| | Slice.cs:20:22:20:30 | call to method Slice | Slice(int, int) | 0 | access to parameter a | | ||
| | Slice.cs:20:22:20:30 | call to method Slice | Slice(int, int) | 1 | access to property Length - access to parameter b - access to parameter a | | ||
| | Slice.cs:21:22:21:27 | call to method Slice | Slice(int, int) | 0 | 0 | | ||
| | Slice.cs:21:22:21:27 | call to method Slice | Slice(int, int) | 1 | access to property Length | | ||
| propertyCalls | ||
| | Slice.cs:10:20:10:25 | access to property Length | Slice.cs:10:20:10:20 | access to local variable s | | ||
| | Slice.cs:11:20:11:26 | access to property Length | Slice.cs:11:20:11:20 | access to local variable s | | ||
| | Slice.cs:12:20:12:27 | access to property Length | Slice.cs:12:20:12:20 | access to local variable s | | ||
| | Slice.cs:13:20:13:24 | access to property Length | Slice.cs:13:20:13:20 | access to local variable s | | ||
| | Slice.cs:18:22:18:28 | access to property Length | Slice.cs:18:22:18:23 | access to local variable sp | | ||
| | Slice.cs:19:22:19:29 | access to property Length | Slice.cs:19:22:19:23 | access to local variable sp | | ||
| | Slice.cs:20:22:20:30 | access to property Length | Slice.cs:20:22:20:23 | access to local variable sp | | ||
| | Slice.cs:21:22:21:27 | access to property Length | Slice.cs:21:22:21:23 | access to local variable sp | |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import csharp | ||
|
|
||
| private string printExpr(Expr e) { | ||
| e = | ||
| any(SubExpr sub | | ||
| result = printExpr(sub.getLeftOperand()) + " - " + printExpr(sub.getRightOperand()) | ||
| ) | ||
| or | ||
| not e instanceof SubExpr and | ||
| result = e.toString() | ||
| } | ||
|
|
||
| query predicate methodCalls(MethodCall mc, string m, int i, string arg) { | ||
| m = mc.getTarget().toStringWithTypes() and | ||
| arg = printExpr(mc.getArgument(i)) | ||
| } | ||
|
|
||
| query predicate propertyCalls(PropertyCall p, Expr qualifier) { qualifier = p.getQualifier() } |
2 changes: 0 additions & 2 deletions
2
csharp/ql/test/query-tests/Telemetry/DatabaseQuality/IsNotOkayCall.expected
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +0,0 @@ | ||
| | Quality.cs:26:19:26:26 | access to indexer | Call without target $@. | Quality.cs:26:19:26:26 | access to indexer | access to indexer | | ||
| | Quality.cs:29:21:29:27 | access to indexer | Call without target $@. | Quality.cs:29:21:29:27 | access to indexer | access to indexer | | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.