-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissedUsingOpportunity.ql
More file actions
34 lines (30 loc) · 1.07 KB
/
MissedUsingOpportunity.ql
File metadata and controls
34 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @name Missed 'using' opportunity
* @description C# provides a 'using' statement as a better alternative to manual resource disposal in a finally block - it makes sense to use it.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cs/missed-using-statement
* @tags maintainability
* language-features
*/
import csharp
import semmle.code.csharp.frameworks.System
/** A call to IDisposable.Dispose or a method that overrides it. */
class DisposeCall extends MethodCall {
DisposeCall() { getTarget() instanceof DisposeMethod }
/** The object being disposed by the call (provided it can be easily determined). */
Variable getDisposee() {
exists(VariableAccess va |
va = getQualifier().stripCasts() and
result = va.getTarget()
)
}
}
from Variable v, DisposeCall c, TryStmt ts
where
v = c.getDisposee() and
c = ts.getFinally().getAChild*()
select v,
"This variable is manually $@ in a $@ - consider a C# using statement as a preferable resource management technique.",
c, "disposed", ts.getFinally(), "finally block"