diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp
index 87811c01cdc28851766d33ba77dfb63e580a38c7..30f589e60264c6864dff35cdd10c574c6e3ecf7b 100644
--- a/src/plugins/cpptools/cppcodeformatter.cpp
+++ b/src/plugins/cpptools/cppcodeformatter.cpp
@@ -1040,16 +1040,18 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
 
     switch (newState) {
     case namespace_start:
-        if (firstToken)
+        if (firstToken) {
             *savedIndentDepth = tokenPosition;
-        *indentDepth = tokenPosition;
+            *indentDepth = tokenPosition;
+        }
         break;
 
     case enum_start:
     case class_start:
-        if (firstToken)
+        if (firstToken) {
             *savedIndentDepth = tokenPosition;
-        *indentDepth = tokenPosition;
+            *indentDepth = tokenPosition;
+        }
         *paddingDepth = 2*m_indentSize;
         break;
 
@@ -1075,9 +1077,10 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
         break;
 
     case declaration_start:
-        if (firstToken)
+        if (firstToken) {
             *savedIndentDepth = tokenPosition;
-        *indentDepth = *savedIndentDepth;
+            *indentDepth = *savedIndentDepth;
+        }
         // continuation indent in function bodies only, to not indent
         // after the return type in "void\nfoo() {}"
         for (int i = 0; state(i).type != topmost_intro; ++i) {
diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
index 9d6f7c6584fcd3ecbb4f81fe76f3512e1fef9e00..bebe2383c29b0ec342e4ef28d4a3d2343bfe11a1 100644
--- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
+++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp
@@ -48,6 +48,7 @@ private Q_SLOTS:
     void blockStmtInIf();
     void nestedInitializer();
     void forStatement();
+    void templateSingleline();
 };
 
 struct Line {
@@ -939,6 +940,18 @@ void tst_CodeFormatter::forStatement()
     checkIndent(data);
 }
 
+void tst_CodeFormatter::templateSingleline()
+{
+    QList<Line> data;
+    data
+         << Line("template <typename T> class Foo")
+         << Line("{")
+         << Line("    T t;")
+         << Line("}")
+         ;
+    checkIndent(data);
+}
+
 QTEST_APPLESS_MAIN(tst_CodeFormatter)
 #include "tst_codeformatter.moc"