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
When shouldn't QString::left(1) be replaced by at(0) ? Wondering if we should just warn for all occurrences.
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.
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