C#: Add tests for Equals methods with nullable parameter types#21342
C#: Add tests for Equals methods with nullable parameter types#21342hvitved wants to merge 1 commit intogithub:mainfrom
Equals methods with nullable parameter types#21342Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds test coverage for nullable parameter types in Equals methods to verify that the API abuse queries correctly handle nullable reference types. The tests validate that queries properly distinguish between nullable and non-nullable parameter types in both IEquatable<T>.Equals and object.Equals overrides.
Changes:
- Added test cases covering all combinations of nullable/non-nullable parameters for both
Equals(T)andEquals(object)methods - Implemented four test classes to validate different parameter type scenarios with nullable reference types enabled
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| csharp/ql/test/query-tests/API Abuse/IncorrectEqualsSignature/NullableTest.cs | Test cases for IncorrectEqualsSignature query with nullable parameter variations |
| csharp/ql/test/query-tests/API Abuse/ClassDoesNotImplementEquals/NullableTest.cs | Test cases for ClassDoesNotImplementEquals query with nullable parameter variations |
|
|
||
| public bool Equals(TestClass2 param1) | ||
| { | ||
| return param1 != null && field1 == param1.field1; |
There was a problem hiding this comment.
The null check param1 != null is ineffective because param1 is declared as non-nullable. With nullable reference types enabled, this parameter cannot be null under normal circumstances, making the null check unnecessary and potentially misleading.
| return param1 != null && field1 == param1.field1; | |
| return field1 == param1.field1; |
| { | ||
| private int field1; | ||
|
|
||
| public bool Equals(TestClass4 param1) |
There was a problem hiding this comment.
The null check param1 != null is ineffective because param1 is declared as non-nullable. With nullable reference types enabled, this parameter cannot be null under normal circumstances, making the null check unnecessary and potentially misleading.
| public bool Equals(TestClass4 param1) | |
| public bool Equals(TestClass4? param1) |
| { | ||
| private int field1; | ||
|
|
||
| public bool Equals(TestClass2 param1) |
There was a problem hiding this comment.
The null check param1 != null is ineffective because param1 is declared as non-nullable. With nullable reference types enabled, this parameter cannot be null under normal circumstances, making the null check unnecessary and potentially misleading.
| public bool Equals(TestClass2 param1) | |
| public bool Equals(TestClass2? param1) |
Adding tests to show that the two queries correctly handle
Equalsmethods with nullable parameter types.