Skip to content

[BUG] Unused variable check only applies to a potentially movable local #890

Closed

Description

@JohelEGP

Title: Unused variable check only applies to a potentially movable local.

Minimal reproducer (https://cpp2.godbolt.org/z/jre1681ae):

f: (x) = { }
g: (copy y) = { }
main: () = { }
Commands:
cppfront main.cpp2
clang++18 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -Werror=unused-result -Werror=unused-value -Werror=unused-parameter -Werror=unused-variable -I . main.cpp

Expected result: For x to be diagnosed as unused.

Actual result and error: Only y is diagnosed as unused.

Output:
main.cpp2...
main.cpp2(2,10): error: local variable y is not used; consider changing its name to '_' to make it explicitly anonymous, or removing it entirely if its side effects are not needed

Activity

  1. JohelEGP commented on Dec 18, 2023

    @JohelEGP
    ContributorAuthor

    It's also necessary to recognize shadowing.
    In this case, the in parameter f is moved
    instead of the last use of the first f (https://cpp2.godbolt.org/z/W4nceobbv):

    t: @struct type = {
      f: (this) -> int == 0;
    }
    main: () = {
      // {
        f: _ = t().f();
        assert(f == 0);
      // }
      (f := t().f()) assert(f == 0);
    }
    
    auto main() -> int{
      // {
        auto f {t().f()}; 
        cpp2::Default.expects(f == 0, "");
    {
    auto const& f = t().f();
      // }
      cpp2::Default.expects(std::move(f) == 0, "");
    }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions