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