False-Positive 1854 while increment in same statement with a calculation

Versions:

  • SonarQube: Community Edition - Version 9.9.3
  • Scanner: 6.2.0.85879

Following code I get reported as issue csharpsquid:S1854:

int_Position = str_Option.IndexOfAny(new[]
                                        {
                                          ':',
                                          '='
                                        });

if(int_Position == -1)
{
  obj_Options.Add(str_Option.Substring(1),
                  String.Empty);
}
else
{
  obj_Options.Add(str_Option.Substring(1, int_Position - 1),
                  str_Option.Substring(++int_Position));
}

The code obj_Options.Add(str_Option.Substring(1, int_Position - 1), str_Option.Substring(++int_Position)); takes the left length of the string by calculation and then right part after increment the position variable. May be another calculation is awaited here?

Hello @HBoskugelS,

Welcome to the community!

This behavior has already been discussed.
Your ++int_Position usage can be translated to int_Position = int_Position + 1:

else
{
  obj_Options.Add(str_Option.Substring(1, int_Position - 1),
                  str_Option.Substring(int_Position = int_Position + 1));
}

Which leads to an unused assignment.
Then, I would suggest replacing ++int_Position with int_Position + 1.

You can find here the previous discussion about this:

Have a nice day!