Skip to content

Commit 00fdc70

Browse files
authored
Merge pull request #2710 from calumgrant/cs/short-circuit-out
C#: Remove false positive in cs/non-short-circuit
2 parents 0276c97 + 6b377d7 commit 00fdc70

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

change-notes/1.24/analysis-csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The following changes in version 1.24 affect C# analysis in all applications.
1818
| **Query** | **Expected impact** | **Change** |
1919
|------------------------------|------------------------|-----------------------------------|
2020
| Useless assignment to local variable (`cs/useless-assignment-to-local`) | Fewer false positive results | Results have been removed when the variable is named `_` in a `foreach` statement. |
21+
| Potentially dangerous use of non-short-circuit logic (`cs/non-short-circuit`) | Fewer false positive results | Results have been removed when the expression contains an `out` parameter. |
2122
| Dereferenced variable may be null (`cs/dereferenced-value-may-be-null`) | More results | Results are reported from parameters with a default value of `null`. |
2223

2324
## Removal of old queries

csharp/ql/src/Likely Bugs/DangerousNonShortCircuitLogic.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class DangerousExpression extends Expr {
2727
e instanceof MethodCall
2828
or
2929
e instanceof ArrayAccess
30-
)
30+
) and
31+
not exists(Expr e | this = e.getParent*() | e.(Call).getTarget().getAParameter().isOutOrRef())
3132
}
3233
}
3334

csharp/ql/test/query-tests/Likely Bugs/DangerousNonShortCircuitLogic/DangerousNonShortCircuitLogic.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ void M()
2020
var b = true;
2121
b &= c.Method(); // GOOD
2222
b |= c[0]; // GOOD
23+
24+
if (c == null | c.Method(out _)) ; // GOOD
25+
if (c == null | (c.Method() | c.Method(out _))) ; // GOOD
2326
}
2427

2528
class C
@@ -28,6 +31,7 @@ class C
2831
public string Property { get; set; }
2932
public bool this[int i] { get { return false; } set { } }
3033
public bool Method() { return false; }
34+
public bool Method(out int x) { x = 0; return false; }
3135
}
3236
}
3337

0 commit comments

Comments
 (0)