MRtrix3 / mrtrix3

MRtrix3 provides a set of tools to perform various advanced diffusion MRI analyses, including constrained spherical deconvolution (CSD), probabilistic tractography, track-density imaging, and apparent fibre density
http://www.mrtrix.org
Mozilla Public License 2.0
295 stars 182 forks source link

Universal revisions based on clang-tidy #2828

Open Lestropie opened 9 months ago

Lestropie commented 9 months ago

Since #2745, clang-tidy was introduced. It is run only on proposed changes within PRs, not across the whole code base, since the latter would be overly burdensome. We can however elect to take any recommendations that it makes based on isolated code, and apply the same changes across the code base, eg. in cases where instances of such can be found through a regex search or where it's known that a particular design pattern is used in a finite number of places. This issue will serve to facilitate listing clang-tidy proposals that may be worthwhile revising the code base. Hopefully it will doubly serve to show clang-tidy proposals that we are aware of but may not necessarily resolve in any unrelated PR, so that those less experienced will not panic if they see them.


daljit46 commented 9 months ago

readability-container-size-empty can be applied using clang-tidy -fix, but the other two would too risky to change using automation. In particular the first would most likely need manual supervision on all instances of const char* as we would need to think about the lifetimes of strings. I'm in favour of this though, so I think a gradual adoption of std::string and perhaps std::string_view would be ideal. Perhaps, we could start doing this with all application level code (i.e. all code in cmd).

Lestropie commented 9 months ago

I wouldn't want to accept the automated changes for readability-container-size-empty either; it suggests changing: value = opt.size() ? A : B; to: value = !opt.empty() ? A : B; , whereas preferable would be: value = opt.empty() ? B : A;

Edit: Wouldn't want to blindly accept the automated change; happy to manually revise #2829 if necessary.

Lestropie commented 9 months ago

On #2702 I'm currently getting a lot of bugprone-narrowing-conversions going from unsigned to signed array indices. clang-tidy seems to be even more aggressive in this respect than the current state of the compilers (which themselves have progressively flagged more code in this respect). Not sure if this is one we might actually want to disable in clang-tidy, as it could produce a lot of noise resulting in people ignoring the meaningful suggestions.

daljit46 commented 9 months ago

I agree on that example.

Edit: Wouldn't want to blindly accept the automated change; happy to manually revise https://github.com/MRtrix3/mrtrix3/pull/2829 if necessary.

That'd be great!

On https://github.com/MRtrix3/mrtrix3/pull/2702 I'm currently getting a lot of bugprone-narrowing-conversions going from unsigned to signed array indices.

Although I understand that this check is noisy, my opinion is that we should make an effort to follow its advice. If a conversion is benign (which often can be a deceiving matter), an explicit cast (preferably static_cast or dynamic_cast) would be best. This tells the reader that the author is intentionally allowing the conversion.

Lestropie commented 9 months ago

Something to think about as #2829 hopefully gets merged:

It might be the case that, for those clang-tidy checks that we do decide to adopt across the repository, we choose to add a CI check that runs clang-tidy -fix just for those specific features, which result in CI failure rather than a bot comment. It would appear alongside any clang-format errors, so it's only kind of a half developer step addition.

Where I'm not sure if this would work is in precommit hooks. It often takes a long time for clang-tidy Action results to come in, so there's a chance it might be more computationally intensive to run at commit time than clang-format?

Lestropie commented 6 months ago

Added a few to the list in reviewing #2877.