Reputation: 2424
Is there a way to get warned about missmatching of the argument names between a function declaration and its definition?
Declaration
double divide(int a, int b);
Definition
double divide(int b, int a)
{
return a / b;
}
For a user, that use the function divide
, will expect a / b as a result and not b / a.
I know, the compiler can't do that, but are there some static analysis tools that can do that? If yes, which ones?
Upvotes: 9
Views: 1553
Reputation: 12527
Are you just interested in checking existing code, or do want to ensure that the code avoids this problem and are willing to redesign the interfaces?
In the latter case:
If you have many arguments of the same type then inconsistencies can be a problem (and I have seen worse than your example):
void foo(bool fast, bool small, bool trivial);
foo(true, false, false);
What you are asking for is a way to ensure that declaration and definition of foo are consistent - but you ignore inconsistencies between call and declaration.
A different solution is to instead create an argument-struct with setRoutines. Something like:
struct FooArg {
bool fast, small, trivial;
FooArg &setFast(bool fast_) {fast=fast_;return *this;};
...
};
void foo(FooArg const&);
foo(FooArg().setFast(true).setSmall(false)...);
You then avoid the inconsistency between declaration, definition, and call, but have to be more verbose. (I am sure there is an answer about this technique somewhere.)
Upvotes: -1
Reputation: 10011
You can use clang-tidy. Calling it a compiler is a bit of a stretch, but maybe there is an option to make clang emit clang-tidy warnings. The specific option you want is readability-inconsistent-declaration-parameter-name
.
Upvotes: 12
Reputation: 3452
Compiler doesn't care about argument names in your declatation, you even can write void foo(int, int);
, so there is no mismatch, and there is no such warning. If you for some reason finding this mismatch confusing, just omit argument names in declarations.
Upvotes: 0