1 .. title:: clang-tidy - readability-suspicious-call-argument
3 readability-suspicious-call-argument
4 ====================================
6 Finds function calls where the arguments passed are provided out of order,
7 based on the difference between the argument name and the parameter names
10 Given a function call ``f(foo, bar);`` and a function signature
11 ``void f(T tvar, U uvar)``, the arguments ``foo`` and ``bar`` are swapped if
12 ``foo`` (the argument name) is more similar to ``uvar`` (the other parameter)
13 than ``tvar`` (the parameter it is currently passed to) **and** ``bar`` is
14 more similar to ``tvar`` than ``uvar``.
16 Warnings might indicate either that the arguments are swapped, or that the
17 names' cross-similarity might hinder code comprehension.
24 The following heuristics are implemented in the check.
25 If **any** of the enabled heuristics deem the arguments to be provided out of
26 order, a warning will be issued.
28 The heuristics themselves are implemented by considering pairs of strings, and
29 are symmetric, so in the following there is no distinction on which string is
30 the argument name and which string is the parameter name.
35 The most trivial heuristic, which compares the two strings for case-insensitive
38 .. _abbreviation_heuristic:
43 Common abbreviations can be specified which will deem the strings similar if
44 the abbreviated and the abbreviation stand together.
45 For example, if ``src`` is registered as an abbreviation for ``source``, then
46 the following code example will be warned about.
50 void foo(int source, int x);
54 The abbreviations to recognise can be configured with the
55 :ref:`Abbreviations<opt_Abbreviations>` check option.
56 This heuristic is case-insensitive.
61 The *prefix* heuristic reports if one of the strings is a sufficiently long
62 prefix of the other string, e.g. ``target`` to ``targetPtr``.
63 The similarity percentage is the length ratio of the prefix to the longer
64 string, in the previous example, it would be `6 / 9 = 66.66...`\%.
66 This heuristic can be configured with :ref:`bounds<opt_Bounds>`.
67 The default bounds are: below `25`\% dissimilar and above `30`\% similar.
68 This heuristic is case-insensitive.
73 Analogous to the `Prefix` heuristic.
74 In the case of ``oldValue`` and ``value`` compared, the similarity percentage
77 This heuristic can be configured with :ref:`bounds<opt_Bounds>`.
78 The default bounds are: below `25`\% dissimilar and above `30`\% similar.
79 This heuristic is case-insensitive.
84 The substring heuristic combines the prefix and the suffix heuristic, and tries
85 to find the *longest common substring* in the two strings provided.
86 The similarity percentage is the ratio of the found longest common substring
87 against the *longer* of the two input strings.
88 For example, given ``val`` and ``rvalue``, the similarity is `3 / 6 = 50`\%.
89 If no characters are common in the two string, `0`\%.
91 This heuristic can be configured with :ref:`bounds<opt_Bounds>`.
92 The default bounds are: below `40`\% dissimilar and above `50`\% similar.
93 This heuristic is case-insensitive.
95 Levenshtein distance (as `Levenshtein`)
96 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98 The `Levenshtein distance <http://en.wikipedia.org/wiki/Levenshtein_distance>`_
99 describes how many single-character changes (additions, changes, or removals)
100 must be applied to transform one string into another.
102 The Levenshtein distance is translated into a similarity percentage by dividing
103 it with the length of the *longer* string, and taking its complement with
105 For example, given ``something`` and ``anything``, the distance is `4` edits,
106 and the similarity percentage is `100`\% `- 4 / 9 = 55.55...`\%.
108 This heuristic can be configured with :ref:`bounds<opt_Bounds>`.
109 The default bounds are: below `50`\% dissimilar and above `66`\% similar.
110 This heuristic is case-sensitive.
112 Jaro--Winkler distance (as `JaroWinkler`)
113 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115 The `Jaro--Winkler distance <http://en.wikipedia.org/wiki/Jaro–Winkler_distance>`_
116 is an edit distance like the Levenshtein distance.
117 It is calculated from the amount of common characters that are sufficiently
118 close to each other in position, and to-be-changed characters.
119 The original definition of Jaro has been extended by Winkler to weigh prefix
121 The similarity percentage is expressed as an average of the common and
122 non-common characters against the length of both strings.
124 This heuristic can be configured with :ref:`bounds<opt_Bounds>`.
125 The default bounds are: below `75`\% dissimilar and above `85`\% similar.
126 This heuristic is case-insensitive.
128 Sørensen--Dice coefficient (as `Dice`)
129 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131 The `Sørensen--Dice coefficient <http://en.wikipedia.org/wiki/Sørensen–Dice_coefficient>`_
132 was originally defined to measure the similarity of two sets.
133 Formally, the coefficient is calculated by dividing `2 * #(intersection)` with
134 `#(set1) + #(set2)`, where `#()` is the cardinality function of sets.
135 This metric is applied to strings by creating bigrams (substring sequences of
136 length 2) of the two strings and using the set of bigrams for the two strings
139 This heuristic can be configured with :ref:`bounds<opt_Bounds>`.
140 The default bounds are: below `60`\% dissimilar and above `70`\% similar.
141 This heuristic is case-insensitive.
147 .. option:: MinimumIdentifierNameLength
149 Sets the minimum required length the argument and parameter names
150 need to have. Names shorter than this length will be ignored.
153 .. _opt_Abbreviations:
155 .. option:: Abbreviations
157 For the **Abbreviation** heuristic
158 (:ref:`see here<abbreviation_heuristic>`), this option configures the
159 abbreviations in the `"abbreviation=abbreviated_value"` format.
160 The option is a string, with each value joined by `";"`.
162 By default, the following abbreviations are set:
196 The configuration options for each implemented heuristic (see above) is
197 constructed dynamically.
198 In the following, `<HeuristicName>` refers to one of the keys from the
199 heuristics implemented.
201 .. option:: <HeuristicName>
203 `True` or `False`, whether a particular heuristic, such as `Equality` or
204 `Levenshtein` is enabled.
206 Defaults to `True` for every heuristic.
210 .. option:: <HeuristicName>DissimilarBelow, <HeuristicName>SimilarAbove
212 A value between `0` and `100`, expressing a percentage.
213 The bounds set what percentage of similarity the heuristic must deduce
214 for the two identifiers to be considered similar or dissimilar by the
217 Given arguments ``arg1`` and ``arg2`` passed to ``param1`` and ``param2``,
218 respectively, the bounds check is performed in the following way:
219 If the similarity of the currently passed argument order
220 (``arg1`` to ``param1``) is **below** the `DissimilarBelow` threshold, and
221 the similarity of the suggested swapped order (``arg1`` to ``param2``) is
222 **above** the `SimilarAbove` threshold, the swap is reported.
224 For the defaults of each heuristic, :ref:`see above<heuristics>`.
230 When comparing the argument names and parameter names, the following logic is
231 used to gather the names for comparison:
233 Parameter names are the identifiers as written in the source code.
237 * If a variable is passed, the variable's name.
238 * If a subsequent function call's return value is used as argument, the called
240 * Otherwise, empty string.
242 Empty argument or parameter names are ignored by the heuristics.