diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp
index 12a9ea8d144631054f88c0f7ae337847004018cc..c88e422135e9a2ab6defec8b26ac9a78286027bb 100644
--- a/src/plugins/cpptools/cppcodeformatter.cpp
+++ b/src/plugins/cpptools/cppcodeformatter.cpp
@@ -1062,7 +1062,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
     case if_statement:
     case return_statement:
         if (firstToken)
-            *savedIndentDepth = tokenPosition;
+            *indentDepth = *savedIndentDepth = tokenPosition;
         *paddingDepth = 2*m_indentSize;
         break;
 
@@ -1144,6 +1144,9 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
     }
 
     case substatement_open:
+        // undo parent continuation indent
+        *savedPaddingDepth = 0;
+
         if (firstToken) {
             *savedIndentDepth = tokenPosition;
             *indentDepth = *savedIndentDepth;
@@ -1192,8 +1195,8 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
 
     case substatement:
         // undo the continuation indent of the parent
-        *indentDepth = parentState.savedIndentDepth;
-        *savedIndentDepth = *indentDepth;
+        *savedPaddingDepth = 0;
+
         break;
 
     case maybe_else: {
@@ -1207,7 +1210,7 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
     }   break;
 
     case for_statement_paren_open:
-        *indentDepth = tokenPosition + 1;
+        *paddingDepth = tokenPosition + 1 - *indentDepth;
         break;
 
     case multiline_comment_start:
@@ -1397,6 +1400,7 @@ bool QtStyleCodeFormatter::shouldClearPaddingOnEnter(int state)
     case return_statement:
     case block_open:
     case substatement_open:
+    case substatement:
         return true;
     }
     return false;
diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
index 50b3e73a4f6d08595de5c213adeff00c34c4a3c7..9d6f7c6584fcd3ecbb4f81fe76f3512e1fef9e00 100644
--- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
+++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
@@ -47,6 +47,7 @@ private Q_SLOTS:
     void streamOp();
     void blockStmtInIf();
     void nestedInitializer();
+    void forStatement();
 };
 
 struct Line {
@@ -904,6 +905,40 @@ void tst_CodeFormatter::nestedInitializer()
     checkIndent(data);
 }
 
+void tst_CodeFormatter::forStatement()
+{
+    QList<Line> data;
+    data
+         << Line("void foo()")
+         << Line("{")
+         << Line("    for (a; b; c)")
+         << Line("        bar();")
+         << Line("    for (a; b; c) {")
+         << Line("        bar();")
+         << Line("    }")
+         << Line("    for (a; b; c)")
+         << Line("    {")
+         << Line("        bar();")
+         << Line("    }")
+         << Line("    for (a;")
+         << Line("    ~    b;")
+         << Line("    ~    c)")
+         << Line("        bar();")
+         << Line("    for (a;")
+         << Line("    ~    b;")
+         << Line("    ~    c) {")
+         << Line("        bar();")
+         << Line("    }")
+         << Line("    for (a;")
+         << Line("    ~    b;")
+         << Line("    ~    c)")
+         << Line("    {")
+         << Line("        bar();")
+         << Line("    }")
+         ;
+    checkIndent(data);
+}
+
 QTEST_APPLESS_MAIN(tst_CodeFormatter)
 #include "tst_codeformatter.moc"