Bug 356699

Summary: detaching-member: don't warn when non-const method is called on item
Product: [Developer tools] clazy Reporter: Nyall Dawson <nyall.dawson>
Component: generalAssignee: Sergio Martins <smartins>
Status: RESOLVED FIXED    
Severity: normal 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 2015-12-14 21:04:39 UTC
Currently clazy throws an incorrect warning with the detaching-member test when a non-const method is called. For instance,

list[1]->someNonConstMethod();

is incorrectly throwing the detach warning.
Comment 1 Sergio Martins 2015-12-14 21:34:16 UTC
the warning is because of [1] no ?
Comment 2 Nyall Dawson 2015-12-14 21:46:09 UTC
yes, but in this case there's no way around calling the detaching [] operator
Comment 3 Sergio Martins 2015-12-14 22:51:45 UTC
you can use .at(0), which doesn't detach:

#include <QVector>
#include <QDebug>

struct T
{
    void nonConst()
    {
        qDebug() << "T::nonConst";
    }
};

int main()
{
    QVector<T*> vec = {new T()};
    vec.at(0)->nonConst();

    return 0;
}
Comment 4 Sergio Martins 2015-12-14 23:02:27 UTC
however for values instead of pointers you're correct
Comment 5 Nyall Dawson 2015-12-14 23:12:29 UTC
Yes - that's what I was referring to (values). Sorry, should have made that clearer to begin with!
Comment 6 Nyall Dawson 2015-12-14 23:13:08 UTC
...And I also just realised my example was very poorly chosen ;)
Comment 7 Sergio Martins 2015-12-15 11:24:49 UTC
Git commit a85e4e1b9da90970d6270cb2674aa41eb0bbe933 by Sergio Martins.
Committed on 15/12/2015 at 11:23.
Pushed by smartins into branch 'master'.

detaching-member: Fix false positive

When calling m_listOfValues[0].nonConstMethod() we can't use .at()

M  +11   -1    checks/detachingmember.cpp
M  +1    -1    tests/detaching-member/main.cpp
M  +1    -1    tests/detaching-member/main.cpp.expected

http://commits.kde.org/clazy/a85e4e1b9da90970d6270cb2674aa41eb0bbe933