Skip to content
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

[clang-tidy] Check request: bugprone-explicit-move-constructor #121707

Open
dodicidodici opened this issue Jan 5, 2025 · 2 comments
Open

[clang-tidy] Check request: bugprone-explicit-move-constructor #121707

dodicidodici opened this issue Jan 5, 2025 · 2 comments
Labels
check-request Request for a new check in clang-tidy clang-tidy

Comments

@dodicidodici
Copy link

When making a move constructor explicit, code may end up invoking the class' copy constructor instead. For Example

#include <utility>

class expensive {
public:
    expensive() = default;
    
    expensive(const expensive& other) {
        // ...
    }
    
    explicit expensive(expensive&& other) {
        // ...
    }
};

void process(expensive e) {
    // ...
}

int main() {
    expensive e{};
    process(std::move(e));
    
    return 0;
}

In this case, the program is not going to invoke the move constructor, but rather, the copy constructor. Changing the function call to process(explicit{std::move(e)}) is going to move e.

Obviously, if the copy constructor is deleted, then this is not an issue as the code is just going to fail to compile. Let me know if this check can be added so I can open a pull request for it.

Thanks in advance.

@llvmbot
Copy link
Member

llvmbot commented Jan 5, 2025

@llvm/issue-subscribers-clang-tidy

Author: dodicidodici (dodicidodici)

When making a move constructor `explicit`, code may end up invoking the class' copy constructor instead. For Example
#include &lt;utility&gt;

class expensive {
public:
    expensive() = default;
    
    expensive(const expensive&amp; other) {
        // ...
    }
    
    explicit expensive(expensive&amp;&amp; other) {
        // ...
    }
};

void process(expensive e) {
    // ...
}

int main() {
    expensive e{};
    process(std::move(e));
    
    return 0;
}

In this case, the program is not going to invoke the move constructor, but rather, the copy constructor. Changing the function call to process(explicit{std::move(e)}) is going to move e.

Obviously, if the copy constructor is deleted, then this is not an issue as the code is just going to fail to compile. Let me know if this check can be added so I can open a pull request for it.

Thanks in advance.

@EugeneZelenko EugeneZelenko added the check-request Request for a new check in clang-tidy label Jan 6, 2025
@denzor200
Copy link

I suppose this not a "bugprone" and should be in "performance" section

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
check-request Request for a new check in clang-tidy clang-tidy
Projects
None yet
Development

No branches or pull requests

4 participants