https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110363

            Bug ID: 110363
           Summary: New use-after-move warning
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

There's a quite common rule "after the object was moved from it is in the
indeterminate state; it should be either destroyed or a new value should be
assigned to it". The C++ Standard Library follows that rule, many libraries and
companies follow that rule.


Please introduce some '-Wuse-after-move' that warns if the object could be used
after move:


struct resource {
    resource(resource&&) noexcept;
    ~resource();

    void kill_it() && noexcept;
    void should_warn_use_after_move() const & noexcept;
};

void should_warn_use_after_move(resource& r) noexcept;

void do_something(resource r) {
    static_cast<resource&&>(r).kill_it(); // moved out
    should_warn_use_after_move(r);        // warn
    r.should_warn_use_after_move();       // warn
}


Some related request on stackoverflow
https://stackoverflow.com/questions/72532377/g-detect-use-after-stdmove

Reply via email to