linter setting to ensure numeric arguments have explicit names?

Viewed 28

Is there a linter setting, preferably for clang-tidy, that identifies or pessimizes the use of direct numeric arguments.

Identify the following as bad

socket(0, 0, 0)

Identify the following as good

int domain = 0; int type = 0; int protocol = 0;
socket(domain, type, protocol)
1 Answers

There is a check named readability-magic-numbers intended for just this purpose.

There is a catch however that would prevent it from identifying your specific case:

Since values 0 and 0.0 are so common as the base counter of loops, or initialization values for sums, they are always accepted without warning, even if not present in the respective ignored values list.

I suspect rewriting the check to identify 0 values would be simple enough if you really need it to work that way.

Related