Bug 410714 - clazy-detaching-temporary: Don't warn on detaching pointer values
Summary: clazy-detaching-temporary: Don't warn on detaching pointer values
Status: RESOLVED NOT A BUG
Alias: None
Product: clazy
Classification: Developer tools
Component: general (other bugs)
Version First Reported In: unspecified
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Unassigned bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-08-08 05:42 UTC by Markus
Modified: 2019-08-25 13:22 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Markus 2019-08-08 05:42:09 UTC
SUMMARY
I am currently fixing deprecation warnings for QChart in Qt 5.12. The following link shows the suggested change (Qt examples changed with adding the deprecation message):
https://codereview.qt-project.org/c/qt/qtcharts/+/227902/4/examples/charts/scatterinteractions/chartview.cpp

Doing this gives me a warning:
warning: Don't call QList::first() on temporary [-Wclazy-detaching-temporary]

I believe calling QList::first() is no problem here, it only means that a pointer is copied, which is not a problem IMHO.


STEPS TO REPRODUCE

Run clazy on the Scatter Interactions Sample in Qt 5.12.


OBSERVED RESULT

Clazy returns clazy-detaching-temporary


EXPECTED RESULT

Clazy should not return an error here.


SOFTWARE/OS VERSIONS

Running clazy from QtC 4.9.2, Qt version is 5.12.4.
Comment 1 Sergio Martins 2019-08-08 12:30:01 UTC
That QList is not a pointer

It's doing axes(Qt::Horizontal).first(), not axes(Qt::Horizontal)->first()
Comment 2 Markus 2019-08-08 13:17:26 UTC
(In reply to Sergio Martins from comment #1)
> That QList is not a pointer
> 
> It's doing axes(Qt::Horizontal).first(), not axes(Qt::Horizontal)->first()

This is of course correct, sorry for being unclear in my description. 

I was referring to the contents of the QList - the QList contains pointers to the axes, and it is safe and intended to manipulate these via the returned temporary QList directly.

I reported this because it just didn't make sense to me to see a warning in this case, for the following reasons: 
* The warning is specific to the call to QList::first(), which is likely to get called on small lists. Here it's a case where typically only one item is returned.
* If the list contents are pointers, it's not so expensive to have a detaching copy, nor is it so problematic since the pointers in the list are still pointing to valid objects.
* The warning is about a "detaching-temporary". This first made me think that "axes(..).first()" points to a temporary and not the real pointee. But this is not the case.
* It seems to be the suggested way to use the API of QChart ...
Comment 3 Sergio Martins 2019-08-08 19:23:21 UTC
It's indeed safe, but the warning is not about safety, it's about an unneeded QList copy when detaching a temporary.

The performance impact can vary from negligeable if the list is small and detach called just a few times to high if the list is big and detached often.


Clazy can't know the size of the list nor does it know how often some method is called (could even be called in a loop from another translation unit).


By using constFirst() instead of first() then we're certain that there's no detach and there's no guessing needed in the first place.