Skip to content

[clang-tidy] Disable bugprone-multi-level-pointer-conversion in C code #141209

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

carlosgalvezp
Copy link
Contributor

It is deemed more of an annoyance than a help, since the patterns the check warns about are quite common and idiomatic in C, and there are no good alternatives.

Thus, enable the check only for C++, where there are safer constructs.

Fixes #140659

It is deemed more of an annoyance than a help, since the patterns the
check warns about are quite common and idiomatic in C, and there are no
good alternatives.

Thus, enable the check only for C++, where there are safer constructs.

Fixes llvm#140659
@llvmbot
Copy link
Member

llvmbot commented May 23, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Carlos Galvez (carlosgalvezp)

Changes

It is deemed more of an annoyance than a help, since the patterns the check warns about are quite common and idiomatic in C, and there are no good alternatives.

Thus, enable the check only for C++, where there are safer constructs.

Fixes #140659


Full diff: https://github.com/llvm/llvm-project/pull/141209.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h (+3)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst (+9)
diff --git a/clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h b/clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h
index 13228145ff35f..c502f31d867d2 100644
--- a/clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h
@@ -26,6 +26,9 @@ class MultiLevelImplicitPointerConversionCheck : public ClangTidyCheck {
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
   std::optional<TraversalKind> getCheckTraversalKind() const override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+    return LangOpts.CPlusPlus;
+  }
 };
 
 } // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 9b29ab12e1bfa..7e76d81eed439 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -148,6 +148,10 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Disabled :doc:`bugprone-multi-level-implicit-pointer-conversion
+  <clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion>` check in
+  C code. The check is now only active in C++ code.
+
 - Improved :doc:`bugprone-optional-value-conversion
   <clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
   conversion in argument of ``std::make_optional``.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst
index e6dc5c13aa025..a226654c9ab75 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst
@@ -42,3 +42,12 @@ Additionally, it is recommended that developers thoroughly check and verify the
 safety of the conversion before using an explicit cast. This extra level of
 caution can help catch potential issues early on in the development process,
 improving the overall reliability and maintainability of the code.
+
+.. note::
+
+  This check is enabled only for C++ code, not for C code. The motivation is the
+  type of conversions that this check warns about are fairly common and
+  idiomatic in C, and there are no safer alternatives. This introduces quite
+  some friction in the form of suppressions or casts. The check remains active
+  in C++ to more easily detect C-style patterns that can potentially be
+  refactored to use safer C++ constructs.

@llvmbot
Copy link
Member

llvmbot commented May 23, 2025

@llvm/pr-subscribers-clang-tidy

Author: Carlos Galvez (carlosgalvezp)

Changes

It is deemed more of an annoyance than a help, since the patterns the check warns about are quite common and idiomatic in C, and there are no good alternatives.

Thus, enable the check only for C++, where there are safer constructs.

Fixes #140659


Full diff: https://github.com/llvm/llvm-project/pull/141209.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h (+3)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst (+9)
diff --git a/clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h b/clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h
index 13228145ff35f..c502f31d867d2 100644
--- a/clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.h
@@ -26,6 +26,9 @@ class MultiLevelImplicitPointerConversionCheck : public ClangTidyCheck {
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
   std::optional<TraversalKind> getCheckTraversalKind() const override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+    return LangOpts.CPlusPlus;
+  }
 };
 
 } // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 9b29ab12e1bfa..7e76d81eed439 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -148,6 +148,10 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Disabled :doc:`bugprone-multi-level-implicit-pointer-conversion
+  <clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion>` check in
+  C code. The check is now only active in C++ code.
+
 - Improved :doc:`bugprone-optional-value-conversion
   <clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
   conversion in argument of ``std::make_optional``.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst
index e6dc5c13aa025..a226654c9ab75 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion.rst
@@ -42,3 +42,12 @@ Additionally, it is recommended that developers thoroughly check and verify the
 safety of the conversion before using an explicit cast. This extra level of
 caution can help catch potential issues early on in the development process,
 improving the overall reliability and maintainability of the code.
+
+.. note::
+
+  This check is enabled only for C++ code, not for C code. The motivation is the
+  type of conversions that this check warns about are fairly common and
+  idiomatic in C, and there are no safer alternatives. This introduces quite
+  some friction in the form of suppressions or casts. The check remains active
+  in C++ to more easily detect C-style patterns that can potentially be
+  refactored to use safer C++ constructs.

@carlosgalvezp carlosgalvezp requested a review from PiotrZSL May 23, 2025 07:49
Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. This check is about IMPLICIT casts, you can always do explicit cast in C (via functional cast or c-style cast) or C++ (reinterpret_cast) to say that mutli-level pointer conversion is expected in that place.

If some C project got lot of such issues that they don't want to fix, then probably they should just disable a check.

In such case I don't see a reason to limit check to C++ only always.
It still could be done via some check option.

@carlosgalvezp
Copy link
Contributor Author

You can check the discussion in the linked issue. The argument is that these casts are idiomatic in C, and does not make sense to pollute the code with casts. There's no other safer alternative either.

then probably they should just disable a check

There doesn't appear to be an actual true positive in C where the check is useful as per the discussion.

@PiotrZSL
Copy link
Member

Ok, but that is for "some projects". If you search internet you may find examples when people omit those casts and were people add those casts.
For example: https://www.tutorialspoint.com/c_standard_library/c_function_malloc.htm
And with multi level pointer conversion it's easy to make mistake later.

This is why I don't like disabling permanently check for C, as for some projects it could be hiding potential issues.
But adding option "EnableInC" and even set it to "false" by default would be fine, as it would still allow projects that want such conversions be explicit to enable it.

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm accepting change and leaving final decision to you.
I can always change this later in internal fork or add option later.

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.

clang-tidy: bugprone-multi-level-implicit-pointer-conversion should ignore malloc/calloc/realloc/free
3 participants