Liyilin
Liyilin

Reputation: 1

Code Analysis does not work in VS 2010

I created a console app using VS2010 like below

int test(int i)
{   
if ( i == 0 )
    return 0;
else
    return 1;
int j = 2;
j++;

return j;
}

void deadCode(char * pa)
{
 printf("%s", pa);
}

int _tmain(int argc, _TCHAR* argv[]) 
{
    test(2);
    return 0;
}

I checked "Enable Code Analysis for C/C++ on Build" and select Rule Set "Microsoft All Rules", when I built the project, I got

1>RunCodeAnalysis:

1> Running Code Analysis...

1> Code Analysis Complete -- 0 error(s), 0 warning(s)

I expected "CA1804" warning for bold part in test(), another warning should be displayed for dead code deadCode().

My question is Why Code Analysis did not catch the defects? I also tried to created my rule set that only enabled CA1804 warning, but the result is same as above (Microsoft All Rules).

Any Ideas?

Upvotes: 0

Views: 159

Answers (1)

us2012
us2012

Reputation: 16253

  • Unused functions are not "dead code". Imagine you are writing a library - that may well expose functions to its users that it doesn't use itself.
  • As for the CA1804, I can only make assumptions: Presumably the analysis doesn't go deep enough to create a full control flow graph and notice that the code after the if/else block can't be reached. But even if it did a full analysis, I would expect a "dead code" warning for the part of test() after the if block, not an "unused local" warning - if the code were reachable, j would be used.

Upvotes: 1

Related Questions