SonarQube: Enterprise Edition v10.6 (92116)
Deploy: Unknown, company handles that
SonarScanner .Net: 9.0.0.100868
The basic problem is, the null dereference operator, in existence since like C# 8 (maybe earlier) will return null or a nullable value type (like bool? or int?) if the calling property or variable is null.
At the indicated line below, SonarQube always thinks that OwningClass could still be null. However, because of the Null dereference operator, the ?. syntax, on the line prior, in addition to the guard clause on “DesiredType” two lines earlier, there is absolutely no way for this.OwningClass to be null and also equal this.DesiredType to cause an NRE on the indicated line.
class MyClass
{
public MyClass(Type type) => this.Type == type ?? throw new ArgumentNullException(nameof(type);
public Type ElemType { get; }
internal bool DoOwningOkayCheck(object value) => value is DateTime;
}
public class OtherClass
{
public OtherClass() {}
public OtherClass(MyClass owner) => this.OwningClass = owner;
private MyClass OwningClass { get; set; }
protected Type DesiredType { get; private set; }
public void ProcessObject(object value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
this.DesiredType = value.GetType();
this.DoSomething(value);
}
protected virtual void DoSomething(object value)
{
if (this.DesiredType == null) throw new InvalidOperationException("shouldn't happen");
bool isOkay = this.OwningClass?.ElemType == this.DesiredType
? this.OwningClass.DoOwningOkayCheck(value) <-- S2259 here on OwningClass
: this.DoNonOwningOkayCheck(value);
}
private bool DoNonOwningOkayCheck(object value) => value is string;
}