Menu

Scope of the variable can be reduced

2021-11-26
2023-07-12
  • Simone Gasparella

    Hi forum ,

    I am using cppcheck 2.6 i an embedded c project .
    The software is showing the style issue : "the scope of the variable can be reduced"...
    It is showing this message in some variables before a switch ..
    If i try to move some variables inside the cases for solving the style issue I get an error from the compiler..
    How Can I fix it ?

    Thanks for the suggestion

     

    Last edit: Simone Gasparella 2021-11-26
  • CHR

    CHR - 2021-11-26

    Hard to say from the snippets you posted, code as text would be better. Maybe you need to open a new scope for a casestatement?

    ...
    case FOO: {
        uint8_t modbus = ...
    }
    ...
    
     
  • Simone Gasparella

    Hi CHR..
    Thanks for your answer
    if i try to do it I get :
    a label can only be part of a statement and a declaration is not a statement

     
  • Daniel Marjamäki

    In an embedded c code it is usual to have all variables at the start of a function. Feel free to keep that programming style. Then you can just suppress these warnings --suppress=variableScope.

    a label can only be part of a statement and a declaration is not a statement

    Sounds strange!

    What happens if you move the variable declaration into the if.

    if( uart_Crc16_In(COM_INV) == 0)
    {
        uint16_t modbus_register_data_read = 0;
    
     
  • Jean-Loic MARTIN

    Hi Simone,

    A workaround is too put a ';' after the ':'

    case FOO:; 
        uint8_t modbus = ...
    

    No need to put braces.

     
    • Simone Gasparella

      Hi ..

      I am using cpp check..

      Can you tell me the place where I can ask help for finding this issue ?

      static uint64_t CalK(machine_status *machine, uint16_t chn, uint32_t adc)
      {
      uint16_t i, n;
      int32_t k1, k2, k;

      n = K_SENSE_POINTS_NUM;
      for (i=0; i!=n; i++) {
      if (adc <= machine->uc1_output[chn].ksense.adc[i]) {
      break;
      }
      }
      if (i == 0) {
      return machine->uc1_output[chn].ksense.k[0];
      }
      if (i == n) {
      return machine->uc1_output[chn].ksense.k[n-1];
      }
      k1 = machine->uc1_output[chn].ksense.k[i-1];
      k2 = machine->uc1_output[chn].ksense.k[i];

      k = k1 + ((k2-k1)*((int32_t)(adc-machine->uc1_output[chn].ksense.adc[i-1])))/((int32_t)(machine->uc1_output[chn].ksense.adc[i] - machine->uc1_output[chn].ksense.adc[i-1]));

      return k;
      }

      I don't understand if cpp check is able to detect the possible divion by 0 if k2 = k1 for example .. or if machine->uc1_output[chn].ksense.adc[i] = machine->uc1_output[chn].ksense.adc[i-1]));

      Is cpp check able to detect this possible issue ?

      Thanks a lot for your support

      --
      Simone Gasparella | Software Engineering Manager

      [cid:logo_systel_elettronica_493634fe-166d-4138-a2f2-18892caaa0d1.png]https://www.systel.it/

      Systel Elettronica S.r.l.
      Via Castellaro 12, 36040 Salcedo (VI), Italy
      Phone: +39 0445 888264<tel:+39%200445%20888264> | Direct: +39 0445 1854214<tel:+39%200445%201854214>
      simone.gasparella@systel.itsimone.gasparella@systel.it | www.systel.ithttps://www.systel.it/</tel:+39%200445%201854214></tel:+39%200445%20888264>

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.