The unchecked-optional-access check identifies attempted value
unwrapping without checking if the value exists. These changes extend
that support to checking folly::Optional.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
To be honest, all those things just should be configurable instead of hardcoding them.
For example, a project that I work for got 4 different custom optional implementations, and this check is useless for all of them.
And still no boost::optional support.
Thank you! Do you have commit access or do you need me to commit this patch for you?
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp | ||
---|---|---|
845 |
This should be a configuration option, we should not hardcore project-specific things in the source code.
I agree, but we already are hardcoding specific types -- I think this is a separate (and valid) critique of the design. I'd propose filing an issue on the github tracker and we can follow up there. I, for one, would love to review such a change but don't have the time to write it.
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp | ||
---|---|---|
846–848 | A few concerns:
|
Is moving these values to config an appropriate task for somebody like me new to working on clang-tidy? I'd be happy to merge this and then try the transition to a config assuming there's some similar examples I can borrow from elsewhere in the codebase.
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp | ||
---|---|---|
846–848 | Sure. I can make that change. |
This is one of the most complex clang-tidy checks. So, if you're looking for a CT starter task, I wouldn't recommend this particular challenge. That said, I think the clang-tidy side will be relatively easy -- CT has a mature config system/API. The harder part (and not CT relevant) is refactoring this code to consume that config. It's not terribly complicated but will require a bunch of changes and probably some design questions.
I think it can be a good starter task for a new engineer on the project. However, don't underestimate this problem, it will require the code to be refactored a little bit. For example, the function hasOptionalClassName needs restructuring so that it can accept class names from a list. Not a lot of work, but it isn't mechanically replacing string literals with a variable either.
Indeed this is not "the standard" CT check, the core is part of Clang so I think it'd be good to add reviewers there as well in case this affects other parts of the codebase. In that sense it does not seen as trivial as I thought to make this user configurable, so perhaps opening a ticket and solve it there is a faster way forward.
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp | ||
---|---|---|
62 | If there's no need for absl here, why do we need to add folly? |
I think it'd be good to add reviewers there
I realize the CodeOwners for Analysis are already in the list of reviewers, I won't interfere then :)
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp | ||
---|---|---|
62 | This is the branch for RD.getName() == "Optional" with a capital "O" because base::Optional, folly;:Optional are spelled with a capital "O". Abseil provides absl::optional and it is handled under RD.getName() == "optional" together with std::optional above. |
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp | ||
---|---|---|
62 | Got it, thanks! |
I have opened a refactoring ticket here: https://github.com/llvm/llvm-project/issues/64037
The review is marked as accepted, should we land it? Let me know if you need help with that :)
We're still waiting on some small changes: https://reviews.llvm.org/D155890#inline-1508661
Update calls to OptionalMemberCall to take a name matcher
Supports treating calls to optional::has_value and optional::hasValue identically
Thanks for all the comments! I think I've got them all addressed and tests are passing locally.
Yes, will need help with that since I don't have commit access. Anybody with commit access, feel free to land this for me.
clang-tools-extra/docs/clang-tidy/checks/bugprone/unchecked-optional-access.rst | ||
---|---|---|
10–11 | 80 characters limit, wrap it |
80 characters limit, wrap it