Skip to content

Formatting: Avoid crashing when indent width is 0. rdar://37835956 #14873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,9 @@ class CodeFormatter {
auto AddIndentFunc = [&] () {
auto Width = FmtOptions.UseTabs ? FmtOptions.TabWidth
: FmtOptions.IndentWidth;
// We don't need to add additional indentation if Width is zero.
if (!Width)
return;
// Increment indent.
ExpandedIndent += Width;
// Normalize indent to align on proper column indent width.
Expand Down
22 changes: 22 additions & 0 deletions test/SourceKit/CodeFormat/indent-zero-width.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Foo {

var test : Int

func foo() {
test = 1
}

}
func bar(a: Int,
b: Int) {}

// RUN: %sourcekitd-test -req=format -line=1 -length=1 -req-opts=usetabs=0 -req-opts=indentwidth=0 %s >%t.response
// RUN: %sourcekitd-test -req=format -line=3 -length=1 -req-opts=usetabs=0 -req-opts=indentwidth=0 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=6 -length=1 -req-opts=usetabs=0 -req-opts=indentwidth=0 %s >>%t.response
// RUN: %sourcekitd-test -req=format -line=11 -length=1 -req-opts=usetabs=0 -req-opts=indentwidth=0 %s >>%t.response
// RUN: %FileCheck --strict-whitespace %s <%t.response

// CHECK: key.sourcetext: "class Foo {"
// CHECK: key.sourcetext: "var test : Int"
// CHECK: key.sourcetext: " test = 1"
// CHECK: key.sourcetext: " b: Int) {}"