You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
structFoo { Foo() {} ~Foo() {} };
voidtest() {
Foo{}; // clang-tidy: warning: object destroyed immediately after creation; did you mean to name the object? [bugprone-unused-raii]
}
This happens for the very common expressions-statements of the form pybind11::class_<...>(...);. The finding does not appear once the value is used (either to initialize a variable or if member functions, such as def, are called on it.
A workaround could be to add an explicit discarding cast-to-void, but that seems quite ugly and hard to justify.
I would like to propose an alternative solution. This one is inspired by absl::Status, which is marked "nodiscard", and it offers a no-op member function IgnoreError so that you can spell f().IgnoreError(); to avoid the warning about not using the return value.
Similarly, we could add some no-op member function to class_ that one could spell to avoid the "immediately destroyed" finding. E.g. if it was hypothetically called end_of_def, we could spell:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
ClangTidy has a finding https://clang.llvm.org/extra/clang-tidy/checks/bugprone/unused-raii.html which fires on an expression whose type has a non-trivial destructor if the expression is discarded:
This happens for the very common expressions-statements of the form
pybind11::class_<...>(...);. The finding does not appear once the value is used (either to initialize a variable or if member functions, such asdef, are called on it.A workaround could be to add an explicit discarding cast-to-
void, but that seems quite ugly and hard to justify.I would like to propose an alternative solution. This one is inspired by
absl::Status, which is marked "nodiscard", and it offers a no-op member functionIgnoreErrorso that you can spellf().IgnoreError();to avoid the warning about not using the return value.Similarly, we could add some no-op member function to
class_that one could spell to avoid the "immediately destroyed" finding. E.g. if it was hypothetically calledend_of_def, we could spell:pybind11::class_<T>(m, "T").end_of_def();Would you at all be amenable to such an addition
Beta Was this translation helpful? Give feedback.
All reactions