Skip to content

Commit 7fe3c5c

Browse files
authored
Formatting: Avoid crashing when indent width is 0. rdar://37835956 (#14873)
1 parent 420328b commit 7fe3c5c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/IDE/Formatting.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,9 @@ class CodeFormatter {
843843
auto AddIndentFunc = [&] () {
844844
auto Width = FmtOptions.UseTabs ? FmtOptions.TabWidth
845845
: FmtOptions.IndentWidth;
846+
// We don't need to add additional indentation if Width is zero.
847+
if (!Width)
848+
return;
846849
// Increment indent.
847850
ExpandedIndent += Width;
848851
// Normalize indent to align on proper column indent width.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Foo {
2+
3+
var test : Int
4+
5+
func foo() {
6+
test = 1
7+
}
8+
9+
}
10+
func bar(a: Int,
11+
b: Int) {}
12+
13+
// RUN: %sourcekitd-test -req=format -line=1 -length=1 -req-opts=usetabs=0 -req-opts=indentwidth=0 %s >%t.response
14+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 -req-opts=usetabs=0 -req-opts=indentwidth=0 %s >>%t.response
15+
// RUN: %sourcekitd-test -req=format -line=6 -length=1 -req-opts=usetabs=0 -req-opts=indentwidth=0 %s >>%t.response
16+
// RUN: %sourcekitd-test -req=format -line=11 -length=1 -req-opts=usetabs=0 -req-opts=indentwidth=0 %s >>%t.response
17+
// RUN: %FileCheck --strict-whitespace %s <%t.response
18+
19+
// CHECK: key.sourcetext: "class Foo {"
20+
// CHECK: key.sourcetext: "var test : Int"
21+
// CHECK: key.sourcetext: " test = 1"
22+
// CHECK: key.sourcetext: " b: Int) {}"

0 commit comments

Comments
 (0)