False positives for S1854 in C# 8, net standard 2.1

With the following code:

string value = “1.2_3”;

        int fieldStartIndex = value.IndexOf('.') + 1;
        int fieldEndIndex = value.IndexOf('_');

        if (fieldEndIndex == -1)
            fieldEndIndex = value.Length;

        int.TryParse(value[fieldStartIndex..fieldEndIndex], NumberStyles.Any, CultureInfo.InvariantCulture, out field);

‘field’ is used later in the function.

I’m getting “Warning S1854 Remove this useless assignment to local variable ‘fieldStartIndex’.” with both the Sonarlint VS plugin and Sonarqube

It happens at other uses of the ‘…’ range operator.

Hello Brian,

Thank you for reporting this case. I can confirm it as False Positive related to range operator. Same would happen for indices as well.

We have similar scenario reported in this issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.