Menu

Union structure access produce "never used" style warning

michele
2020-09-28
2020-09-29
  • michele

    michele - 2020-09-28

    Hello
    We noticed that when union type structures are used cppchecker often fires some "never used" warning probably not detecting the accessing of the structure.
    I did this simple example to show the problem: the cppchecker result is after and we can see that bit assignment is considered as "never used".

    unReadvar.c file

    typedef unsigned short U16;

    typedef union
    {
    U16 u16All;
    struct
    {
    U16 b00 :1;
    U16 b01 :1;
    U16 b02 :1;
    U16 b03 :1;
    U16 b04 :1;
    U16 b05 :1;
    U16 b06 :1;
    U16 b07 :1;
    U16 b08 :1;
    U16 b09 :1;
    U16 b10 :1;
    U16 b11 :1;
    U16 b12 :1;
    U16 b13 :1;
    U16 b14 :1;
    U16 b15 :1;
    } u16Bits;
    }tU16union;

    extern U16 zResult;
    void test(void)
    {
    tU16union zTest;

               zTest.u16All = 0u;
    
               zTest.u16Bits.b15 = 1u;
    
               zResult = zTest.u16All;
    

    }

    Cppchecker output

    D:\Dev\cppchecker_problems>"c:\Program Files\Cppcheck\cppcheck.exe" --enable=all unReadvar.c
    Checking unReadvar.c ...
    [unReadvar.c:33]: (style) Variable 'zTest.u16Bits.b00' is assigned a value that is never used.
    [unReadvar.c:34]: (style) Variable 'zTest.u16Bits.b01' is assigned a value that is never used.
    [unReadvar.c:35]: (style) Variable 'zTest.u16Bits.b02' is assigned a value that is never used.
    [unReadvar.c:36]: (style) Variable 'zTest.u16Bits.b03' is assigned a value that is never used.
    [unReadvar.c:37]: (style) Variable 'zTest.u16Bits.b04' is assigned a value that is never used.
    [unReadvar.c:38]: (style) Variable 'zTest.u16Bits.b05' is assigned a value that is never used.
    [unReadvar.c:39]: (style) Variable 'zTest.u16Bits.b06' is assigned a value that is never used.
    [unReadvar.c:40]: (style) Variable 'zTest.u16Bits.b07' is assigned a value that is never used.
    [unReadvar.c:41]: (style) Variable 'zTest.u16Bits.b08' is assigned a value that is never used.
    [unReadvar.c:42]: (style) Variable 'zTest.u16Bits.b09' is assigned a value that is never used.
    [unReadvar.c:43]: (style) Variable 'zTest.u16Bits.b10' is assigned a value that is never used.
    [unReadvar.c:44]: (style) Variable 'zTest.u16Bits.b11' is assigned a value that is never used.
    [unReadvar.c:45]: (style) Variable 'zTest.u16Bits.b12' is assigned a value that is never used.
    [unReadvar.c:46]: (style) Variable 'zTest.u16Bits.b13' is assigned a value that is never used.
    [unReadvar.c:47]: (style) Variable 'zTest.u16Bits.b14' is assigned a value that is never used.
    [unReadvar.c:48]: (style) Variable 'zTest.u16Bits.b15' is assigned a value that is never used.
    [unReadvar.c:29]: (style) The function 'test' is never used.

    Best regards
    Michele
    Informativa sul trattamenti dei dati dei contatti di clienti e fornitori La Societ?, Titolare del trattamento La informa che, nell'esecuzione del rapporto contrattuale o precontrattuale che ci lega alla impresa per cui lei opera, potranno venir comunicati reciprocamente tra le due imprese, o comunque messi a disposizione dei rispettivi referenti, i Suoi dati personali e di contatto (dati anagrafici, e-mail aziendali, telefoni aziendali, smartphone ad uso lavorativo, etc.) in funzione delle mansioni e degli incarichi a Lei conferiti, per la gestione ed esecuzione dei rapporti precontrattuali o contrattuali in questione. Pertanto, la nostra azienda tratter? tali dati personali nei limiti in cui siano strettamente necessari per la esecuzione di tutti gli aspetti del contratto. I dati verranno conservati fino al completamento delle reciproche prestazioni oggetto del contratto e successivamente in ragione dei termini prescrizionali previsti per gli atti e documenti dell'impresa.

    Information on data processing of customer and supplier contacts The Company, owner of the personal data, informs you that, in the execution of the contractual or pre-contractual relationship that binds us to the company you are currently working for, might exchange respective contacts, your personal data and contact details (personal data, company e-mails, company telephones, smartphones for business use, etc.) according to the tasks and tasks assigned to you, for the management and execution of the pre-contractual or contractual relationships in question. Therefore, our company will process such personal data to the extent that it is strictly necessary for the execution of all aspects of the contract. The data will be retained until the completion of the reciprocal services covered by the contract and subsequently according to the prescribed time limits for the company's documents and documents.

     
  • Daniel Marjamäki

    Thanks! I can reproduce. I created https://trac.cppcheck.net/ticket/9923

     

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.