cppcheck icon indicating copy to clipboard operation
cppcheck copied to clipboard

Fix false positive struct member is never used

Open KenPatrickLehrmann opened this issue 4 years ago • 6 comments

KenPatrickLehrmann avatar Nov 29 '21 15:11 KenPatrickLehrmann

Oh ok. I'll try to do that then ;-)

KenPatrickLehrmann avatar Nov 30 '21 08:11 KenPatrickLehrmann

I ended up with https://github.com/danmar/cppcheck/commit/6ce4b2bd6caf52210e81f70b1d4cd233ee11e8e7, but I feel like it would be easier to remove the parentheses in the simplification. Maybe simplifyRedundantConsecutiveBraces is enough, or maybe doing it in the handling of "->" in Tokenizer::combineOperators. I'm afraid that not simplifying it would make checks more difficult.

Edit: besides, the code to handle parentheses is fragile (the commit above broke (foo[1]).x = 123), and there literally can be parentheses everywhere... It really feels better to simply remove the redundant parentheses.

Edit bis: that being said, it looks like there are some other issues in createSymbolDatabaseSetVariablePointers, I'll see if I can find the time to debug it. Maybe rewriting it to use the ast instead of parsing the code directly will do the trick.

Typically, with master and the following, the member a is reported as unused:

struct A {
  int a;
};

#define dAa(ptr) (((ptr))->a)

void f(A p[]) {
   int i=0;
  if (32 == dAa(&p[i])) {
  }
  if (dAa(&p[i])) {
  }
  if ((&p[i])->a) {
  }
}

KenPatrickLehrmann avatar Nov 30 '21 10:11 KenPatrickLehrmann

Maybe rewriting it to use the ast instead of parsing the code directly will do the trick.

imho that sounds like a better approach but I understand it's not a quick fix.

sooner or later the simplification can bite us. for instance if we would like to write a check for misplaced/redundant parentheses.

danmar avatar Dec 02 '21 07:12 danmar

Indeed. This looks better, but I have at least one FP on the codebase where I work.

KenPatrickLehrmann avatar Dec 02 '21 12:12 KenPatrickLehrmann

We also have astParentSkipParens to skip parenthesis when traversing the ast.

pfultz2 avatar Dec 02 '21 15:12 pfultz2

On the topic of parentheses, not sure if this is related: The token list currently does not contain the double parentheses in foo((x = 1, x = x + 1)); from https://trac.cppcheck.net/ticket/8215

chrchr-github avatar Dec 02 '21 16:12 chrchr-github