json/clang_tidy_plugin/ModernizeNlohmannJsonExplicitConversionsCheck.h
Mike Crowe 4eda97d6bb Add clang-tidy plugin for modernize-nlohmann-json-explicit-conversions
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
https://github.com/llvm/llvm-project/pull/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>
2025-02-23 19:28:47 +00:00

32 lines
1.3 KiB
C++

// SPDX-FileCopyrightText: 2025 Mike Crowe
// SPDX-License-Identifier: MIT
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_NLOHMANNJSONEXPLICITCONVERSIONSCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_NLOHMANNJSONEXPLICITCONVERSIONSCHECK_H
#include "clang-tidy/ClangTidyCheck.h"
namespace clang::tidy::modernize {
/// Convert implicit conversions via operator ValueType() from nlohmann::json to
/// explicit calls to the get<> method because the next major version of the
/// library will remove support for implicit conversions.
///
/// For the user-facing documentation see:
/// https://clang.llvm.org/extra/clang-tidy/checks/modernize/nlohmann-json-explicit-conversions.html
class NlohmannJsonExplicitConversionsCheck : public ClangTidyCheck {
public:
NlohmannJsonExplicitConversionsCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus;
}
};
} // namespace clang::tidy::modernize
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_NLOHMANNJSONEXPLICITCONVERSIONSCHECK_H