Bug 355682 - new check: test QList.count() must be replaced by !QList.isEmpty()
Summary: new check: test QList.count() must be replaced by !QList.isEmpty()
Status: RESOLVED FIXED
Alias: None
Product: clazy
Classification: Developer tools
Component: general (other bugs)
Version First Reported In: unspecified
Platform: Kubuntu Linux
: NOR wishlist
Target Milestone: ---
Assignee: Unassigned bugs mailing-list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-21 09:53 UTC by Stephane MANKOWSKI
Modified: 2016-02-04 08:24 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 Stephane MANKOWSKI 2015-11-21 09:53:36 UTC
QList list;
if(list.count) {
}

must raise a warning and must be fixed by:
QList list;
if(!list.isEmpty()) {
}

Reproducible: Always
Comment 1 Sergio Martins 2015-12-07 18:43:46 UTC
Git commit ef337e2facc0dcae72c84917d0c678b02031b06e by Sergio Martins.
Committed on 07/12/2015 at 18:40.
Pushed by smartins into branch 'master'.

Introduce isempty-vs-count check

if (list.count()) -> if (!list.isEmpty())

This is just a warning, if someone is interested in doing the fixit please
volunteer.

M  +1    -0    CMakeLists.txt
A  +9    -0    checks/README-isempty-vs-count
A  +62   -0    checks/isempty-vs-count.cpp     [License: GPL (v2+) (+Qt exception)]
A  +46   -0    checks/isempty-vs-count.h     [License: GPL (v2+) (+Qt exception)]
M  +11   -11   tests/clazy/test_requested_checks.sh.expected
A  +7    -0    tests/isempty-vs-count/config.json
A  +10   -0    tests/isempty-vs-count/main.cpp     [License: UNKNOWN]  *
A  +3    -0    tests/isempty-vs-count/main.cpp.expected

The files marked with a * at the end have a non valid license. Please read: http://techbase.kde.org/Policies/Licensing_Policy and use the headers which are listed at that page.


http://commits.kde.org/clazy/ef337e2facc0dcae72c84917d0c678b02031b06e
Comment 2 Stephane MANKOWSKI 2015-12-08 19:10:20 UTC
Hi,

Thank you, this helps me a lot.

Just one more comment: could be interesting to generate warning for the 2 followings cases too !
    if (list.count()>0) {} 
    if (list.count()==0) {}
Comment 3 eric.lemanissier 2016-02-04 08:24:20 UTC
The suggestion to replace container::count with !container::isEmpty works only if there is no argument to container::count. Otherwise container::contains should be suggested