I encountered this problem with a code that I simplified a first time to the following (kinda close to what I was actually doing):
voidfunk(char*str,boolignorestr){charbuf[32],*p=buf;if(ignorestr)buf[0]='\0';// style: Variable 'buf[0]' is assigned a value that is never used. [unreadVariable]elsep=str;puts(p);// but p can point to buf!}
Then I thought a bit more and simplified even more the problem like this: and with the following code I still get the unreadVariable style warning using the latest 2.11 release
voidf(void){intx[1],*p=x;x[0]=42;// style: Variable 'x[0]' is assigned a value that is never used. [unreadVariable]g(p);}
However when I use a simple int for x I get no error ie:
voidf2(void){intx,*p=&x;x=42;g(p);}
No warnings here...
So I thought here is a problem with implicit casting from int[N] to int* but using int *p=&x[0] does not fix the warning. Of course avoiding the warning is easy and could help clarifying the code (what I did) but it is still a false positive.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I also encountered a FP using reference. Not sure if it's the exact same pattern though:
#include<nlohmann/json.hpp>boolcheck();booldouble_check();nlohmann::jsontest(){nlohmann::jsondata;auto&elem=data["elem"];// style: Variable 'elem' is assigned a value that is never used. [unreadVariable]if(check()){elem=42;}elseif(double_check()){elem=1;}returndata;}
Notes:
* I could not easily reduce/reproduce this without using nlohmann::json, so I don't know if this is actually a FP or a missing "library" configuration for this type.
* If I change the else if into an else the warning disappears
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I encountered this problem with a code that I simplified a first time to the following (kinda close to what I was actually doing):
Then I thought a bit more and simplified even more the problem like this: and with the following code I still get the
unreadVariable
style warning using the latest 2.11 releaseHowever when I use a simple
int
for x I get no error ie:No warnings here...
So I thought here is a problem with implicit casting from
int[N]
toint*
but usingint *p=&x[0]
does not fix the warning. Of course avoiding the warning is easy and could help clarifying the code (what I did) but it is still a false positive.Example added here: https://trac.cppcheck.net/ticket/11139
I also encountered a FP using reference. Not sure if it's the exact same pattern though:
Notes:
* I could not easily reduce/reproduce this without using
nlohmann::json
, so I don't know if this is actually a FP or a missing "library" configuration for this type.* If I change the
else if
into anelse
the warning disappearsThanks for reporting, ticket is here; https://trac.cppcheck.net/ticket/11817