Skip to content

Modernize C++ tests #5386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tests/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "local_bindings.h"
#include "pybind11_tests.h"

#include <memory>
#include <utility>

PYBIND11_WARNING_DISABLE_MSVC(4324)
Expand Down Expand Up @@ -92,8 +93,8 @@ TEST_SUBMODULE(class_, m) {
// test_inheritance
class Pet {
public:
Pet(const std::string &name, const std::string &species)
: m_name(name), m_species(species) {}
Pet(std::string name, std::string species)
: m_name(std::move(name)), m_species(std::move(species)) {}
std::string name() const { return m_name; }
std::string species() const { return m_species; }

Expand Down
8 changes: 4 additions & 4 deletions tests/test_exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ class MyException6 : public std::exception {
};

struct PythonCallInDestructor {
explicit PythonCallInDestructor(const py::dict &d) : d(d) {}
explicit PythonCallInDestructor(py::dict d) : d(std::move(d)) {}
~PythonCallInDestructor() { d["good"] = true; }

py::dict d;
};

struct PythonAlreadySetInDestructor {
explicit PythonAlreadySetInDestructor(const py::str &s) : s(s) {}
explicit PythonAlreadySetInDestructor(py::str s) : s(std::move(s)) {}
~PythonAlreadySetInDestructor() {
py::dict foo;
try {
Expand All @@ -112,12 +112,12 @@ struct PythonAlreadySetInDestructor {
};

struct CustomData {
explicit CustomData(const std::string &a) : a(a) {}
explicit CustomData(std::string a) : a(std::move(a)) {}
std::string a;
};

struct MyException7 {
explicit MyException7(const CustomData &message) : message(message) {}
explicit MyException7(CustomData message) : message(std::move(message)) {}
CustomData message;
};

Expand Down
2 changes: 1 addition & 1 deletion tests/test_kwargs_and_defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
struct CustomRepr {
std::string repr_string;

explicit CustomRepr(const std::string &repr) : repr_string(repr) {}
explicit CustomRepr(std::string repr) : repr_string(std::move(repr)) {}

std::string __repr__() const { return repr_string; }
};
Expand Down
4 changes: 2 additions & 2 deletions tests/test_pickling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ TEST_SUBMODULE(pickling, m) {
// test_roundtrip
class Pickleable {
public:
explicit Pickleable(const std::string &value) : m_value(value) {}
explicit Pickleable(std::string value) : m_value(std::move(value)) {}
const std::string &value() const { return m_value; }

void setExtra1(int extra1) { m_extra1 = extra1; }
Expand Down Expand Up @@ -133,7 +133,7 @@ TEST_SUBMODULE(pickling, m) {
// test_roundtrip_with_dict
class PickleableWithDict {
public:
explicit PickleableWithDict(const std::string &value) : value(value) {}
explicit PickleableWithDict(std::string value) : value(std::move(value)) {}

std::string value;
int extra;
Expand Down
4 changes: 3 additions & 1 deletion tests/test_tagbased_polymorphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "pybind11_tests.h"

#include <utility>

struct Animal {
// Make this type also a "standard" polymorphic type, to confirm that
// specializing polymorphic_type_hook using enable_if_t still works
Expand All @@ -35,7 +37,7 @@ struct Animal {
const std::string name;

protected:
Animal(const std::string &_name, Kind _kind) : kind(_kind), name(_name) {}
Animal(std::string _name, Kind _kind) : kind(_kind), name(std::move(_name)) {}
};

struct Dog : Animal {
Expand Down
3 changes: 2 additions & 1 deletion tests/test_virtual_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "pybind11_tests.h"

#include <thread>
#include <utility>

/* This is an example class that we'll want to be able to extend from Python */
class ExampleVirt {
Expand Down Expand Up @@ -357,7 +358,7 @@ TEST_SUBMODULE(virtual_functions, m) {
};
std::string v;
A a;
explicit OverrideTest(const std::string &v) : v{v} {}
explicit OverrideTest(std::string v) : v{std::move(v)} {}
OverrideTest() = default;
OverrideTest(const OverrideTest &) = delete;
virtual std::string str_value() { return v; }
Expand Down