Skip to content

Commit 410af42

Browse files
committed
C#: Adjust PR feedback on assertion methods
1 parent 6d8abd8 commit 410af42

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

csharp/ql/src/semmle/code/csharp/commons/Assertions.qll

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,23 @@ private import ControlFlow::BasicBlocks
99

1010
/** An assertion method. */
1111
abstract class AssertMethod extends Method {
12+
/**
13+
* DEPRECATED: renamed to `getAnAssertionIndex`.
14+
*
15+
* Gets the index of a parameter being asserted.
16+
*/
17+
deprecated int getAssertionIndex() { result = getAnAssertionIndex() }
18+
1219
/** Gets the index of a parameter being asserted. */
1320
abstract int getAnAssertionIndex();
1421

22+
/**
23+
* DEPRECATED: renamed to `getAnAssertedParameter`.
24+
*
25+
* Gets a parameter being asserted.
26+
*/
27+
deprecated Parameter getAssertedParameter() { result = getAnAssertedParameter() }
28+
1529
/** Gets a parameter being asserted. */
1630
final Parameter getAnAssertedParameter() {
1731
result = this.getParameter(this.getAnAssertionIndex())
@@ -42,6 +56,13 @@ class Assertion extends MethodCall {
4256
/** Gets the assertion method targeted by this assertion. */
4357
AssertMethod getAssertMethod() { result = target }
4458

59+
/**
60+
* DEPRECATED: renamed to `getAnExpr`.
61+
*
62+
* Gets an expression that this assertion pertains to.
63+
*/
64+
deprecated Expr getExpr() { result = this.getAnExpr() }
65+
4566
/** Gets an expression that this assertion pertains to. */
4667
Expr getAnExpr() { result = this.getArgumentForParameter(target.getAnAssertedParameter()) }
4768

@@ -199,7 +220,7 @@ class SystemDiagnosticsContractAssertTrueMethod extends AssertTrueMethod {
199220

200221
private predicate isDoesNotReturnIfAttributeParameter(Parameter p, boolean value) {
201222
exists(Attribute a | a = p.getAnAttribute() |
202-
a.getType() = any(SystemDiagnosticsCodeAnalysisDoesNotReturnIfAttributeClass c) and
223+
a.getType() instanceof SystemDiagnosticsCodeAnalysisDoesNotReturnIfAttributeClass and
203224
a.getConstructorArgument(0).(BoolLiteral).getBoolValue() = value
204225
)
205226
}
@@ -209,41 +230,31 @@ private predicate isDoesNotReturnIfAttributeParameter(Parameter p, boolean value
209230
* `System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)`.
210231
*/
211232
class SystemDiagnosticsCodeAnalysisDoesNotReturnIfAnnotatedAssertTrueMethod extends AssertTrueMethod {
233+
private int i;
234+
212235
SystemDiagnosticsCodeAnalysisDoesNotReturnIfAnnotatedAssertTrueMethod() {
213-
this = any(Method m | isDoesNotReturnIfAttributeParameter(m.getAParameter(), false))
236+
isDoesNotReturnIfAttributeParameter(this.getParameter(i), false)
214237
}
215238

216-
override int getAnAssertionIndex() {
217-
exists(Parameter p |
218-
this.getParameter(result) = p and isDoesNotReturnIfAttributeParameter(p, false)
219-
)
220-
}
239+
override int getAnAssertionIndex() { result = i }
221240

222-
override Class getExceptionClass() {
223-
// The user defines the thrown exception.
224-
any()
225-
}
241+
override SystemExceptionClass getExceptionClass() { any() }
226242
}
227243

228244
/**
229245
* A method with a parameter that is annotated with
230246
* `System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(true)`.
231247
*/
232248
class SystemDiagnosticsCodeAnalysisDoesNotReturnIfAnnotatedAssertFalseMethod extends AssertFalseMethod {
249+
private int i;
250+
233251
SystemDiagnosticsCodeAnalysisDoesNotReturnIfAnnotatedAssertFalseMethod() {
234-
this = any(Method m | isDoesNotReturnIfAttributeParameter(m.getAParameter(), true))
252+
isDoesNotReturnIfAttributeParameter(this.getParameter(i), true)
235253
}
236254

237-
override int getAnAssertionIndex() {
238-
exists(Parameter p |
239-
this.getParameter(result) = p and isDoesNotReturnIfAttributeParameter(p, true)
240-
)
241-
}
255+
override int getAnAssertionIndex() { result = i }
242256

243-
override Class getExceptionClass() {
244-
// The user defines the thrown exception.
245-
any()
246-
}
257+
override SystemExceptionClass getExceptionClass() { any() }
247258
}
248259

249260
/** A Visual Studio assertion method. */

0 commit comments

Comments
 (0)