Repository: go-critic/go-critic (go)

Issue:
commentedOutCode: false positive for the example output oneliner
The following code in the file `gocritic_test.go` produces `commentedOutCode` false positive:
```go
package env_test

import "fmt"

func ExampleCommentedOutCode() {
	const c = "{Foo:a}"
	fmt.Printf("%v", c)
	// Output: {Foo:a}
}
```

```sh
❯ gocritic check -enable commentedOutCode gocritic_test.go
./gocritic_test.go:8:2: commentedOutCode: may want to remove commented-out code
```


Found in the `caarlos0/env` [repo](https://github.com/caarlos0/env/blob/a9fcfa336f5ca5d31788135b4a6c9b3375b06976/example_test.go#L354):

```sh
❯ gocritic check -enable commentedOutCode example_test.go
./example_test.go:353:2: commentedOutCode: may want to remove commented-out code
./example_test.go:391:2: commentedOutCode: may want to remove commented-out code
```

Pull request description:
checkers: fix commentedOutCode false positive
Fixes #1444

Patch:
diff --git a/checkers/commentedOutCode_checker.go b/checkers/commentedOutCode_checker.go
index 8595b79..788355d 100644
--- a/checkers/commentedOutCode_checker.go
+++ b/checkers/commentedOutCode_checker.go
@@ -93,6 +93,10 @@ func (c *commentedOutCodeChecker) VisitLocalComment(cg *ast.CommentGroup) {
 		return
 	}
 
+	if c.isExampleOutputComment(s) {
+		return
+	}
+
 	stmt := strparse.Stmt(s)
 
 	if c.isPermittedStmt(stmt) {
@@ -109,11 +113,6 @@ func (c *commentedOutCodeChecker) VisitLocalComment(cg *ast.CommentGroup) {
 		return
 	}
 
-	// Some attempts to avoid false positives.
-	if c.skipBlock(s) {
-		return
-	}
-
 	// Add braces to make block statement from
 	// multiple statements.
 	stmt = strparse.Stmt(fmt.Sprintf("{ %s }", s))
@@ -123,15 +122,18 @@ func (c *commentedOutCodeChecker) VisitLocalComment(cg *ast.CommentGroup) {
 	}
 }
 
-func (c *commentedOutCodeChecker) skipBlock(s string) bool {
-	lines := strings.Split(s, "\n") // There is at least 1 line, that's invariant
-
-	// Special example test block.
-	if isExampleTestFunc(c.fn) && strings.Contains(lines[0], "Output:") {
-		return true
-	}
-
-	return false
+// An example output comment can be one of the following:
+//
+//	Output: some output
+//
+// or
+//
+//	Output:
+//	some output
+//
+// See https://go.dev/blog/examples
+func (c *commentedOutCodeChecker) isExampleOutputComment(s string) bool {
+	return isExampleTestFunc(c.fn) && strings.Contains(s, "Output:")
 }
 
 func (c *commentedOutCodeChecker) isPermittedStmt(stmt ast.Stmt) bool {


Test patch:
diff --git a/checkers/testdata/commentedOutCode/negative_tests.go b/checkers/testdata/commentedOutCode/negative_tests.go
index cf71ca1..8e7c187 100644
--- a/checkers/testdata/commentedOutCode/negative_tests.go
+++ b/checkers/testdata/commentedOutCode/negative_tests.go
@@ -6,6 +6,10 @@ func ExampleFoo() {
 	// blah
 }
 
+func ExampleBar() {
+	// Output: {Bar:a}
+}
+
 func permittedComments() {
 	// TODO: foo(1+2)