Skip to content

clang-tidy check request: a new expression that is implicitly converted to bool #64461

Open
@chandlerc

Description

@chandlerc

Consider the following code:

struct S {};

void f(bool flag);

void g() {
  // Check should fire here:
  f(new S());
}

This new expression seems clearly a bug. This feels like a case clang-tidy could error on easily, similar to other bugprone checks. It might even make sense as a warning. The only use case I can imagine where this isn't a bug due to leaking memory is for types that register themselves in some collection when heap allocated, so the pointer produced by new isn't actually needed and can be used as a bool. But it is always true, and so it seems much more clear to write:

void g() {
  (void)new S();
  f(true);
}

And not get a warning. This seems to more clearly show that the result of the new expression is intentionally discarded.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions