[clang-tools-extra] [clang-tidy] Enhance modernize-use-starts-ends-with to handle substr patterns (PR #116033)
Nicolas van Kempen via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 15 13:08:45 PST 2024
================
@@ -7,26 +7,39 @@ Checks for common roundabout ways to express ``starts_with`` and ``ends_with``
and suggests replacing with the simpler method when it is available. Notably,
this will work with ``std::string`` and ``std::string_view``.
-.. code-block:: c++
+The check handles the follow ing expressions:
- std::string s = "...";
- if (s.find("prefix") == 0) { /* do something */ }
- if (s.rfind("prefix", 0) == 0) { /* do something */ }
- if (s.compare(0, strlen("prefix"), "prefix") == 0) { /* do something */ }
- if (s.compare(s.size() - strlen("suffix"), strlen("suffix"), "suffix") == 0) {
- /* do something */
- }
- if (s.rfind("suffix") == (s.length() - 6)) {
- /* do something */
- }
-
-becomes
+====================================================== =====================
+Expression Replacement
+------------------------------------------------------ ---------------------
+``u.find(v) == 0`` ``u.starts_with(v)``
+``u.rfind(v, 0) != 0`` ``!u.starts_with(v)``
+``u.compare(0, v.size(), v) == 0`` ``u.starts_with(v)``
+``u.substr(0, v.size()) == v`` ``u.starts_with(v)``
+``v == u.substr(0, v.size())`` ``u.starts_with(v)``
+``u.substr(0, v.size()) != v`` ``!u.starts_with(v)``
+``u.compare(u.size() - v.size(), v.size(), v) == 0`` ``u.ends_with(v)``
+``u.rfind(v) == u.size() - v.size()`` ``u.ends_with(v)``
+====================================================== =====================
+
+For example:
----------------
nicovank wrote:
I think everything below the table can disappear.
- The example is somewhat redundant, the info is already in the table.
- The information about changes not being made unless guaranteed correct is assumed in Clang-Tidy, the aim is no false positives especially for checks that provide fixes.
https://github.com/llvm/llvm-project/pull/116033
More information about the cfe-commits
mailing list