Open Bug 1791078 Opened 3 years ago Updated 3 years ago

Add static analysis to forbid `Foo* a, b, c` (i.e. declaring pointers as a list), since the pointer asterisk doesn't apply to later list items

Categories

(Developer Infrastructure :: Source Code Analysis, enhancement)

Desktop
All
enhancement

Tracking

(Not tracked)

People

(Reporter: dholbert, Unassigned)

Details

Our coding style requires that pointer asterisks be "type-hugging", but syntactically they're part of the variable declaration, which creates kind of a C/C++ footgun.

If you have the following (using type-hugging asterisk per our style guide):

Foo* a, b, c;

then you'd intuitively expect that a,b,c will all have the same type -- Foo* -- but in fact only a has type Foo* whereas b and c have type Foo.

It would be great to just forbid this problematic syntax in our codebase -- i.e. if you're declaring a list of variables and at least one of them is a pointer, throw a static-analysis error, which the developer should address by declaring the variables separately. The developer could rewrite the above code as the following more-explicit equivalent:

Foo* a;
Foo b;  // or Foo* if that was in fact what they intended
Foo c;  // or Foo* if that was in fact what they intended

I don't know enough about static analysis capabilities to know if this is possible, but it'd be great (and would increase confidence in the type-hugging asterisk being a legitimate/valid thing) if we could add this check as part of our static analysis tooling.

Summary: Add static analysis to forbid `Foo* a, b, c` (i.e. declaring pointers as a list), since the "*" doesn't apply to later list items → Add static analysis to forbid `Foo* a, b, c` (i.e. declaring pointers as a list), since the pointer asterisk doesn't apply to later list items
You need to log in before you can comment on or make changes to this bug.