[clang-tools-extra] [clang-tidy] Don't run use-nullptr check on objective-c code. (PR #141229)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri May 23 05:27:43 PDT 2025


https://github.com/hokein updated https://github.com/llvm/llvm-project/pull/141229

>From 7d6ba7c08e260d75ad33c06f069898a528fe6159 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Fri, 23 May 2025 14:16:52 +0200
Subject: [PATCH] [clang-tidy] Don't run use-nullptr check on objective-c code.

If we compile an objective-c file with the `-std=c23` flag, this check
will warn the NULL usage, this patch fix it.

nullptr doesn't exist in objective-c.
---
 clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h   | 2 +-
 clang-tools-extra/docs/ReleaseNotes.rst                    | 4 ++++
 .../test/clang-tidy/checkers/modernize/use-nullptr-c23.m   | 7 +++++++
 3 files changed, 12 insertions(+), 1 deletion(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-c23.m

diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h
index f1591bae44657..ee380d03064f9 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h
@@ -19,7 +19,7 @@ class UseNullptrCheck : public ClangTidyCheck {
   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
     // FIXME this should be CPlusPlus11 but that causes test cases to
     // erroneously fail.
-    return LangOpts.CPlusPlus || LangOpts.C23;
+    return LangOpts.CPlusPlus || (LangOpts.C23 && !LangOpts.ObjC);
   }
   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 579fca54924d5..70b519ed0d40b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -219,6 +219,10 @@ Changes in existing checks
   tolerating fix-it breaking compilation when functions is used as pointers
   to avoid matching usage of functions within the current compilation unit.
 
+- Improved :doc:`modernize-use-nullptr
+  <clang-tidy/checks/modernize/use-nullptr>` check to not run on
+  objective-c code.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-c23.m b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-c23.m
new file mode 100644
index 0000000000000..1d6446c486317
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-c23.m
@@ -0,0 +1,7 @@
+// RUN: %check_clang_tidy %s modernize-use-nullptr %t -- -- -std=c23
+
+#define NULL 0
+
+void test_assignment() {
+  int *p1 = NULL;
+}



More information about the cfe-commits mailing list