Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/bugprone/suspicious-string-compare.rst

raw
1 .. title:: clang-tidy - bugprone-suspicious-string-compare
2
3 bugprone-suspicious-string-compare
4 ==================================
5
6 Find suspicious usage of runtime string comparison functions.
7 This check is valid in C and C++.
8
9 Checks for calls with implicit comparator and proposed to explicitly add it.
10
11 .. code-block:: c++
12
13 if (strcmp(...)) // Implicitly compare to zero
14 if (!strcmp(...)) // Won't warn
15 if (strcmp(...) != 0) // Won't warn
16
17 Checks that compare function results (i.e., ``strcmp``) are compared to valid
18 constant. The resulting value is
19
20 .. code::
21
22 < 0 when lower than,
23 > 0 when greater than,
24 == 0 when equals.
25
26 A 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
32 Additionally, the check warns if the results value is implicitly cast to a
33 *suspicious* non-integer type. It's happening when the returned value is used in
34 a wrong context.
35
36 .. code-block:: c++
37
38 if (strcmp(...) < 0.) // Incorrect usage of the returned value.
39
40 Options
41 -------
42
43 .. option:: WarnOnImplicitComparison
44
45 When `true`, the check will warn on implicit comparison. `true` by default.
46
47 .. option:: WarnOnLogicalNotComparison
48
49 When `true`, the check will warn on logical not comparison. `false` by default.
50
51 .. option:: StringCompareLikeFunctions
52
53 A string specifying the comma-separated names of the extra string comparison
54 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`.