mirror of
https://github.com/github/codeql.git
synced 2026-04-30 19:26:02 +02:00
C#: Add dynamic casts to useless upcast test
This commit is contained in:
@@ -207,6 +207,17 @@ class ExplicitUpcast extends ExplicitCast {
|
||||
}
|
||||
}
|
||||
|
||||
// Stop external filepaths from appearing in the results
|
||||
class ValueOrRefTypeLocation extends ValueOrRefType {
|
||||
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
|
||||
exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
|
||||
if exists(this.getFile().getRelativePath())
|
||||
then path = fullPath
|
||||
else path = fullPath.regexpReplaceAll(".*/", "<external>/")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
from ExplicitUpcast u, ValueOrRefType src, ValueOrRefType dest
|
||||
where
|
||||
src = u.getSourceType() and
|
||||
|
||||
@@ -17,8 +17,8 @@ class A : I1, I2
|
||||
|
||||
class B : A
|
||||
{
|
||||
public static bool operator==(B b1, B b2) { return false; }
|
||||
public static bool operator!=(B b1, B b2) { return true; }
|
||||
public static bool operator ==(B b1, B b2) { return false; }
|
||||
public static bool operator !=(B b1, B b2) { return true; }
|
||||
public void M(B b) { }
|
||||
}
|
||||
|
||||
@@ -68,11 +68,11 @@ class Tests
|
||||
|
||||
((I2)a).Foo(); // GOOD: Cast to an interface
|
||||
|
||||
o = a==(A)b; // GOOD: EQExpr
|
||||
o = a == (A)b; // GOOD: EQExpr
|
||||
|
||||
o = b==(B)b; // GOOD: Operator call
|
||||
o = b == (B)b; // GOOD: Operator call
|
||||
|
||||
var act = (Action) (() => { }); // GOOD
|
||||
var act = (Action)(() => { }); // GOOD
|
||||
|
||||
var objects = args.Select(arg => (object)arg); // GOOD
|
||||
|
||||
@@ -126,9 +126,9 @@ static class IExtensions
|
||||
|
||||
static class StaticMethods
|
||||
{
|
||||
public static void M1(A _) { }
|
||||
public static void M1(B _) { }
|
||||
public static void M2(B _) { }
|
||||
public static void M1(A _) { }
|
||||
public static void M1(B _) { }
|
||||
public static void M2(B _) { }
|
||||
}
|
||||
|
||||
class Constructors : I2
|
||||
@@ -162,4 +162,12 @@ class Constructors : I2
|
||||
new Sub((Sub)ss); // BAD
|
||||
}
|
||||
}
|
||||
|
||||
class Dynamic
|
||||
{
|
||||
void M(object o)
|
||||
{
|
||||
var s0 = ((dynamic)o).ToString(); // GOOD
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,5 @@
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
|
||||
Reference in New Issue
Block a user