Repository: bombsimon/wsl
Pull request #181 (2025-06-28 14:58:01)

Make `append` check work isolated, update check type for `append`


Patch:
diff --git a/wsl.go b/wsl.go
index 7e408ba..1d3c441 100644
--- a/wsl.go
+++ b/wsl.go
@@ -402,6 +402,8 @@ func (w *WSL) checkAssign(stmt *ast.AssignStmt, cursor *Cursor) {
 		for _, expr := range stmt.Rhs {
 			w.checkExpr(expr, cursor)
 		}
+
+		w.checkAppend(stmt, cursor)
 	}()
 
 	if _, ok := w.config.Checks[CheckAssign]; !ok {
@@ -411,7 +413,6 @@ func (w *WSL) checkAssign(stmt *ast.AssignStmt, cursor *Cursor) {
 	cursor.SetChecker(CheckAssign)
 
 	w.checkCuddlingWithoutIntersection(stmt, cursor)
-	w.checkAppend(stmt, cursor)
 }
 
 func (w *WSL) checkAppend(stmt *ast.AssignStmt, cursor *Cursor) {
@@ -444,7 +445,7 @@ func (w *WSL) checkAppend(stmt *ast.AssignStmt, cursor *Cursor) {
 	}
 
 	if !hasIntersection(appendNode, previousNode) {
-		w.addErrorNoIntersection(stmt.Pos(), cursor.checkType)
+		w.addErrorNoIntersection(stmt.Pos(), CheckAppend)
 	}
 }
 


Test patch:
diff --git a/testdata/src/default_config/assign/assign.go b/testdata/src/default_config/assign/assign.go
index e3edd6d..7b8064a 100644
--- a/testdata/src/default_config/assign/assign.go
+++ b/testdata/src/default_config/assign/assign.go
@@ -29,7 +29,7 @@ func strictAppend() {
 	x := "c"
 	s = append(s, x)
 	y := "e"
-	s = append(s, "d") // want `missing whitespace above this line \(no shared variables above assign\)`
+	s = append(s, "d") // want `missing whitespace above this line \(no shared variables above append\)`
 	s = append(s, y)
 }
 
diff --git a/testdata/src/default_config/assign/assign.go.golden b/testdata/src/default_config/assign/assign.go.golden
index 1dad1b5..99a2ae4 100644
--- a/testdata/src/default_config/assign/assign.go.golden
+++ b/testdata/src/default_config/assign/assign.go.golden
@@ -30,7 +30,7 @@ func strictAppend() {
 	s = append(s, x)
 	y := "e"
 
-	s = append(s, "d") // want `missing whitespace above this line \(no shared variables above assign\)`
+	s = append(s, "d") // want `missing whitespace above this line \(no shared variables above append\)`
 	s = append(s, y)
 }
 
diff --git a/testdata/src/with_config/append_only/code.go b/testdata/src/with_config/append_only/code.go
new file mode 100644
index 0000000..59ac901
--- /dev/null
+++ b/testdata/src/with_config/append_only/code.go
@@ -0,0 +1,9 @@
+package testpkg
+
+func fn1(s []string) {
+	a := "a"
+	s = append(s, a)
+
+	x := 1
+	s = append(s, "s") // want `missing whitespace above this line \(no shared variables above append\)`
+}
diff --git a/testdata/src/with_config/append_only/code.go.golden b/testdata/src/with_config/append_only/code.go.golden
new file mode 100644
index 0000000..2cf75c4
--- /dev/null
+++ b/testdata/src/with_config/append_only/code.go.golden
@@ -0,0 +1,11 @@
+package testpkg
+
+func fn1(s []string) {
+	a := "a"
+	s = append(s, a)
+
+	x := 1
+
+	s = append(s, "s") // want `missing whitespace above this line \(no shared variables above append\)`
+}
+
diff --git a/wsl_test.go b/wsl_test.go
index 935bef0..bc84008 100644
--- a/wsl_test.go
+++ b/wsl_test.go
@@ -93,6 +93,13 @@ func TestWithConfig(t *testing.T) {
 				config.Checks.Add(CheckErr)
 			},
 		},
+		{
+			subdir: "append_only",
+			configFn: func(config *Configuration) {
+				config.Checks = NoChecks()
+				config.Checks.Add(CheckAppend)
+			},
+		},
 	} {
 		t.Run(tc.subdir, func(t *testing.T) {
 			config := NewConfig()