For reference, we already have C++ static code analysis to find redundant casts. However, it appears this will only find c-style casts and named casts. This leaves functional casts and braced init as undetected.
I’m looking at a context where having better rules would help discover cases where previously useful conversions are now redundant.
Consider the existance of a strong type in the interface, to separate from int
. For the sake of the argument, consider something like
struct Id
{
int id = 0;
};
If we gradually adopt this type in our code base, then it’s plausible that we would have many instances
of function calls similar to
someFunction(int{someId});
someOtherFunction(Id{someInt});
When driving adoption further through refactoring, one of these conversions is easier to detect than the other. Presumably, someInt
will have it’s type changed from int
to Id
, and plausibly renamed to someId
, however, this is due to other parts of the surrounding code. Meanwhile, the conversion around someId
will cause a compilation failure, and the conversion will simply be removed.
Either way, it would be useful to have better tooling to clean up leftover unnecessary conversions.
This might fall under a notion of an unnecessary copy, when you pass the argument into a function that takes the parameter by value (or potentially const ref).
I’m not sure if there are any plans to make a rule that suggests modernization into C++23’s auto(x)
/auto{x}
. Although I suspect that such a rule wouldn’t necessarily be meant to catch cases where the copy is unintended, this might be a convenient motivation to point at code snippets that deserve attention.