Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clang-tidy plugin to convert implicit conversions to explicit ones #4663

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

mikecrowe
Copy link

Add a clang-tidy plugin containing one check to replace implicit conversions (as enabled by default with JSON_USE_IMPLICIT_CONVERSIONS) with explicit ones. This will make it easier for library users to switch away from using implicit conversions which should make it possible for the library to start disallowing them sooner.

The skeleton of the check was generated by the clang-tidy add_new_check.py script. It added headers indicating that the files were under the LLVM Apache-2.0-with-LLVM-exception licence. I've replaced these with the MIT licence to match the rest of the library but would be happy to change them back.

The check was written using LLVM's clang-format style. I can modify that if you'd prefer.

The check_clang_tidy.py script was copied in its entirety from the LLVM repository so it must keep its existing Apache-2.0-with-LLVM-exception licence.

The test file is written in the style of clang-tidy's own tests. This means that it cannot include system headers. We might be able to relax that for a plugin so that the actual single_include header could be used. That might make cross-platform CI harder though and I haven't looked into doing so.

I know very little about CMake or GitHub workflows. What I have implemented appears to work, but I don't know whether both work the way you would like. I have a Linux and Debian bias.

A reviewer of the original check asked about implicit conversions to std::wstring and std::string_view. I think that neither of those are possible. Is that true?

  • The changes are described in detail, both the what and why. In individual commit messages
  • If applicable, an existing issue is referenced. N/A
  • The Code coverage remained at 100%. A test case for every new line of code. It looks like I need to create the pull request before I see the results of this
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running make amalgamate. N/A

Add a clang-tidy plugin containing one check to replace implicit
conversions (as enabled by default with JSON_USE_IMPLICIT_CONVERSIONS)
with explicit ones. This will make it easier for library users to switch
away from using implicit conversions which should make it possible for
the library to start disallowing them sooner.

Being able to test the plugin in a similar way to how checks are tested
within clang-tidy itself requires copying the check_clang_tidy.py script
from the LLVM code itself.

The check itself is virtually identical to the one proposed for
inclusion in clang-tidy itself at
llvm/llvm-project#126425 .

Unfortunately it is necessary to add "C" to the languages for the
project in CMakeLists.txt for find_package to work for LLVM.

Signed-off-by: Mike Crowe <mac@mcrowe.com>
The purpose of these tests is to ensure that anyone attempting to
compile the clang-tidy plugin on the tested distributions will be able
to and that the resulting plugin will work. Clang-tidy plugins were only
introduced with Clang 16, so we can't test with any earlier versions
than that.

Signed-off-by: Mike Crowe <mac@mcrowe.com>
Signed-off-by: Mike Crowe <mac@mcrowe.com>
@coveralls
Copy link

Coverage Status

coverage: 99.186%. remained the same
when pulling 4c16dd2 on mikecrowe:mac/clang-tidy-plugin
into 8215dba on nlohmann:develop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants