cppcheck icon indicating copy to clipboard operation
cppcheck copied to clipboard

cfg/qt.cfg - Change the Q_D definition to avoid false positives

Open winterz opened this issue 6 months ago • 6 comments

Change the O_D macro definition by moving the 'const' qualifier to the front of the statement.

Without this change we get false positives "style: Variable 'd' can be declared as pointer to const [constVariablePointer]"

The current definition follows Qt correctly as we see this define in the QtCore/qtclasshelpermacros.h file: #define Q_D(Class) Class##Private * const d = d_func()

but apparently cppcheck doesn't properly recognize that as a const variable pointer.

winterz avatar Jun 25 '25 20:06 winterz

Can you add a test in qt.cpp?

chrchr-github avatar Jun 27 '25 13:06 chrchr-github

yes, I can add a test.

however, I think a larger problem is that cppcheck seems to not like FooPrivate * const d = d_func() whereas it seems ok with const FooPrivate *d = d_func() ? unless something else is happening.

winterz avatar Jun 27 '25 13:06 winterz

however, I think a larger problem is that cppcheck seems to not like FooPrivate * const d = d_func() whereas it seems ok with const FooPrivate *d = d_func() ? unless something else is happening.

some simple sample code that demonstrate a problem would be nice. we should like the first code as far as I know.

danmar avatar Jun 28 '25 19:06 danmar

I wrote a test here but it isn't showing any problems. must be something else is happening . I'll look closer.

in my test, I did run into a checkLibraryFunction issue for qGetPtrHelper from the Q_DECLARE_PRIVATE macro. I'm not sure where to look or where to set qGetPtrHelper . can I get a hint?

winterz avatar Jul 01 '25 12:07 winterz

I'm not sure where to look or where to set qGetPtrHelper . can I get a hint?

hmm.. you already identified that it's from Q_DECLARE_PRIVATE

if a simple configuration is provided like:

    <function name="qGetPtrHelper">
         <arg nr="1"/>
    </function>

does it work better then?

That configuration is not ideal. Cppcheck can not know what value it returns then. However I am not sure that providing the template would help neither the type deduction can be too complex.

danmar avatar Jul 02 '25 13:07 danmar

I am not 100% sure that providing the exact real replacement lists for various macros is always a great idea. Maybe sometimes it's preferable to simplify the code. If this applies to the Q_DECLARE_PRIVATE I don't know, I've never actually used that macro myself in my code.

danmar avatar Jul 02 '25 13:07 danmar