]> granicus.if.org Git - clang/commitdiff
Extend -Wliteral-conversion to catch "int i = -1.234"
authorMatt Beaumont-Gay <matthewbg@google.com>
Thu, 8 Sep 2011 22:30:47 +0000 (22:30 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Thu, 8 Sep 2011 22:30:47 +0000 (22:30 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@139326 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/warn-literal-conversion.cpp

index f5dec069ccf34d58bde915485201e304ec435daa..aec90bd9ceedbbd18f8a4675e1b0edb7092c2c83 100644 (file)
@@ -3287,6 +3287,11 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
         return;
       
       Expr *InnerE = E->IgnoreParenImpCasts();
+      // We also want to warn on, e.g., "int i = -1.234"
+      if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
+        if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
+          InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
+
       if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) {
         DiagnoseFloatingLiteralImpCast(S, FL, T, CC);
       } else {
index b9c952873b9f7915ee89088f49685dd1a6c0d464..3fc8a6fec7931a413c7cc96829c93accecf5c6fe 100644 (file)
@@ -30,8 +30,7 @@ void test0() {
   // Test passing a literal floating-point value to a function that takes an integer.
   foo(1.2F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
 
-  // FIXME: -Wconversion-literal doesn't catch "-1.2F".
-  int y10 = -1.2F;
+  int y10 = -1.2F;  // expected-warning {{implicit conversion turns literal floating-point number into integer}}
 
   // -Wconversion-literal does NOT catch const values.
   // (-Wconversion DOES catch them.)