Bug 360374 - Check for QString::left(1) mis-use.
Summary: Check for QString::left(1) mis-use.
Status: RESOLVED FIXED
Alias: None
Product: clazy
Classification: Developer tools
Component: general (show other bugs)
Version: unspecified
Platform: Other Linux
: NOR wishlist
Target Milestone: ---
Assignee: Sergio Martins
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-10 18:37 UTC by Volker Krause
Modified: 2016-05-05 19:18 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 Volker Krause 2016-03-10 18:37:25 UTC
QString::left(1) is an inefficient way of saying QString::at(0) in most cases.

A quick grep over Qt and KDE sources show basically two use-cases (about 40-50 hits):
* str.left(1) == QStringLiteral("#") 
* str.left(1).toUpper() - typically used as part of string concatenations
Both cases would work with at(0) just as well, and at least the latter one isn't caught yet by clazy.

Reproducible: Always
Comment 1 Sergio Martins 2016-04-08 21:49:29 UTC
When shouldn't QString::left(1)  be replaced by at(0) ?

Wondering if we should just warn for all occurrences.
Comment 2 Volker Krause 2016-04-13 12:43:29 UTC
I haven't found a valid use of left(1) yet, so an unconditional warning would probably be fine. In theory it differs from at(0) in that it returns a QString rather than a QChar, which doesn't matter for string concatenations or comparisons, but might be relevant in some other context.
Comment 3 Sergio Martins 2016-05-05 19:18:30 UTC
Git commit bb1c95331786149d0682be67653a725366358f0f by Sergio Martins.
Committed on 05/05/2016 at 19:16.
Pushed by smartins into branch 'master'.

Introduce qstring-left check

Warns when using QString::left(1) or QString::left(0).
The former should be replaced by at(0), the later shouldn't be used
and is probably a typo.

M  +1    -0    CMakeLists.txt
M  +1    -0    README
A  +4    -0    checks/README-qstring-left
A  +64   -0    checks/qstring-left.cpp     [License: LGPL (v2+)]
A  +39   -0    checks/qstring-left.h     [License: LGPL (v2+)]
M  +5    -5    tests/clazy/test_requested_checks.sh.expected
A  +7    -0    tests/qstring-left/config.json
A  +13   -0    tests/qstring-left/main.cpp     [License: UNKNOWN]  *
A  +2    -0    tests/qstring-left/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/bb1c95331786149d0682be67653a725366358f0f