llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.14k stars 12.02k forks source link

Create check to prefer wxEmptyString over wxT("") #27674

Open LegalizeAdulthood opened 8 years ago

LegalizeAdulthood commented 8 years ago
Bugzilla Link 27300
Version unspecified
OS All
CC @EugeneZelenko

Extended Description

wxWidgets has a preconstructed empty string object wxEmptyString of type wxString.

This should be preferred over wxT("") as this constructs an anonymously named temporary of type wxString wherever wxEmptyString could be used instead.

Particularly for default values of functions taking references to const wxString, this can save creating and destroying anonymous temporaries for every function call.

There are probably other wxWidgets idioms that make sense for clang-tidy.

This check should probably go in a wxWidgets module.

LegalizeAdulthood commented 8 years ago

Oops, ignore my last comment, it was intended for another bug.

LegalizeAdulthood commented 8 years ago

A similar check could be created for std::string::operator== vs std::string::compare

LegalizeAdulthood commented 8 years ago

This isn't the same thing, because we are checking for more things than just constructing an object.

EugeneZelenko commented 8 years ago

Clang-tidy already has readability-redundant-string-init check which work for STL strings. Will be good idea to expand it instead iof introducing new one.

firewave commented 1 year ago

This is a general issue with std::string. There are cases where using a per-constructed object is faster and others where it isn't. See https://github.com/danmar/cppcheck/pull/4034 where I apply a few such changes to make the Cppcheck code base consistent.

There's also #58002, #60165, https://github.com/danmar/cppcheck/pull/4337 (and most likely more) where I try (often awkwardly) to dig into sub-optimal/inconsistent code being for pattern which appear to be the same.

As I have haven't had much experience with c++17 and do not work on a code base which actively uses it I did not look into how std::string_view would play into this.