Skip to content

[clang-tidy] Check request: modernize-use-std-tie-to-decompose-result #138732

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

Open
denzor200 opened this issue May 6, 2025 · 1 comment
Open
Labels
check-request Request for a new check in clang-tidy clang-tidy

Comments

@denzor200
Copy link

Needs a check that will find creations of a tuple-like which was assigned from a returned value. The check will show warning to change it to use std::tie.
Also the check will provide fix-it in the case when it's possible to deduce names for future "tied" variables.

BEFORE:

iterator it;
bool succeed;
const auto results = mapping.try_emplace("hello!");      // WARNING AND FIX-IT
it = results.first;
succeed = results.second; // deduced 'it' and 'succeed'

AFTER:

iterator it;
bool succeed;
std::tie(it, succeed) = mapping.try_emplace("hello!"); // use 'it' and 'succeed'

The check will respect original order of default constructors, copy constructors, and assignment operators for a not built-in type and will not provide a fix-it that will broke the order - only warning will be emited:

const auto results = mapping.try_emplace("hello!");      // WARNING
const iterator& it = results.first; // only copy constructor being called
                                    // but with `std::tie` we would call default constructor, copy constructor and then assignment operator
const bool succeed = results.second;

Another example with a built-in type:

const auto pos = get_position();                         // WARNING AND FIX-IT
const float x = pos.first;
const float y = pos.second;

Keep in mind that the check will not provide fix-it in the case when it's impossible to deduce names - just a warning would be enough:

const auto results = mapping.try_emplace("hello!");      // WARNING
if (results.second) {
    handle_inserted(results.first);
}
// no name deduced

In the case when it's impossible to use std::tie no warning must be provided:

const auto results = mapping.try_emplace("hello!");
handle_inserted(results);
@llvmbot
Copy link
Member

llvmbot commented May 6, 2025

@llvm/issue-subscribers-clang-tidy

Author: Denis Mikhailov (denzor200)

Needs a check that will find creations of a tuple-like which was assigned from a returned value. The check will show warning to change it to use `std::tie`. Also the check will provide fix-it in the case when it's possible to deduce names for future "tied" variables.

BEFORE:

iterator it;
bool succeed;
const auto results = mapping.try_emplace("hello!");      // WARNING AND FIX-IT
it = results.first;
succeed = results.second; // deduced 'it' and 'succeed'

AFTER:

iterator it;
bool succeed;
std::tie(it, succeed) = mapping.try_emplace("hello!"); // use 'it' and 'succeed'

The check will respect original order of default constructors, copy constructors, and assignment operators for a not built-in type and will not provide a fix-it that will broke the order - only warning will be emited:

const auto results = mapping.try_emplace("hello!");      // WARNING
const iterator& it = results.first; // only copy constructor being called
                                    // but with `std::tie` we would call default constructor, copy constructor and then assignment operator
const bool succeed = results.second;

Another example with a built-in type:

const auto pos = get_position();                         // WARNING AND FIX-IT
const float x = pos.first;
const float y = pos.second;

Keep in mind that the check will not provide fix-it in the case when it's impossible to deduce names - just a warning would be enough:

const auto results = mapping.try_emplace("hello!");      // WARNING
if (results.second) {
    handle_inserted(results.first);
}
// no name deduced

In the case when it's impossible to use std::tie no warning must be provided:

const auto results = mapping.try_emplace("hello!");
handle_inserted(results);

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

3 participants