Skip to content

Commit 422c2d5

Browse files
committed
C#: Add dynamic casts to useless upcast test
1 parent 02c4966 commit 422c2d5

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

csharp/ql/src/Language Abuse/UselessUpcast.ql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ class ExplicitUpcast extends ExplicitCast {
207207
}
208208
}
209209

210+
// Stop external filepaths from appearing in the results
211+
class ValueOrRefTypeLocation extends ValueOrRefType {
212+
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
213+
exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
214+
if exists(this.getFile().getRelativePath())
215+
then path = fullPath
216+
else path = fullPath.regexpReplaceAll(".*/", "<external>/")
217+
)
218+
}
219+
}
220+
210221
from ExplicitUpcast u, ValueOrRefType src, ValueOrRefType dest
211222
where
212223
src = u.getSourceType() and

csharp/ql/test/query-tests/Language Abuse/UselessUpcast/UselessUpcast.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public void M(A a) { }
1717

1818
class B : A
1919
{
20-
public static bool operator==(B b1, B b2) { return false; }
21-
public static bool operator!=(B b1, B b2) { return true; }
20+
public static bool operator ==(B b1, B b2) { return false; }
21+
public static bool operator !=(B b1, B b2) { return true; }
2222
public void M(B b) { }
2323
}
2424

@@ -68,11 +68,11 @@ void Test1(string[] args)
6868

6969
((I2)a).Foo(); // GOOD: Cast to an interface
7070

71-
o = a==(A)b; // GOOD: EQExpr
71+
o = a == (A)b; // GOOD: EQExpr
7272

73-
o = b==(B)b; // GOOD: Operator call
73+
o = b == (B)b; // GOOD: Operator call
7474

75-
var act = (Action) (() => { }); // GOOD
75+
var act = (Action)(() => { }); // GOOD
7676

7777
var objects = args.Select(arg => (object)arg); // GOOD
7878

@@ -126,9 +126,9 @@ public static void M2(this I3 i) =>
126126

127127
static class StaticMethods
128128
{
129-
public static void M1(A _) { }
130-
public static void M1(B _) { }
131-
public static void M2(B _) { }
129+
public static void M1(A _) { }
130+
public static void M1(B _) { }
131+
public static void M2(B _) { }
132132
}
133133

134134
class Constructors : I2
@@ -162,4 +162,12 @@ void M(SubSub ss)
162162
new Sub((Sub)ss); // BAD
163163
}
164164
}
165+
166+
class Dynamic
167+
{
168+
void M(object o)
169+
{
170+
var s0 = ((dynamic)o).ToString(); // GOOD
171+
}
172+
}
165173
}

csharp/ql/test/query-tests/Language Abuse/UselessUpcast/UselessUpcast.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
| UselessUpcast.cs:103:16:103:19 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:25:7:25:7 | C | C | UselessUpcast.cs:18:7:18:7 | B | B |
77
| UselessUpcast.cs:158:34:158:40 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:156:11:156:16 | SubSub | SubSub | UselessUpcast.cs:151:11:151:13 | Sub | Sub |
88
| UselessUpcast.cs:162:21:162:27 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:156:11:156:16 | SubSub | SubSub | UselessUpcast.cs:151:11:151:13 | Sub | Sub |
9+
| UselessUpcast.cs:170:23:170:32 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | file://<external>/System.Private.CoreLib.dll:0:0:0:0 | Object | Object | file://<external>/System.Private.CoreLib.dll:0:0:0:0 | dynamic | dynamic |
910
| UselessUpcastBad.cs:9:23:9:32 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcastBad.cs:4:11:4:13 | Sub | Sub | UselessUpcastBad.cs:3:11:3:15 | Super | Super |

0 commit comments

Comments
 (0)