devops.cpp.ast.checks.no_delete_in_gtest_teardown

Flag delete/delete[] expressions inside a GTest teardown function.

A member function named TearDown, TearDownTestSuite, or TearDownTestCase — the fixture-teardown hooks GTest calls by name (TearDown() after every test, the other two once per test suite) — is reported for any delete/delete[] expression appearing anywhere in its body. Manually deleting a raw pointer there is exactly the kind of lifetime bookkeeping RAII/smart pointers exist to make unnecessary, and it’s especially fragile in a teardown hook: if SetUp() throws or returns early, the matching delete in TearDown() never runs, and if a test body already frees the pointer, the TearDown() delete double-frees it. Prefer std::unique_ptr/std::shared_ptr (or another RAII owner) for anything the fixture needs to release, so its lifetime doesn’t depend on a teardown hook running at all.

Only a literal delete/delete[] lexically inside the teardown function’s own body is flagged — a call to some other function that itself deletes something is not traced into.

To disable this check for a project set:

[cpp]
ast_check_disabled_ids = ["noDeleteInGtestTeardown"]

Classes

NoDeleteInGtestTeardown()

Flag delete/delete[] expressions inside a GTest teardown function.

class devops.cpp.ast.checks.no_delete_in_gtest_teardown.NoDeleteInGtestTeardown[source]

Bases: Check

Flag delete/delete[] expressions inside a GTest teardown function.

id: str = 'noDeleteInGtestTeardown'

unique id, used as the cppcheck-style [id] suffix

visit(cursor, filename)[source]

Flag any delete-expression inside a GTest teardown function body.

Parameters:
  • cursor (clang.Cursor) – The AST node currently being visited.

  • filename (str) – Path of the file being checked.

Returns:

list[Diagnostic] – One diagnostic per delete/delete[] expression found inside cursor’s body, if cursor is a GTest teardown function; otherwise an empty list.

Return type:

list[Diagnostic]