r326619 - [analyzer] Don't throw NSNumberObjectConversion warning on object initialization in if-expression

George Karpenkov via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 2 13:34:24 PST 2018


Author: george.karpenkov
Date: Fri Mar  2 13:34:24 2018
New Revision: 326619

URL: http://llvm.org/viewvc/llvm-project?rev=326619&view=rev
Log:
[analyzer] Don't throw NSNumberObjectConversion warning on object initialization in if-expression

```
if (NSNumber* x = ...)
```
is a reasonable pattern in objc++, we should not warn on it.

rdar://35152234

Differential Revision: https://reviews.llvm.org/D44044

Added:
    cfe/trunk/test/Analysis/number-object-conversion.mm
Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp?rev=326619&r1=326618&r2=326619&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/NumberObjectConversionChecker.cpp Fri Mar  2 13:34:24 2018
@@ -270,8 +270,10 @@ void NumberObjectConversionChecker::chec
                            hasRHS(SuspiciousNumberObjectExprM)));
 
   auto ConversionThroughBranchingM =
-      ifStmt(hasCondition(SuspiciousNumberObjectExprM))
-      .bind("pedantic");
+      ifStmt(allOf(
+          hasCondition(SuspiciousNumberObjectExprM),
+          unless(hasConditionVariableStatement(declStmt())
+      ))).bind("pedantic");
 
   auto ConversionThroughCallM =
       callExpr(hasAnyArgument(allOf(hasType(SuspiciousScalarTypeM),

Added: cfe/trunk/test/Analysis/number-object-conversion.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/number-object-conversion.mm?rev=326619&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/number-object-conversion.mm (added)
+++ cfe/trunk/test/Analysis/number-object-conversion.mm Fri Mar  2 13:34:24 2018
@@ -0,0 +1,13 @@
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -fblocks -fobjc-arc -w -analyzer-checker=osx.NumberObjectConversion -analyzer-config osx.NumberObjectConversion:Pedantic=true %s -verify
+
+#include "Inputs/system-header-simulator-objc.h"
+
+NSNumber* generateNumber();
+
+// expected-no-diagnostics
+int test_initialization_in_ifstmt() { // Don't warn on initialization in guard.
+  if (NSNumber* number = generateNumber()) { // no-warning
+    return 0;
+  }
+  return 1;
+}




More information about the cfe-commits mailing list