dart-lint icon indicating copy to clipboard operation
dart-lint copied to clipboard

The lint invariant_booleans is confused inside a switch statement.

Open bsutton opened this issue 4 years ago • 1 comments

When using lint 1.5.1

In the following switch statement there are two cases that both check if char = ' '.

The lint incorrectly marks the second test as invariant_booleans as it sees the if char = ' ' in the first case and assume that the test protects the second case from having a space.

If you change the first char = ' ' in the first case to anything else the warning goes away.

The lint should not fire in this scenario.


 enum _ParseState { starting, inQuote, inWord }

  /// parses the given command breaking them done into words
void _parse2(String commandLine) {
    var state = _ParseState.starting;

    for (var i = 0; i < commandLine.length; i++) {
      final char = commandLine[i];

      switch (state) {
        case _ParseState.starting:
          if (char == ' ') {
            break;
          }
          break;

        case _ParseState.inWord:
          if (char == ' ')
          {
            break;
          }
      }
    }
  }

bsutton avatar Dec 21 '20 22:12 bsutton

I don't think this is an issue with lint itself; it should probably be reported in https://github.com/dart-lang/linter or https://github.com/dart-lang/sdk (not sure which one), if somebody hasn't already done so.

triallax avatar Dec 11 '21 10:12 triallax