2

If you switch on an enum, and neither cover all cases nor provide a default, it is useful to get a compiler warning. Other answers on this site have suggested that javac should provide such a warning.

I'm using Maven, and have added the following to pom.xml in an attempt to enable all warnings:

    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
        <compilerArgs>
          <arg>-Xlint:all</arg>
        </compilerArgs>
        <showDeprecation>true</showDeprecation>
        <showWarnings>true</showWarnings>
      </configuration>
    </plugin>

But the compiler is still silent about nonexhaustive switch statements in my code. Is there some other flag I can set, to enable such warnings?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 1
    Why not have a SonarLint/Qube integration, in maven. – Joop Eggen Oct 11 '19 at 10:26
  • It's worth mentioning the Java and Maven versions you're using and sample code of course if good to be a part of the question to reproduce the error. – Naman Oct 16 '19 at 19:31

1 Answers1

3

I know about such warnings because of:

The latter should be of interest:

An annotation processor will make sure that you get a compile-time error.
The project also includes a partial enum-mapper class which you may want to use instead of a switch statement.

The project is available in Maven Central, so you could add it to your dependencies.

The point remains:

A javac non-standard option like -Xlint might not be enough, alone, to catch that partial switch enum usecase.
It would only detects Fall-through cases: cases in a switch block, other than the last case in the block, whose code does not include a break statement, allowing code execution to "fall through" from that case to the next case.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250