Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Clang-Tidy, part 1: Modernize your source code using C++11/C++14

Automated refactoring of your source code using powerful open-source tooling

struct Base {
    virtual void reimplementMe(int a) {}
};
struct Derived : public Base  {
    virtual void reimplementMe(int a) {}
};

Clang-Tidy is a tool developed and maintained by the Clang/LLVM community.

% sudo apt-get install clang-tidy
% clang-tidy test.cpp -- -Imy_project/include -DMY_DEFINES ...
$ clang-tidy --list-checks -checks='*' | grep "modernize"
    modernize-avoid-bind
    modernize-deprecated-headers
    modernize-loop-convert
    modernize-make-shared
    modernize-make-unique
    modernize-pass-by-value
    modernize-raw-string-literal
    modernize-redundant-void-arg
    modernize-replace-auto-ptr
    modernize-shrink-to-fit
    modernize-use-auto
    modernize-use-bool-literals
    modernize-use-default
    modernize-use-emplace
    modernize-use-nullptr
    modernize-use-override
    modernize-use-using
struct Base {
    virtual void reimplementMe(int a) {}
};
struct Derived : public Base  {
    virtual void reimplementMe(int a) {}
};
% clang-tidy-3.9 -checks='modernize-use-override' test.cpp -- -std=c++11
1 warning generated.
/home/kfunk/test.cpp:5:18: warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override]
    virtual void reimplementMe(int a) {}
                 ^
                                      override
% clang-tidy-3.9 -checks='modernize-use-override' -fix test.cpp -- -std=c++11
1 warning generated.
/home/kfunk/test.cpp:5:18: warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override]
    virtual void reimplementMe(int a) {}
                 ^
                                      override
/home/kfunk/test.cpp:5:5: note: FIX-IT applied suggested code changes
    virtual void reimplementMe(int a) {}
    ^
/home/kfunk/test.cpp:5:38: note: FIX-IT applied suggested code changes
    virtual void reimplementMe(int a) {}
                                     ^
clang-tidy applied 2 of 2 suggested fixes.
% cd my-cmake-based-project
% mkdir build
% cd build
% cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
% run-clang-tidy.py
% run-clang-tidy.py -header-filter='.*' -checks='-*,modernize-use-override' -fix
% run-clang-tidy.py -header-filter='.*' -checks='-*,modernize-use-nullptr' -fix

Need help?

Contact Us


19 Comments

17 - Mar - 2017

Mats Taraldsvik

17 - Mar - 2017

Kevin Funk

17 - Mar - 2017

Mats Taraldsvik

17 - Mar - 2017

jani mikkonen

12 - Apr - 2017

MHM

8 - Oct - 2017

Daniel Marjamäki

1 - Aug - 2017

Mateusz Loskot

17 - Mar - 2017

Florent Castelli

30 - Mar - 2017

Markus Franke

30 - Mar - 2017

Markus Franke

30 - Mar - 2017

Kevin Funk

13 - Apr - 2017

Asgs

13 - Apr - 2017

Kevin Funk

% clang-tidy-3.9 --list-checks | wc -l
69
% clang-tidy-3.9 --list-checks -checks="*" | wc -l
226

14 - Apr - 2017

Jatin Bhateja

9 - Jun - 2017

Gargola

Replace 48:3-48:84 with "using RealType = typename itk::NumericTraits::RealType" Replace 48:3-48:84 with "using RealType = typename itk::NumericTraits::RealType"

7 - Aug - 2019

Iyappan Gunasekaran

3 - Nov - 2020

George

9 - Aug - 2022

Anandraj

10 - Aug - 2022

Kevin Funk