Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio not warning about non exhaustive switch statments

This might be a bug; however I am unsure. In java switch statements thats are non-exhaustive, should produce a warning of sorts in the IDE. In this case, Android Studio 3.0 (B6)

Given the enum class Foo

public enum  Foo {

  FOO_ONE,
  FOO_TWO
}

and then later used in a switch statement as this:

switch(fooEnum){
      case FOO_ONE:
     //Do something here 
        break;
    }

Shouldn't the compiler warn me in some way, that this switch statement doesn't use all the Foo enum options?

like image 694
Robert J. Clegg Avatar asked Dec 02 '25 07:12

Robert J. Clegg


2 Answers

The compiler should not give a warning, this is an action for a linter. You can enable this exact feature in Android Studio. If you click the button in the very bottom right of the Android Studio window that looks like a little dude's head, there is a "Configure Inspections" button.

Then under "Java-> Control Flow Issues -> Enum 'switch' statement that misses case" enable that option.

like image 56
amacf Avatar answered Dec 04 '25 19:12

amacf


No they shouldn't. Non-exhaustive switch statements aren't illegal. Producing a warning would be a compiler specific option at most (and truthfully I think a really really annoying one). There's nothing in the language which requires a compiler to warn you of anything. Although there likely is a lint setting that would turn that on, it seems the type of thing lint would do.

like image 22
Gabe Sechan Avatar answered Dec 04 '25 19:12

Gabe Sechan