Skip to content

C#: Add tests for Equals methods with nullable parameter types#21342

Open
hvitved wants to merge 1 commit intogithub:mainfrom
hvitved:csharp/equals-nullable-tests
Open

C#: Add tests for Equals methods with nullable parameter types#21342
hvitved wants to merge 1 commit intogithub:mainfrom
hvitved:csharp/equals-nullable-tests

Conversation

@hvitved
Copy link
Contributor

@hvitved hvitved commented Feb 18, 2026

Adding tests to show that the two queries correctly handle Equals methods with nullable parameter types.

@github-actions github-actions bot added the C# label Feb 18, 2026
@hvitved hvitved marked this pull request as ready for review February 18, 2026 07:58
@hvitved hvitved requested a review from a team as a code owner February 18, 2026 07:58
Copilot AI review requested due to automatic review settings February 18, 2026 07:58
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Feb 18, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) and Equals(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;
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
return param1 != null && field1 == param1.field1;
return field1 == param1.field1;

Copilot uses AI. Check for mistakes.
{
private int field1;

public bool Equals(TestClass4 param1)
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
public bool Equals(TestClass4 param1)
public bool Equals(TestClass4? param1)

Copilot uses AI. Check for mistakes.
{
private int field1;

public bool Equals(TestClass2 param1)
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
public bool Equals(TestClass2 param1)
public bool Equals(TestClass2? param1)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C# no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments