fabiangreffrath / taradino

SDL2 port of Rise of the Triad
GNU General Public License v2.0
35 stars 9 forks source link

Consider using clang-tidy at some point #30

Open malespiaut opened 1 year ago

malespiaut commented 1 year ago

As for another bold proposal, clang-tidy is an intersting tool to automate a large number of code modification to increade quality.

That includes initialising variables, and putting brackets for one line ifs (a change to consider regarding some confusing parts of the source code).

If you haven't used clang-tidy before, I highly encourage you to give it a try on small pieces of example code. I think that it can play an essential part for this project.

fabiangreffrath commented 1 year ago

So, what's the difference to clang-format then?

malespiaut commented 1 year ago

To summarize, clang-format only reformats the code by only changing whitespaces and newlines.

clang-tidy will modify the code according to certain rules.

For instance, clang-tidy have a rule called cppcoreguidelines-init-variables with initialize all variables to 0.

It'll change this code:

void function() {
  int x;
  char *txt;
  double d;

  // Rest of the function.
}

to this

#include <math.h>

void function() {
  int x = 0;
  char *txt = nullptr;
  double d = NAN;

  // Rest of the function.
}

See https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/init-variables.html for more details.

There's also the rule readability-braces-around-statements which, as the name implies, adds braces around statements, for readability.

It'll change this code:

if (condition)
  statement;

to this

if (condition) {
  statement;
}

See https://releases.llvm.org/11.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/readability-braces-around-statements.html for more details.