Skip to content

Commit 20752c8

Browse files
committed
C#: Address review comments
1 parent f1bb8b8 commit 20752c8

File tree

11 files changed

+37
-3
lines changed

11 files changed

+37
-3
lines changed

csharp/ql/src/semmle/code/csharp/Stmt.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,11 +1295,11 @@ class LocalConstantDeclStmt extends LocalVariableDeclStmt, @const_decl_stmt {
12951295
class UsingDeclStmt extends LocalVariableDeclStmt, UsingStmt, @using_decl_stmt {
12961296
override string toString() { result = "using ... ...;" }
12971297

1298-
override LocalConstantDeclExpr getAVariableDeclExpr() {
1298+
override LocalVariableDeclExpr getAVariableDeclExpr() {
12991299
result = LocalVariableDeclStmt.super.getAVariableDeclExpr()
13001300
}
13011301

1302-
override LocalConstantDeclExpr getVariableDeclExpr(int n) {
1302+
override LocalVariableDeclExpr getVariableDeclExpr(int n) {
13031303
result = LocalVariableDeclStmt.super.getVariableDeclExpr(n)
13041304
}
13051305

csharp/ql/test/library-tests/assignables/AssignableAccess.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
| Assignables.cs:133:15:133:15 | access to local variable x | Assignables.cs:132:13:132:13 | x | read |
105105
| Assignables.cs:133:15:133:15 | access to local variable x | Assignables.cs:132:13:132:13 | x | write |
106106
| Assignables.cs:133:29:133:29 | String s | Assignables.cs:133:29:133:29 | s | write |
107+
| Assignables.cs:138:19:138:19 | access to local variable x | Assignables.cs:138:19:138:19 | x | write |
107108
| Discards.cs:7:9:7:9 | access to parameter x | Discards.cs:5:30:5:30 | x | write |
108109
| Discards.cs:20:32:20:32 | Boolean z | Discards.cs:20:32:20:32 | z | write |
109110
| Discards.cs:25:27:25:30 | access to parameter args | Discards.cs:23:27:23:30 | args | read |

csharp/ql/test/library-tests/assignables/AssignableDefinition.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
| Assignables.cs:132:13:132:13 | x | Assignables.cs:132:13:132:17 | Int32 x = ... | Assignables.cs:132:13:132:13 | access to local variable x | Assignables.cs:132:17:132:17 | 0 | certain |
8888
| Assignables.cs:132:13:132:13 | x | Assignables.cs:133:15:133:15 | access to local variable x | Assignables.cs:133:15:133:15 | access to local variable x | Assignables.cs:133:15:133:15 | <none> | certain |
8989
| Assignables.cs:133:29:133:29 | s | Assignables.cs:133:29:133:29 | String s | Assignables.cs:133:29:133:29 | String s | Assignables.cs:133:29:133:29 | <none> | certain |
90+
| Assignables.cs:138:19:138:19 | x | Assignables.cs:138:19:138:50 | MemoryStream x = ... | Assignables.cs:138:19:138:19 | access to local variable x | Assignables.cs:138:23:138:50 | object creation of type MemoryStream | certain |
9091
| Discards.cs:5:30:5:30 | x | Discards.cs:5:30:5:30 | x | Discards.cs:5:30:5:30 | <none> | Discards.cs:5:30:5:30 | <none> | certain |
9192
| Discards.cs:5:30:5:30 | x | Discards.cs:7:9:7:17 | ... = ... | Discards.cs:7:9:7:9 | access to parameter x | Discards.cs:7:13:7:17 | false | certain |
9293
| Discards.cs:19:14:19:14 | x | Discards.cs:19:9:19:29 | ... = ... | Discards.cs:19:9:19:29 | <none> | Discards.cs:19:9:19:29 | <none> | certain |

csharp/ql/test/library-tests/assignables/Assignables.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,11 @@ void DelegateRef(Delegate d)
132132
var x = 0;
133133
d(ref x, out string s);
134134
}
135+
136+
void UsingDeclarations()
137+
{
138+
using var x = new System.IO.MemoryStream();
139+
}
135140
}
141+
142+
// semmle-extractor-options: /langversion:8.0

csharp/ql/test/library-tests/assignables/Assignables.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
| Assignables.cs:130:31:130:31 | d |
9696
| Assignables.cs:132:13:132:13 | x |
9797
| Assignables.cs:133:29:133:29 | s |
98+
| Assignables.cs:138:19:138:19 | x |
9899
| Discards.cs:5:6:5:8 | Item1 |
99100
| Discards.cs:5:11:5:16 | Item2 |
100101
| Discards.cs:5:30:5:30 | x |

csharp/ql/test/library-tests/assignables/GetAnAssignedValue.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| Assignables.cs:125:13:125:13 | s | Assignables.cs:125:17:125:25 | nameof(...) |
3838
| Assignables.cs:125:13:125:13 | s | Assignables.cs:126:13:126:30 | nameof(...) |
3939
| Assignables.cs:132:13:132:13 | x | Assignables.cs:132:17:132:17 | 0 |
40+
| Assignables.cs:138:19:138:19 | x | Assignables.cs:138:23:138:50 | object creation of type MemoryStream |
4041
| Discards.cs:5:30:5:30 | x | Discards.cs:7:13:7:17 | false |
4142
| Finally.cs:7:13:7:13 | i | Finally.cs:7:17:7:17 | 0 |
4243
| Finally.cs:7:13:7:13 | i | Finally.cs:15:17:15:17 | 1 |

csharp/ql/test/library-tests/csharp8/UsingDeclarations.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ localVars
44
| UsingDeclarations.cs:10:26:10:30 | file3 |
55
| UsingDeclarations.cs:10:72:10:76 | file4 |
66
localVariableDeclarations
7+
| UsingDeclarations.cs:8:9:8:116 | using ... ...; | 0 | UsingDeclarations.cs:8:26:8:69 | FileStream file1 = ... |
8+
| UsingDeclarations.cs:8:9:8:116 | using ... ...; | 1 | UsingDeclarations.cs:8:72:8:115 | FileStream file2 = ... |
79
usingStmts1
810
| UsingDeclarations.cs:8:9:8:116 | using ... ...; |
911
| UsingDeclarations.cs:10:9:12:9 | using (...) {...} |
1012
| UsingDeclarations.cs:14:9:15:13 | using (...) {...} |
1113
usingStmts
14+
| UsingDeclarations.cs:8:9:8:116 | using ... ...; | 0 | UsingDeclarations.cs:8:26:8:69 | FileStream file1 = ... |
15+
| UsingDeclarations.cs:8:9:8:116 | using ... ...; | 1 | UsingDeclarations.cs:8:72:8:115 | FileStream file2 = ... |
1216
| UsingDeclarations.cs:10:9:12:9 | using (...) {...} | 0 | UsingDeclarations.cs:10:26:10:69 | FileStream file3 = ... |
1317
| UsingDeclarations.cs:10:9:12:9 | using (...) {...} | 1 | UsingDeclarations.cs:10:72:10:115 | FileStream file4 = ... |
1418
usingDecls
1519
| UsingDeclarations.cs:8:9:8:116 | using ... ...; | 0 | UsingDeclarations.cs:8:26:8:69 | FileStream file1 = ... |
1620
| UsingDeclarations.cs:8:9:8:116 | using ... ...; | 1 | UsingDeclarations.cs:8:72:8:115 | FileStream file2 = ... |
21+
usingExprs
22+
| UsingDeclarations.cs:8:9:8:116 | using ... ...; | UsingDeclarations.cs:8:34:8:69 | object creation of type FileStream |
23+
| UsingDeclarations.cs:8:9:8:116 | using ... ...; | UsingDeclarations.cs:8:80:8:115 | object creation of type FileStream |
24+
| UsingDeclarations.cs:10:9:12:9 | using (...) {...} | UsingDeclarations.cs:10:34:10:69 | object creation of type FileStream |
25+
| UsingDeclarations.cs:10:9:12:9 | using (...) {...} | UsingDeclarations.cs:10:80:10:115 | object creation of type FileStream |
26+
| UsingDeclarations.cs:14:9:15:13 | using (...) {...} | UsingDeclarations.cs:14:15:14:50 | object creation of type FileStream |

csharp/ql/test/library-tests/csharp8/UsingDeclarations.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ query predicate usingStmts(UsingStmt stmt, int i, LocalVariableDeclExpr decl) {
1818
}
1919

2020
query predicate usingDecls(UsingDeclStmt stmt, int i, Expr e) { e = stmt.getChild(i) }
21+
22+
query predicate usingExprs(UsingStmt stmt, Expr e) { e = stmt.getAnExpr() }

csharp/ql/test/query-tests/API Abuse/DisposeNotCalledOnException/DisposeNotCalledOnException.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public void Method()
5858
SqlConnection c1f = new SqlConnection();
5959
Throw2(c1f);
6060
c1f.Dispose();
61+
62+
// GOOD: using declaration
63+
using SqlConnection c2 = new SqlConnection("");
64+
c2.Open();
6165
}
6266

6367
void Throw1(SqlConnection sc)
@@ -71,3 +75,5 @@ SqlConnection Throw2(SqlConnection sc)
7175
return sc == null ? throw new Exception() : sc;
7276
}
7377
}
78+
79+
// semmle-extractor-options: /langversion:8.0

csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public IDisposable Method()
7878
// GOOD: Passed to a library. This is only detected in CIL.
7979
Console.SetOut(new StreamWriter("output.txt"));
8080

81+
// GOOD: Disposed automatically.
82+
using var c2 = new Timer(TimerProc);
83+
8184
return null;
8285
}
8386

@@ -88,3 +91,5 @@ void TimerProc(object obj)
8891
{
8992
}
9093
}
94+
95+
// semmle-extractor-options: /langversion:8.0

0 commit comments

Comments
 (0)