Skip to content

Commit 5c127ef

Browse files
committed
C#: Fix false positives in cs/unchecked-return-value
1 parent b6f3f78 commit 5c127ef

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

change-notes/1.22/analysis-csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| Number of commits | No results | Query has been removed. |
2525
| Poorly documented files with many authors | No results | Query has been removed. |
2626
| Recent activity | No results | Query has been removed. |
27+
| 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. |
2728

2829
## Changes to code extraction
2930

csharp/ql/src/API Abuse/UncheckedReturnValue.ql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@ predicate whitelist(Method m) {
8989
}
9090

9191
class DiscardedMethodCall extends MethodCall {
92-
DiscardedMethodCall() { this.getParent() instanceof ExprStmt }
92+
DiscardedMethodCall() {
93+
this.getParent() instanceof ExprStmt
94+
or
95+
exists(Callable c |
96+
this = c.getExpressionBody() and
97+
not c.canReturn(this)
98+
)
99+
}
93100

94101
string query() {
95102
exists(Method m |

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ class Callable extends DotNet::Callable, Parameterizable, ExprOrStmtParent, @cal
209209
override predicate canReturn(DotNet::Expr e) {
210210
exists(ReturnStmt ret | ret.getEnclosingCallable() = this | e = ret.getExpr())
211211
or
212-
e = getExpressionBody()
212+
e = this.getExpressionBody() and
213+
not this.getReturnType() instanceof VoidType
213214
}
214215

215216
/** Holds if this callable can yield return the expression `e`. */

csharp/ql/test/query-tests/API Abuse/UncheckedReturnValue/UncheckedReturnValue.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
| UncheckedReturnValue.cs:109:9:109:17 | call to method M1 | Result of call to 'M1' is ignored, but 90% of calls to this method have their result used. |
55
| UncheckedReturnValue.cs:130:9:130:21 | call to method M2 | Result of call to 'M2' is ignored, but 90% of calls to this method have their result used. |
66
| UncheckedReturnValue.cs:142:9:142:20 | call to method M3 | Result of call to 'M3' is ignored, but 90% of calls to this method have their result used. |
7-
| UncheckedReturnValue.cs:169:9:169:12 | call to method M1 | Result of call to 'M1' is ignored, but 90% of calls to this method have their result used. |
87
| UncheckedReturnValueBad.cs:29:9:29:20 | call to method DoPrint | Result of call to 'DoPrint' is ignored, but 90% of calls to this method have their result used. |
98
| UncheckedReturnValueBad.cs:36:13:36:40 | call to method Read | Result of call to 'Read' is ignored, but should always be checked. |

0 commit comments

Comments
 (0)