Fix ticket #4900 (updated)

This PR is a redo of https://github.com/danmar/cppcheck/pull/150 so that it applies cleanly to the current source base. Thanks to pull.

Simon