3

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

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
  • ADL is one of Titus Winters's *Scary Stories*. Are you trying to disable ADL? Warn about ADL use? Lean into ADL to get diagnostics when `std::` is superfluous due to ADL? – Eljay Dec 07 '21 at 13:56
  • If I understand correctly, you want a warning to be emitted if a function from `namespace std` is called without the `std::` qualification? – AndyG Dec 07 '21 at 13:58
  • @Eljay ADL is totally fine for `operator<<` and the likes. I just want to enforce usage of `std::` prefixes for explicit library function calls. It's mostly a coding guidelines thing. – Felix Dombek Dec 07 '21 at 13:59
  • I see. Hmm. I'd like the same thing. (Titus had a good presentation on how to thwart ADL, but that was for your own code, not `std`.) If you took the C++ source (like GCC or Clang) and disable ADL (except still enable for `operator@`), you might be able to come close. – Eljay Dec 07 '21 at 14:03
  • 1
    Notice that given code doesn't necessary compile as `std::vector::iterator` might simply be `int*` (or type not in namespace `std`). – Jarod42 Dec 07 '21 at 14:07

1 Answers1

6

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.

dfrib
  • 70,367
  • 12
  • 127
  • 192