False positive for S2589

Please provide

  • Operating system: Windows 11
  • Visual Studio version: 2022 (17.9.6)
  • SonarLint plugin version: 7.3.0.77872
  • Programming language you’re coding in: C#
  • Is connected mode used: No
    • Connected to SonarCloud or SonarQube (and which version):

And a thorough description of the problem / question:

The following code incorrectly triggers S2589:

ParameteredCtorConstructionInfo? parameteredCtor = null;
string? parameteredCtorError = null;
if (parameterlessCtor == null)
    (parameteredCtor, parameteredCtorError) = GetParameteredCtor(type);

if (parameterlessCtor == null && parameteredCtor == null)
    throw new XmlModelDefinitionException($"...");

SonarLint reports that parameteredCtor == null is always true, but it actually depends on results of GetParameteredCtor(type).

Hey there

Can you upgrade to v7.8 and let us know if the issue persists?

I just did. Looks OK now, sorry that my false positive was a false positive :slight_smile:

1 Like

Thanks all the same :slight_smile: Don’t hesitate to come back and report another if you find one!

I stand corrected. A different part of code:

                 // Check if property doesn't match ctor parameter

                ConstructorParameterInfo? matchingCtorParam = null;
                int? matchingCtorParamIndex = null;

                if (parameteredCtor != null)
                {
                    int i = parameteredCtor.ConstructorParameters.Count - 1;

                    while (i >= 0 && parameteredCtor.ConstructorParameters[i].MatchingProperty != property)
                        i--;

                    if (i >= 0)
                    {
                        matchingCtorParam = parameteredCtor.ConstructorParameters[i];
                        matchingCtorParamIndex = i;
                    }
                }

                if (matchingCtorParam != null && matchingCtorParamIndex != null)
                {
                    // ...
                }

Now the lint claims that matchingCtorParamIndex != null is always true.

Hello @WojciechSura, this is a true positive. matchingCtorParam and matchingCtorParamindex are only ever set in tandem. If one of them is not null, so is the other.
Let me know if I’m missing something.