Search code examples
c++compiler-warningsargument-dependent-lookupclang-tidy

Warn against missing std:: prefixes due to ADL


It is possible to omit the std:: namespace for <algorithm>s when the argument types are in that namespace, which is usually the case. Is there any warning or clang-tidy rule that finds such omissions?

#include <vector>
#include <algorithm>

std::vector<int> v;
for_each(v.begin(), v.end(), [](auto){});
return 0;

The above example, compiled with the latest clang and -Wall, -Wextra and -Wpedantic does not emit any diagnostic:

https://godbolt.org/z/dTsKbbEKe


Solution

  • There is an open change in tidy that could be used to flag this:

    [patch] Summary

    This patch adds bugprone-unintended-adl which flags uses of ADL that are not on the provided whitelist.

    bugprone-unintended-adl

    Finds usages of ADL (argument-dependent lookup), or potential ADL in the case of templates, that are not on the provided lists of allowed identifiers and namespaces. [...]

    .. option:: IgnoreOverloadedOperators

    If non-zero, ignores calls to overloaded operators using operator syntax (e.g. a + b), but not function call syntax (e.g. operator+(a, b)). Default is 1.

    .. option:: AllowedIdentifiers

    Semicolon-separated list of names that the check ignores. Default is
    swap;make_error_code;make_error_condition;data;begin;end;rbegin;rend;crbegin;crend;size;ssize;empty.

    .. option:: AllowedNamespaces

    Semicolon-separated list of namespace names (e.g. foo;bar::baz). If the check finds an unqualified call that resolves to a function in a namespace in this list, the call is ignored. Default is an empty list.

    There seems to have been no activity on the patch since July 2020, though, but if this is of interest of the OP, the OP could try to resuscitate the patch.