hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.39k stars 232 forks source link

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

Closed JohelEGP closed 6 months ago

JohelEGP commented 8 months ago

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: ```bash 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

JohelEGP commented 8 months ago

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, "");
}
}