65 lines · plain
1.. title:: clang-tidy - bugprone-suspicious-string-compare2 3bugprone-suspicious-string-compare4==================================5 6Find suspicious usage of runtime string comparison functions.7This check is valid in C and C++.8 9Checks for calls with implicit comparator and proposed to explicitly add it.10 11.. code-block:: c++12 13 if (strcmp(...)) // Implicitly compare to zero14 if (!strcmp(...)) // Won't warn15 if (strcmp(...) != 0) // Won't warn16 17Checks that compare function results (i.e., ``strcmp``) are compared to valid18constant. The resulting value is19 20.. code::21 22 < 0 when lower than,23 > 0 when greater than,24 == 0 when equals.25 26A common mistake is to compare the result to `1` or `-1`.27 28.. code-block:: c++29 30 if (strcmp(...) == -1) // Incorrect usage of the returned value.31 32Additionally, the check warns if the results value is implicitly cast to a33*suspicious* non-integer type. It's happening when the returned value is34used in a wrong context.35 36.. code-block:: c++37 38 if (strcmp(...) < 0.) // Incorrect usage of the returned value.39 40Options41-------42 43.. option:: WarnOnImplicitComparison44 45 When `true`, the check will warn on implicit comparison. `true` by default.46 47.. option:: WarnOnLogicalNotComparison48 49 When `true`, the check will warn on logical not comparison. `false` by default.50 51.. option:: StringCompareLikeFunctions52 53 A string specifying the comma-separated names of the extra string comparison54 functions. Default is an empty string.55 The check will detect the following string comparison functions:56 `__builtin_memcmp`, `__builtin_strcasecmp`, `__builtin_strcmp`,57 `__builtin_strncasecmp`, `__builtin_strncmp`, `_mbscmp`, `_mbscmp_l`,58 `_mbsicmp`, `_mbsicmp_l`, `_mbsnbcmp`, `_mbsnbcmp_l`, `_mbsnbicmp`,59 `_mbsnbicmp_l`, `_mbsncmp`, `_mbsncmp_l`, `_mbsnicmp`, `_mbsnicmp_l`,60 `_memicmp`, `_memicmp_l`, `_stricmp`, `_stricmp_l`, `_strnicmp`,61 `_strnicmp_l`, `_wcsicmp`, `_wcsicmp_l`, `_wcsnicmp`, `_wcsnicmp_l`,62 `lstrcmp`, `lstrcmpi`, `memcmp`, `memicmp`, `strcasecmp`, `strcmp`,63 `strcmpi`, `stricmp`, `strncasecmp`, `strncmp`, `strnicmp`, `wcscasecmp`,64 `wcscmp`, `wcsicmp`, `wcsncmp`, `wcsnicmp`, `wmemcmp`.65