-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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] bugprone-unchecked-optional-access
false positive when analyzing optional field of object accessed via chain of getters
#126283
Comments
@llvm/issue-subscribers-clang-tidy Author: Valentyn Yukhymenko (BaLiKfromUA)
### Problem
The next code snippet: #include <optional>
#include <string>
#include <iostream>
class A {
private:
std::optional<std::string> d_field;
public:
const std::optional<std::string>& field() const {
return d_field;
}
};
class B {
private:
A d_a;
public:
const A& a() const {
return d_a;
}
};
void foo(const B& data) {
if (data.a().field().has_value()) {
std::cout << data.a().field().value();
}
} produces a false positive warning from clang-tidy:
The next workaround could solve this issue: void bar(const B& data) {
const std::optional<std::string>& field = data.a().field();
if (field.has_value()) {
std::cout << field.value();
}
} But it is easy to forget it, especially if such pattern is frequent in the codebase. Similar issuesI've checked existing open issues and one is similar-ish to mine -- #93194 Seems like it is fixed now and I cannot reproduce it on my side. I assume that the bug is connected to the "chaining" of calls. I am happy to contribute the fix to this issue if someone could give me initial suggestions about a proper solution and possible places to look :) |
Found a connected issue which has been solved recently -- #58510 @jvoung sorry for pinging, but could you please take a look at my issue when you have time? Is it something that makes sense to fix as well? I believe that we could use the approach introduced in #112605 but we also need somehow to track the chain of const getters calls. Thank you in advance! |
Hi, yes it may make sense to fix as well. I believe the difference between your example and the other tested cases is that For now, we had only cached if it is a
Perhaps reference to "anything" is similar enough to pointer to anything, that we could expand. However, we probably shouldn't allow copy of anything. At least, it's a bit wasteful to be repeating a chain involving copies. |
Thank you! I will try to extend your PR to see how it works! Yeah, I was not planning to cache copies. I am interested only in const references. |
@jvoung, I created a small PR, which passed my tests. Would appreciate some initial feedback! |
Problem
The next code snippet:
produces a false positive warning from clang-tidy:
The next workaround could solve this issue:
But it is easy to forget it, especially if such pattern is frequent in the codebase.
Compiler explorer link
Similar issues
I've checked existing open issues and one is similar-ish to mine -- #93194
Seems like it is fixed now and I cannot reproduce it on my side. I assume that my bug is connected to the "chaining" of calls.
Compiler explorer link
I am happy to contribute the fix to this issue if someone could give me initial suggestions about a proper solution and possible places to look :)
The text was updated successfully, but these errors were encountered: