Repository: go-critic/go-critic
Pull request #1371 (2023-09-22 22:33:55)

checkers/commentedOutCode: add minimum length of comment parameter.
Implements go-critic#1244.


Patch:
diff --git a/checkers/commentedOutCode_checker.go b/checkers/commentedOutCode_checker.go
index 402ba33..8595b79 100644
--- a/checkers/commentedOutCode_checker.go
+++ b/checkers/commentedOutCode_checker.go
@@ -6,6 +6,7 @@ import (
 	"go/token"
 	"regexp"
 	"strings"
+	"unicode/utf8"
 
 	"github.com/go-critic/go-critic/checkers/internal/astwalk"
 	"github.com/go-critic/go-critic/linter"
@@ -18,6 +19,12 @@ func init() {
 	info.Name = "commentedOutCode"
 	info.Tags = []string{linter.DiagnosticTag, linter.ExperimentalTag}
 	info.Summary = "Detects commented-out code inside function bodies"
+	info.Params = linter.CheckerParams{
+		"minLength": {
+			Value: 15,
+			Usage: "min length of the comment that triggers a warning",
+		},
+	}
 	info.Before = `
 // fmt.Println("Debugging hard")
 foo(1, 2)`
@@ -27,6 +34,7 @@ foo(1, 2)`
 		return astwalk.WalkerForLocalComment(&commentedOutCodeChecker{
 			ctx:              ctx,
 			notQuiteFuncCall: regexp.MustCompile(`\w+\s+\([^)]*\)\s*$`),
+			minLength:        info.Params.Int("minLength"),
 		}), nil
 	})
 }
@@ -37,6 +45,7 @@ type commentedOutCodeChecker struct {
 	fn  *ast.FuncDecl
 
 	notQuiteFuncCall *regexp.Regexp
+	minLength        int
 }
 
 func (c *commentedOutCodeChecker) EnterFunc(fn *ast.FuncDecl) bool {
@@ -69,7 +78,7 @@ func (c *commentedOutCodeChecker) VisitLocalComment(cg *ast.CommentGroup) {
 	// Some very short comment that can be skipped.
 	// Usually triggering on these results in false positive.
 	// Unless there is a very popular call like print/println.
-	cond := len(s) < len("quite too short") &&
+	cond := utf8.RuneCountInString(s) < c.minLength &&
 		!strings.Contains(s, "print") &&
 		!strings.Contains(s, "fmt.") &&
 		!strings.Contains(s, "log.")
diff --git a/docs/overview.md b/docs/overview.md
index 34833a2..ea3203f 100644
--- a/docs/overview.md
+++ b/docs/overview.md
@@ -582,6 +582,16 @@ foo(1, 2)
 ```
 
 
+Checker parameters:
+<ul>
+<li>
+
+  `@commentedOutCode.minLength` min length of the comment that triggers a warning (default 15)
+
+</li>
+
+</ul>
+
 ## commentedOutImport
 
 [


Test patch:
diff --git a/checkers/checkers_test.go b/checkers/checkers_test.go
index b2d1a87..96655dc 100644
--- a/checkers/checkers_test.go
+++ b/checkers/checkers_test.go
@@ -21,7 +21,8 @@ func init() {
 
 func TestCheckers(t *testing.T) {
 	allParams := map[string]map[string]interface{}{
-		"captLocal": {"paramsOnly": false},
+		"captLocal":        {"paramsOnly": false},
+		"commentedOutCode": {"minLength": 9},
 	}
 
 	for _, info := range linter.GetCheckersInfo() {
diff --git a/checkers/testdata/commentedOutCode/negative_tests.go b/checkers/testdata/commentedOutCode/negative_tests.go
index d6840da..cf71ca1 100644
--- a/checkers/testdata/commentedOutCode/negative_tests.go
+++ b/checkers/testdata/commentedOutCode/negative_tests.go
@@ -77,4 +77,6 @@ func permittedComments() {
 	// //line directives with omitted filenames lead to empty filenames
 
 	/* CINC/CINV/CNEG */
+
+	// v := 世界
 }
diff --git a/checkers/testdata/commentedOutCode/positive_tests.go b/checkers/testdata/commentedOutCode/positive_tests.go
index 1e95944..cb3e3f1 100644
--- a/checkers/testdata/commentedOutCode/positive_tests.go
+++ b/checkers/testdata/commentedOutCode/positive_tests.go
@@ -38,4 +38,7 @@ func multiLineCode() {
 	//rulebases.POST("/", postRsHandler)                                //create a rulebase
 	//rulebases.DELETE("/:id", deleteRsHandler)                         //delete a rulebase
 	//rulebases.PUT("/:setid", putRsHandler)                            //update a rulebase
+
+	/*! may want to remove commented-out code */
+	// v := 781
 }