Ticket Created
over 3 years ago

CIQQA-733

multiply case and variable v may not have been initialized error

this code 1A

...

case a1:

//here there is nothing I want to have one code for a1 & a2

//case a2:

...

break;

and  this 1B

..

//case a1:

//here there is nothing I want to have one code for a1 & a2

case a2:

...

break;

no compilation errors

but this code 2

..

case a1:

//here there is nothing I want to have one code for a1 & a2

case a2:

...

break;

generates compilation error "variable v may not have been initialized" but if first is good (all variables initialised) second has to be good too because code 1 there is in 2

sdk 4.0.6
eclipse CIQ plug in: 4.1.0.beta1
eclipse ver: 2021-09 (4.21.0) Build id: 20210910-1417
windows 10
java jre1.8.0_311
watch face
minSdkVersion 2.4.0

Parents Comment Children
  • not working but error

    BUILD: ERROR: fenix6spro: ... :912: Variable v may not have been initialized.
    BUILD: ERROR: fenix6spro: ... .mc:965: Variable icon may not have been initialized.
    BUILD: Complete
    Aborting launch due to failed build.

    Strange thing that happens when you try to launch, after saving and compilation is no error and  BUILD: Complete.
    Is switch/case analysed in steps? You can write:

    case 1:
      br;//not break

    and error only when launching...

    and you can write also :

    case 1:
      ...
      break;
    case 1:
      ...
      break;

    as Brandon.ConnectIQ says, technically OKbut it should be a small warning (yes, I know it can be case variable: and variable is known in runtime but it's developer's risk when).

  • Yep I can recreate this. For completeness here's a full test case:

            var v, i;
            i = 1;
            switch (i)
            {
                case 1:
                case 2:
                    v = 123;
                    break;
                default:
                    v = 321;
                //break unnecessary
            }
    
            System.println(v); // warning here: Variable v may not have been initialized in all code paths.
    

    This is clearly a compiler bug.

  • of course no, but it very useful feature(if working well) that compiler gives information that I set value in all CASEs and now I can't compile  and spent some time to look for case without setting value

    I think you don't understand problem, I didn't tested it but probably shows the same like my more complicated:

    var v, i;

    i=1;
    switch(i)
    {
      case 1:
      case 2:
        v = 123;
        break;
      default:
         v = 321;
      //break unnecessary
    }

    dc.drawText....

    this code probably gives error, comment case 1 or case 2 no error

  • does it still happen if you change

    var v;

    to

    var v=null;

  • It doesn't matter because as you see i 1a and 1b there is no error so code is good and error appearer when to CASE are together.

    But v is outside switch:

    var v;

    switch...

    {

    // each case includes v =...

    case...

    }

    v = ...