Add more cases to the "swapped args" case of
bugprone-string-constructor check when the first argument is
a reference to a char variable or function that returns a char.
Calls to the fill constructor where both arguments are
implicitly cast is "confusing" since the implicit cast may not be
apparent to the reader. In many cases, this is a programmer error. In
cases where it's not an error, the code should use explicit casts to
clearly specify intent.
Note that the swapped args case is a subset of the "confusing args"
case. Not all confusing args are swapped args. Consider
char buffer = "abcdef"; std::string not_swapped(buffer, 2); // oops! should be '&buffer' assert(not_swapped == "bc");
The only case where the swapped args is assumed is when the
first argument to the fill constructor is one where it is not possible
the author forgot to prefix the expression with &. I.e., when the
first arg is
- char literal
- declRefExpr to variable of type char (but not char&).
- function that returns char
With the declRefExpr case, it is possible to take the address
of a variable of char type, but only if the number of bytes
read is no more than 1:
char ch; std::string fill(ch, 1); // intended 'std::string fill(1, ch)' instead? // or intended 'std::string fill(&ch, 1)'? // both lead to the same end result anyway, but // the former is cleaner. so swapping here is safe.