llvm / llvm-project

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

clang-tidy feature request: C++ Core Guidelines "ES.23: Prefer the `{}`-initializer syntax" (avoid `()` initialization) #61454

Open N-Dekker opened 1 year ago

N-Dekker commented 1 year ago

C++ Core Guidelines, September 23, 2022, item ES.23: Prefer the {}-initializer syntax says:

Prefer {}. The rules for {} initialization are simpler, more general, less ambiguous, and safer than for other forms of initialization.

Use = only when you are sure that there can be no narrowing conversions. For built-in arithmetic types, use = only with auto.

Avoid () initialization, which allows parsing ambiguities.

So for example, when declaring an m x n matrix object (assuming a Matrix::Matrix(int m, int n) constructor):

Matrix oldStyleMatrix(m, n);  // Old style: initialization using parentheses
Matrix newStyleMatrix{m, n};  // New style: initialization using curly braces.

The new style (using curly braces) appears preferable, according to this guideline. Would it be possible to add a clang-tidy check, to warn against using the old style, as well as the ability to convert code to the new style (using the option -fix)?

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-tidy

samacumen commented 5 months ago

Are C++ core guidelines fully supported in all of clang tools? I see it is with clang-tidy and clang-format. Any other reference would help.

JonasToth commented 2 weeks ago

Are C++ core guidelines fully supported in all of clang tools? I see it is with clang-tidy and clang-format. Any other reference would help.

No. The cppcoreguidelines are not fully supported. There is no "global preset" or anything as an best-effort available either. clang-tidy has its module and probably a few checks of other modules address issues of the coreguidelines as well (they are often aliased into the cppcoreguidelines module too).