Bug 354124 - new check: Add check for use of [] on containers when not modifying value
Summary: new check: Add check for use of [] on containers when not modifying value
Status: RESOLVED FIXED
Alias: None
Product: clazy
Classification: Developer tools
Component: general (other bugs)
Version First Reported In: unspecified
Platform: Other Linux
: NOR wishlist
Target Milestone: ---
Assignee: Sergio Martins
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-20 11:20 UTC by Nyall Dawson
Modified: 2015-11-01 20:06 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 Nyall Dawson 2015-10-20 11:20:15 UTC
A check for use of [] on non-const containers when the value isn't being modified would be handy - this could also potentially avoid a detach of the container.
Comment 1 Sergio Martins 2015-10-20 14:13:22 UTC
I was thinking of a more general check, that included other methods, like first(), last() etc.
Comment 2 Sergio Martins 2015-10-22 18:30:05 UTC
commit 6c32f836d79cee3b1716d3e99ae185ad55df877f
Author: Sergio Martins <smartins@kde.org>
Date:   Thu Oct 22 18:59:19 2015 +0100

    WIP: Introducing detaching-members check
    
    Lots of improvements can be done, for example:
    container[0] = ..; // This detachment is unavoidable and desired, yet we are emitting a warning
Comment 3 Sergio Martins 2015-11-01 20:04:29 UTC
Git commit a8f25a126b8405884557d8d82dff4c586d176e3f by Sergio Martins.
Committed on 01/11/2015 at 19:58.
Pushed by smartins into branch 'master'.

detaching-member: Fix a bunch of false-positives

such as:

a[0] = "foo"; These kind of detachments are fine and unavoidable

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

http://commits.kde.org/clazy/a8f25a126b8405884557d8d82dff4c586d176e3f
Comment 4 Sergio Martins 2015-11-01 20:06:59 UTC
Seems to be working nicely for member variables.
For local variables it's a bit more difficult, since most cases will be false-positives, so I won't work on that.
Feel free to try, if it has a low rate of false-positives it can be accepted into clazy