S3900 and S3877 fight against each other at operators

When implementing (implicit) operators dereferencing the parameter we have two rules fighting against each other:

  1. S3900 requires to check the parameter to be dereferenced whether null or not. The operator must not be used while member is null. Therefore I want to throw ArgumentNullException.
  2. S3877 does not allow throwing exceptions in operators.

What is the correct solution?

S3877 will raise in the following

  • operator ==, !=, <, >, <=, >=
  • implicit cast operators

Indeed, you should not throw from these operators.

And S3900 does not ask to throw an exception, but to do a null-check before dereferencing (defensive coding).

I don’t see the problem here… why do you have to throw an exception inside an ==, !=, <, >, <=, >= operator?

And S3900 does not ask to throw an exception, but to do a null-check before dereferencing (defensive coding).

But, what to return in the operator if null or default is not allowed?
This is why I think an exception is the better solution?

the use-case seems quite specific to your application, so I cannot give a general opinion.

if you decide to take a path by throwing, then you can always mark SonarQube issues as won’t fix, and add a comment to specify the reasoning behind the decision