Skip to content

Conversation

@rniwa
Copy link
Contributor

@rniwa rniwa commented Dec 11, 2025

Don't emit a warning when a variable declaration appears within the "if" condition and if its "then" statement is trivial.

…h trivial "then"

Don't emit a warning when a variable declaration appears within the "if" condition
and if its "then" statement is trivial.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Dec 11, 2025
@rniwa rniwa requested a review from t-rasmud December 11, 2025 05:30
@llvmbot
Copy link
Member

llvmbot commented Dec 11, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Ryosuke Niwa (rniwa)

Changes

Don't emit a warning when a variable declaration appears within the "if" condition and if its "then" statement is trivial.


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

4 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp (+4)
  • (modified) clang/test/Analysis/Checkers/WebKit/local-vars-checked-const-member.cpp (+1-3)
  • (modified) clang/test/Analysis/Checkers/WebKit/local-vars-counted-const-member.cpp (+1-3)
  • (modified) clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp (+39-1)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp
index f2235e7c25ab2..404ec0783dd64 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp
@@ -234,6 +234,10 @@ class RawPtrRefLocalVarsChecker
       }
 
       bool TraverseIfStmt(IfStmt *IS) override {
+        if (IS->getConditionVariable()) {
+          if (auto *Then = IS->getThen(); !Then || TFA.isTrivial(Then))
+            return true;
+        }
         if (!TFA.isTrivial(IS))
           return DynamicRecursiveASTVisitor::TraverseIfStmt(IS);
         return true;
diff --git a/clang/test/Analysis/Checkers/WebKit/local-vars-checked-const-member.cpp b/clang/test/Analysis/Checkers/WebKit/local-vars-checked-const-member.cpp
index be04cf26be2e8..bf58e676f3bb0 100644
--- a/clang/test/Analysis/Checkers/WebKit/local-vars-checked-const-member.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/local-vars-checked-const-member.cpp
@@ -21,10 +21,8 @@ class Foo {
   CheckedObj& ensureObj4() {
     if (!m_obj4)
       const_cast<CheckedPtr<CheckedObj>&>(m_obj4) = new CheckedObj;
-    if (auto* next = m_obj4->next()) {
-      // expected-warning@-1{{Local variable 'next' is unchecked and unsafe [alpha.webkit.UncheckedLocalVarsChecker]}}
+    if (auto* next = m_obj4->next())
       return *next;
-    }
     return *m_obj4;
   }
 
diff --git a/clang/test/Analysis/Checkers/WebKit/local-vars-counted-const-member.cpp b/clang/test/Analysis/Checkers/WebKit/local-vars-counted-const-member.cpp
index e12c9b900a423..5f35535bde3aa 100644
--- a/clang/test/Analysis/Checkers/WebKit/local-vars-counted-const-member.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/local-vars-counted-const-member.cpp
@@ -21,10 +21,8 @@ class Foo {
   RefCountable& ensureObj4() {
     if (!m_obj4)
       const_cast<RefPtr<RefCountable>&>(m_obj4) = RefCountable::create();
-    if (auto* next = m_obj4->next()) {
-      // expected-warning@-1{{Local variable 'next' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
+    if (auto* next = m_obj4->next())
       return *next;
-    }
     return *m_obj4;
   }
 
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
index 3364637369799..bf093dcf3fdd3 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
@@ -492,4 +492,42 @@ namespace virtual_function {
     auto* baz = &*obj;
     // expected-warning@-1{{Local variable 'baz' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
   }
-}
\ No newline at end of file
+}
+
+namespace vardecl_in_if_condition {
+  RefCountable* provide();
+
+  RefCountable* get() {
+    if (auto* obj = provide())
+      return obj; // no warning
+    return nullptr;
+  }
+
+  RefCountable* get_non_trivial_then() {
+    if (auto* obj = provide()) // expected-warning{{Local variable 'obj' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
+      return obj->next();
+    return nullptr;
+  }
+
+  RefCountable* get_non_trivial_else() {
+    if (auto* obj = provide())
+      return obj;
+    else
+      return provide()->next();
+    return nullptr;
+  }
+
+  RefCountable& get_ref() {
+    if (auto* obj = provide())
+      return *obj; // no warning
+    static Ref<RefCountable> empty = RefCountable::create();
+    return empty.get();
+  }
+
+  RefCountable* get_non_trivial_condition() {
+    if (auto* obj = provide(); obj && obj->next())
+      return obj; // expected-warning@-1{{Local variable 'obj' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
+    return nullptr;
+  }
+
+}

@rniwa rniwa requested a review from steakhal December 11, 2025 05:30
@github-actions
Copy link

github-actions bot commented Dec 11, 2025

🪟 Windows x64 Test Results

  • 53024 tests passed
  • 2076 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Dec 11, 2025

🐧 Linux x64 Test Results

  • 111970 tests passed
  • 4512 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Dec 12, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@rniwa rniwa merged commit 6f1e3c3 into llvm:main Dec 12, 2025
10 checks passed
@rniwa rniwa deleted the fix-webkit-if-vardecl-trivial-then branch December 12, 2025 23:00
rniwa added a commit to rniwa/llvm-project that referenced this pull request Dec 13, 2025
…h trivial "then" (llvm#171764)

Don't emit a warning when a variable declaration appears within the "if"
condition and if its "then" statement is trivial.

(cherry picked from commit 6f1e3c3)
anonymouspc pushed a commit to anonymouspc/llvm that referenced this pull request Dec 15, 2025
…h trivial "then" (llvm#171764)

Don't emit a warning when a variable declaration appears within the "if"
condition and if its "then" statement is trivial.
rniwa added a commit to rniwa/llvm-project that referenced this pull request Dec 15, 2025
…h trivial "then" (llvm#171764)

Don't emit a warning when a variable declaration appears within the "if"
condition and if its "then" statement is trivial.

(cherry picked from commit 6f1e3c3)
rniwa added a commit to rniwa/WebKit that referenced this pull request Dec 15, 2025
https://bugs.webkit.org/show_bug.cgi?id=304203

Reviewed by NOBODY (OOPS!).

Remove the unnecessary suppressions for a variable declaration within "if" statement
now that the corresponding clang static analyzer fix has been merged and deployed:
llvm/llvm-project#171764

No new tests since there should be no behavioral changes.

* Source/WebCore/accessibility/AXCoreObject.cpp:
(WebCore::AXCoreObject::nextSiblingIncludingIgnoredOrParent const):
(WebCore::AXCoreObject::parentObjectUnignored const):
* Source/WebCore/accessibility/AXObjectCacheInlines.h:
(WebCore::AXObjectCache::getOrCreate):
* Source/WebCore/accessibility/AccessibilityRenderObject.cpp:
(WebCore::AccessibilityRenderObject::parentObject const):
* Source/WebCore/dom/EventPath.h:
(WebCore::EventPath::eventTargetRespectingTargetRules):
* Source/WebCore/editing/TextIterator.cpp:
(WebCore::firstNode):
* Source/WebCore/loader/archive/ArchiveResourceCollection.cpp:
(WebCore::ArchiveResourceCollection::archiveResourceForURL):
* Source/WebCore/platform/graphics/Path.cpp:
(WebCore::Path::ensureImpl):
* Source/WebCore/platform/mac/PlatformScreenMac.mm:
(WebCore::screen):
* Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm:
(WebKit::NetworkSessionCocoa::sessionSetForPage):
(WebKit::NetworkSessionCocoa::sessionSetForPage const):
* Source/WebKit/WebProcess/Automation/WebAutomationSessionProxy.cpp:
(WebKit::WebAutomationSessionProxy::getAccessibilityObjectForNode):
(WebKit::containerElementForElement):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:static analyzer clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants