-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang] Warn on unqualified calls to std::move and std::forward
This adds a diagnostic when an unqualified call is resolved to std::move or std::forward. This follows some C++ committee discussions where some people where concerns that this might be an usual anti pattern particularly britle worth warning about - both because move is a common name and because these functions accept any values. This warns inconditionnally of whether the current context is in std:: or not, as implementations probably want to always qualify these calls too, to avoid triggering adl accidentally. Differential Revision: https://reviews.llvm.org/D119670
- Loading branch information
Showing
5 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// RUN: %clang_cc1 -verify -std=c++20 -Wall %s | ||
// RUN: cp %s %t | ||
// RUN: %clang_cc1 -x c++ -std=c++20 -fixit %t | ||
// RUN: %clang_cc1 -Wall -Werror -x c++ -std=c++20 %t | ||
// RUN: cat %t | FileCheck %s | ||
|
||
namespace std { | ||
|
||
void move(auto &&a) {} | ||
|
||
void forward(auto &a) {} | ||
|
||
} // namespace std | ||
|
||
using namespace std; | ||
|
||
void f() { | ||
int i = 0; | ||
move(i); // expected-warning {{unqualified call to std::move}} | ||
// CHECK: {{^}} std:: | ||
forward(i); // expected-warning {{unqualified call to std::forward}} | ||
// CHECK: {{^}} std:: | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify -Wall -std=c++11 %s | ||
|
||
namespace std { | ||
|
||
template <typename T> | ||
void dummy(T &&) {} | ||
template <typename T> | ||
void move(T &&) {} | ||
template <typename T, typename U> | ||
void move(T &&, U &&) {} | ||
|
||
inline namespace __1 { | ||
template <typename T> | ||
void forward(T &) {} | ||
} // namespace __1 | ||
|
||
struct foo {}; | ||
|
||
} // namespace std | ||
|
||
namespace global { | ||
|
||
using namespace std; | ||
|
||
void f() { | ||
int i = 0; | ||
std::move(i); | ||
move(i); // expected-warning{{unqualified call to std::move}} | ||
(move)(i); // expected-warning{{unqualified call to std::move}} | ||
std::dummy(1); | ||
dummy(1); | ||
std::move(1, 2); | ||
move(1, 2); | ||
forward<int>(i); // expected-warning{{unqualified call to std::forward}} | ||
std::forward<int>(i); | ||
} | ||
|
||
template <typename T> | ||
void g(T &&foo) { | ||
std::move(foo); | ||
move(foo); // expected-warning{{unqualified call to std::move}} | ||
|
||
std::forward<decltype(foo)>(foo); | ||
forward<decltype(foo)>(foo); // expected-warning{{unqualified call to std::forward}} | ||
move(1, 2); | ||
dummy(foo); | ||
} | ||
|
||
void call() { | ||
g(0); //expected-note {{here}} | ||
} | ||
|
||
} // namespace global | ||
|
||
namespace named { | ||
|
||
using std::forward; | ||
using std::move; | ||
|
||
void f() { | ||
int i = 0; | ||
move(i); // expected-warning{{unqualified call to std::move}} | ||
move(1, 2); | ||
forward<int>(i); // expected-warning{{unqualified call to std::forward}} | ||
} | ||
|
||
template <typename T> | ||
void g(T &&foo) { | ||
move(foo); // expected-warning{{unqualified call to std::move}} | ||
forward<decltype(foo)>(foo); // expected-warning{{unqualified call to std::forward}} | ||
(forward<decltype(foo)>)(foo); // expected-warning{{unqualified call to std::forward}} | ||
move(1, 2); | ||
} | ||
|
||
void call() { | ||
g(0); //expected-note {{here}} | ||
} | ||
|
||
} // namespace named | ||
|
||
namespace overload { | ||
using namespace std; | ||
template <typename T> | ||
int move(T &&); | ||
void f() { | ||
int i = 0; | ||
move(i); | ||
} | ||
} // namespace overload | ||
|
||
namespace adl { | ||
void f() { | ||
move(std::foo{}); // expected-warning{{unqualified call to std::move}} | ||
} | ||
|
||
} // namespace adl | ||
|
||
namespace std { | ||
|
||
void f() { | ||
int i = 0; | ||
move(i); // expected-warning{{unqualified call to std::move}} | ||
forward<int>(i); // expected-warning{{unqualified call to std::forward}} | ||
} | ||
|
||
} // namespace std | ||
|
||
namespace test_alias { | ||
namespace alias = std; | ||
using namespace alias; | ||
void f() { | ||
int i = 0; | ||
move(i); // expected-warning{{unqualified call to std::move}} | ||
move(1, 2); | ||
forward<int>(i); // expected-warning{{unqualified call to std::forward}} | ||
} | ||
|
||
} // namespace test_alias |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters