Bug 109515 - Diagnostic request: warning on out-of-order structured bindings names
Summary: Diagnostic request: warning on out-of-order structured bindings names
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2023-04-14 17:50 UTC by Barry Revzin
Modified: 2023-04-14 20:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Barry Revzin 2023-04-14 17:50:31 UTC
Consider this example:

struct div_t {
    int quot;
    int rem;
};

auto div(int, int) -> div_t;

int main() {
    auto [rem, quot] = div(1, 2);
    return quot;
}

I'm using structured bindings, but div_t's members are in the order quot then rem, but I accidentally typed rem then quot. This is a bug! Currently, nobody warns here though.

At the very least, if I'm:
(a) using a name in a structured binding,
(b) where we fall into the 3rd case of structured bindings (struct, not array/tuple),
(c) the name I'm using is the name of one of the members of the type, and
(d) the structured binding is in the wrong place

I think that's a situation where a warning would have a low false-positive rate, could be a useful -Wall (or at least -Wextra) kind of warning. 

There's a more expansive potential diagnostic, if I used names that weren't names of members at all, but I think that sort of warning would have to be opt-in.