mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Merge pull request #1648 from hvitved/csharp/unchecked-return-lambda
C#: Fix false positives in `cs/unchecked-return-value`
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
| Number of commits | No results | Query has been removed. |
|
||||
| Poorly documented files with many authors | No results | Query has been removed. |
|
||||
| Recent activity | No results | Query has been removed. |
|
||||
| Unchecked return value (`cs/unchecked-return-value`) | Fewer false positive results | Method calls that are expression bodies of `void` callables (for example, the call to `Foo` in `void Bar() => Foo()`) are no longer considered to use the return value. |
|
||||
|
||||
## Changes to code extraction
|
||||
|
||||
|
||||
@@ -89,7 +89,14 @@ predicate whitelist(Method m) {
|
||||
}
|
||||
|
||||
class DiscardedMethodCall extends MethodCall {
|
||||
DiscardedMethodCall() { this.getParent() instanceof ExprStmt }
|
||||
DiscardedMethodCall() {
|
||||
this.getParent() instanceof ExprStmt
|
||||
or
|
||||
exists(Callable c |
|
||||
this = c.getExpressionBody() and
|
||||
not c.canReturn(this)
|
||||
)
|
||||
}
|
||||
|
||||
string query() {
|
||||
exists(Method m |
|
||||
|
||||
@@ -209,7 +209,8 @@ class Callable extends DotNet::Callable, Parameterizable, ExprOrStmtParent, @cal
|
||||
override predicate canReturn(DotNet::Expr e) {
|
||||
exists(ReturnStmt ret | ret.getEnclosingCallable() = this | e = ret.getExpr())
|
||||
or
|
||||
e = getExpressionBody()
|
||||
e = this.getExpressionBody() and
|
||||
not this.getReturnType() instanceof VoidType
|
||||
}
|
||||
|
||||
/** Holds if this callable can yield return the expression `e`. */
|
||||
|
||||
@@ -158,3 +158,23 @@ class C4
|
||||
class C6 : C5 { }
|
||||
class C7 : C6 { }
|
||||
}
|
||||
|
||||
class C5
|
||||
{
|
||||
int M1() => 0;
|
||||
|
||||
void M2()
|
||||
{
|
||||
// GOOD
|
||||
M1();
|
||||
Action a = () => M1();
|
||||
a = () => M1();
|
||||
a = () => M1();
|
||||
a = () => M1();
|
||||
a = () => M1();
|
||||
a = () => M1();
|
||||
a = () => M1();
|
||||
a = () => M1();
|
||||
a = () => M1();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user