Adds a check to clang-tidy to ensure that some (absl::Status by default) non-trivially-destructible objects are used.
Example code that should trigger a warning:
{ absl::Status status = call_some_function(); }
Differential D138583
Create unused non-trivially-destructible check in clang-tidy ankineri on Nov 23 2022, 8:43 AM. Authored by
Details
Diff Detail
Event TimelineComment Actions Clang already has a warning -Wunused-variable that is designed for this specific purpose. So unless this is bringing anything enhanced functionality I don't see the need for this check. Comment Actions -Wunused-variable does not detect unused non-trivially-destructible objects because they may be used for RAII, i.e. { scoped_lock lock(&global_mutex); critical_section(); } lock here is not an unused variable because its destructor side-effects are its usage. However if replaced with say absl::Status or even std::string it will be more or less obvious that the variable is unused, but the warning will still not be triggered (as both absl::Status and std::string have nontrivial destructors). This is where this checks is useful: for non-trivially-destructible types known not to be used in RAII. Comment Actions Extending --Wunused-variable to other types seems preferable in a few ways:
In fact it looks like an attribute for this already exists: it can be spelled __attribute__((warn_unused)) or [[gnu::warn_unused]] struct __attribute__((warn_unused)) S { ~S(); }; void foo() { S s; } $ clang -fsyntax-only ~/test.cc -Wall /usr/local/google/home/sammccall/test.cc:4:5: warning: unused variable 's' [-Wunused-variable] S s; Comment Actions I do agree that having it as part of -Wunused-variable is better on many aspects, but I have a few concerns about it. In other words, this check is:
Comment Actions My guess is that libc++ and libstdc++ would accept such a patch *where it's safe*, since clang and gcc respectively support the attribute, but you'd have to check with them. Since the standard library is mostly templates, one issue you're going to run into with say std::vector<int> is that it's only warn_unused because int is warn_unused, and this is hard to express (with attributes or otherwise). Fundamentally this is all stuff that such a clang-tidy check would have to address as well, and IMO the tools you have to address it with clang + attributes are much better than in a clang-tidy check. |