Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b99e1a

Browse files
committed
C#: Teach cs/useless-upcast about disambiguating constructor calls
1 parent d848208 commit 6b99e1a

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ int getMaximumArguments(Callable c) {
6565
result = c.getNumberOfParameters()
6666
}
6767

68+
private class ConstructorCall extends Call {
69+
ConstructorCall() {
70+
this instanceof ObjectCreation or
71+
this instanceof ConstructorInitializer
72+
}
73+
}
74+
6875
/** An explicit upcast. */
6976
class ExplicitUpcast extends ExplicitCast {
7077
ValueOrRefType src;
@@ -140,6 +147,17 @@ class ExplicitUpcast extends ExplicitCast {
140147
)
141148
}
142149

150+
/** Holds if this upcast may be used to disambiguate the target of a constructor call. */
151+
pragma[nomagic]
152+
private predicate isDisambiguatingConstructorCall(Constructor other, int args) {
153+
exists(ConstructorCall cc, Constructor target, ValueOrRefType t | this.isArgument(cc, target) |
154+
t = target.getDeclaringType() and
155+
t.hasMember(other) and
156+
args = cc.getNumberOfArguments() and
157+
other != target
158+
)
159+
}
160+
143161
/** Holds if this upcast may be used to disambiguate the target of a call. */
144162
private predicate isDisambiguatingCall() {
145163
exists(Callable other, int args |
@@ -148,6 +166,8 @@ class ExplicitUpcast extends ExplicitCast {
148166
this.isDisambiguatingExtensionCall(other, args)
149167
or
150168
this.isDisambiguatingStaticCall(other, args)
169+
or
170+
this.isDisambiguatingConstructorCall(other, args)
151171
|
152172
args >= getMinimumArguments(other) and
153173
not args > getMaximumArguments(other)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
| UselessUpcast.cs:85:12:85:15 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:20:7:20:7 | B | B | UselessUpcast.cs:13:7:13:7 | A | A |
55
| UselessUpcast.cs:94:12:94:15 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:20:7:20:7 | B | B | UselessUpcast.cs:13:7:13:7 | A | A |
66
| UselessUpcast.cs:105:16:105:19 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:27:7:27:7 | C | C | UselessUpcast.cs:20:7:20:7 | B | B |
7-
| UselessUpcast.cs:141:32:141:36 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:153:11:153:13 | Sub | Sub | UselessUpcast.cs:9:11:9:12 | I2 | I2 |
8-
| UselessUpcast.cs:146:26:146:30 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:136:7:136:18 | Constructors | Constructors | UselessUpcast.cs:9:11:9:12 | I2 | I2 |
97
| UselessUpcast.cs:160:34:160:40 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:158:11:158:16 | SubSub | SubSub | UselessUpcast.cs:153:11:153:13 | Sub | Sub |
108
| UselessUpcast.cs:164:21:164:27 | (...) ... | There is no need to upcast from $@ to $@ - the conversion can be done implicitly. | UselessUpcast.cs:158:11:158:16 | SubSub | SubSub | UselessUpcast.cs:153:11:153:13 | Sub | Sub |
119
| 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)