Bug 407981

Summary: unneeded-cast incorrectly warns in some circumstances
Product: [Developer tools] clazy Reporter: Nyall Dawson <nyall.dawson>
Component: generalAssignee: Sergio Martins <smartins>
Status: RESOLVED FIXED    
Severity: minor CC: smartins
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Nyall Dawson 2019-05-27 06:32:44 UTC
SUMMARY


STEPS TO REPRODUCE

Run the unneeded-cast check over the code:

    bool someCondition = ...;
    QLabel* label = ...;
    QLineEdit* edit = ...;
    QWidget* w = someCondition ? qobject_cast< QWidget* >( label ) : qobject_cast< QWidget* >( edit );

Clazy raises the "explicity casting to base is unnecessary" warning, yet the code will not compile if the casts are removed ("incompatible operand types QLabel* and QLineEdit*")

OBSERVED RESULT


EXPECTED RESULT


SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
Comment 1 Sergio Martins 2019-05-27 13:05:02 UTC
it should still warn, as you should use static_cast instead in that case
will fix the warning message
Comment 2 Sergio Martins 2019-05-27 13:12:43 UTC
Git commit 4918d8efba9b20b273a15ab2dd215a768ff884f9 by Sergio Martins.
Committed on 27/05/2019 at 13:12.
Pushed by smartins into branch 'master'.

unneeded-cast: Improve warning message for unneeded qobject_cast

Instead of saying that no cast to base is needed, simply say that
no qobject_cast is needed. Since a static_cast might still be needed.

This is the case for the ternary operator.

M  +13   -3    src/checks/manuallevel/unneeded-cast.cpp
M  +1    -1    src/checks/manuallevel/unneeded-cast.h
M  +2    -2    tests/unneeded-cast/main.cpp

https://commits.kde.org/clazy/4918d8efba9b20b273a15ab2dd215a768ff884f9