S1854 Remove this useless assignment to a local variable, when used in range operator

A false positive is generated when the variable is only used in a range operator.

  • versions used :

    • Sonarlint 4.23.0.19399
    • Visual Studio 2019 Community Edition 16.6.4
    • c#
  • minimal code sample to reproduce:

          var prepTexts = "From here";
          var test = prepTexts.IndexOf(" ", StringComparison.Ordinal)+1; //hits S1854 Remove this useless assignment to local variable
          var res=  prepTexts[test..9]; //when the variable is used in a range operator
    

The following code does NOT generate the same warning:

        var prepTexts = "From here";
        var test = prepTexts.IndexOf(" ", StringComparison.Ordinal)+1; //no warning
        var res=  prepTexts[test]; //when the variable is used as an index

Hello @pvk,

Thank you for reporting this issue. I can confirm it’s a False Positive.

We already have issue on GitHub with similar case to track it.

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