Search code examples
c#sonarlint

False positive with rule S2538 in nested while-switch-switch construction


We are getting false positives while using rule S2538 in the following code

  EventLogLevel[] eventLevels = null;
  bool reachedEnd = false;
  while(!reachedEnd && jsonReader.Read())
  {
    switch(jsonReader.TokenType)
    {
      case JsonToken.PropertyName:
        string propertyName = jsonReader.Value.ToString();

        switch(propertyName)
        {
          case nameof(EventLevels):
            eventLevels = EventSettingsJson.ParseEventLogLevelsArray(nameof(EventLevels), jsonReader);
            break;
          default:
            throw new JsonParserException($"Invalid property: {propertyName}");
        }

        break;
      case JsonToken.EndObject:
        reachedEnd = true;
        break;
      default:
        throw new JsonParserException($"Unexpected Token Type while parsing json properties. TokenType: {jsonReader.TokenType}");
    }
  }

  if(eventLevels != null)
  {
    return new EventLogCollectionSettings(eventLogName, eventLevels);
  }

The last if (eventLevels != null) shows the warning with the message:

[Change this condition so that it does not always evaluate to "false"].

I couldn't create a testcase to reproduce it.


Solution

  • We know about this limitation in our data flow analysis engine. It's related to this ticket: https://jira.sonarsource.com/browse/SLVS-1091. We have no fix for it yet.