Skip to content
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

clang-tidy 14.0.6 gives a false-positive bugprone-use-after-move report in chained expressions #57758

Open
georgthegreat opened this issue Sep 15, 2022 · 10 comments
Labels
clang-tidy false-positive Warning fires when it should not

Comments

@georgthegreat
Copy link
Contributor

georgthegreat commented Sep 15, 2022

Consider the following code

#include <cassert>
#include <vector>

struct test {
 test& foo(std::vector<int> const& v) { assert(v.size() == 1); return *this; }
 int bar(std::vector<int> v ) { return v.size(); }
};

int main() {
    std::vector<int> v = { 42 };
    return test{}.foo(v).bar(std::move(v));

}

clang-tidy 14.0.6 reports the following error:

tidy14-bug/main.cpp:11:23: error: 'v' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    return test{}.foo(v).bar(std::move(v));
                      ^
tidy14-bug/main.cpp:11:30: note: move occurred here
    return test{}.foo(v).bar(std::move(v));
                             ^
tidy14-bug/main.cpp:11:23: note: the use and move are unsequenced, i.e. there is no guarantee about the order in which they are evaluated
    return test{}.foo(v).bar(std::move(v));
                      ^

The standard, however, clearly defines the behavior for the case.
This false-positive breaks a lot of code in our codebase.

@georgthegreat
Copy link
Contributor Author

It turns out that the exaple above is broken even with clang-tidy12.

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 15, 2022

@llvm/issue-subscribers-clang-tidy

@EugeneZelenko
Copy link
Contributor

Could you please try 15 or main branch? https://godbolt.org should be helpful.

@georgthegreat
Copy link
Contributor Author

georgthegreat commented Sep 15, 2022

It would be hard to import entire clang15 toolchain (that is, llvm - clang - clang-tools-extra) into our infrastructure.
Could you, please, point to a commit or a set of commits fixing the problem?

@georgthegreat
Copy link
Contributor Author

I can not find an option to run clang-tidy via godbolt.
It only supports compilation, and my problem is not related with compilation.

@EugeneZelenko
Copy link
Contributor

Add tool button allow to do this.

@georgthegreat
Copy link
Contributor Author

clang15 still have this problem:
https://godbolt.org/z/31Prdv1dK

so does clang12

@georgthegreat
Copy link
Contributor Author

Corresponding StackOverflow discussion is here.

@JonasToth
Copy link
Member

the check predates c++17 so very likely the guarantee from c++-17 is not considered in the implementation.

@georgthegreat
Copy link
Contributor Author

clang-tidy is capable of getting the standard version from cmdline.
This check, however, does not take the standard into account (I am able to reproduce the problem with -std=c++20).

@EugeneZelenko EugeneZelenko added the false-positive Warning fires when it should not label Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-tidy false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

4 participants