Merge pull request #1648 from hvitved/csharp/unchecked-return-lambda

C#: Fix false positives in `cs/unchecked-return-value`
This commit is contained in:
Luke Cartey
2019-08-02 21:48:38 -07:00
committed by GitHub
4 changed files with 31 additions and 2 deletions

View File

@@ -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

View File

@@ -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 |

View File

@@ -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`. */

View File

@@ -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();
}
}