nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: mgechev/revive
Pull request #985 (2024-05-08 12:53:22)

Fix false positive confusing-naming error on generics with pointer type receiver
Fix logic to handle expressions that include pointers to generic types.
Add test data

Closes https://github.com/mgechev/revive/issues/982



Patch:
diff --git a/rule/confusing-naming.go b/rule/confusing-naming.go
index febfd88..32f6dd8 100644
--- a/rule/confusing-naming.go
+++ b/rule/confusing-naming.go
@@ -138,16 +138,32 @@ func getStructName(r *ast.FieldList) string {
 
 	switch v := t.(type) {
 	case *ast.StarExpr:
-		t = v.X
+		return extractFromStarExpr(v)
 	case *ast.IndexExpr:
-		t = v.X
+		return extractFromIndexExpr(v)
+	case *ast.Ident:
+		return v.Name
 	}
 
-	if p, _ := t.(*ast.Ident); p != nil {
-		result = p.Name
+	return defaultStructName
+}
+
+func extractFromStarExpr(expr *ast.StarExpr) string {
+	switch v := expr.X.(type) {
+	case *ast.IndexExpr:
+		return extractFromIndexExpr(v)
+	case *ast.Ident:
+		return v.Name
 	}
+	return defaultStructName
+}
 
-	return result
+func extractFromIndexExpr(expr *ast.IndexExpr) string {
+	switch v := expr.X.(type) {
+	case *ast.Ident:
+		return v.Name
+	}
+	return defaultStructName
 }
 
 func checkStructFields(fields *ast.FieldList, structName string, w *lintConfusingNames) {
diff --git a/rule/utils.go b/rule/utils.go
index 8ffded4..1267c2d 100644
--- a/rule/utils.go
+++ b/rule/utils.go
@@ -166,7 +166,7 @@ func checkNumberOfArguments(expected int, args lint.Arguments, ruleName string)
 	}
 }
 
-var directiveCommentRE = regexp.MustCompile("//(line |extern |export |[a-z0-9]+:[a-z0-9])") // see https://go-review.googlesource.com/c/website/+/442516/1..2/_content/doc/comment.md#494
+var directiveCommentRE = regexp.MustCompile("^//(line |extern |export |[a-z0-9]+:[a-z0-9])") // see https://go-review.googlesource.com/c/website/+/442516/1..2/_content/doc/comment.md#494
 
 func isDirectiveComment(line string) bool {
 	return directiveCommentRE.MatchString(line)


Test patch:
diff --git a/testdata/comment-spacings.go b/testdata/comment-spacings.go
index da8459b..695485f 100644
--- a/testdata/comment-spacings.go
+++ b/testdata/comment-spacings.go
@@ -52,3 +52,6 @@ type c struct {
 //export MyFunction
 
 //nolint:gochecknoglobals
+
+//this is a regular command that's incorrectly formatted //nolint:foobar // because one two three
+// MATCH:56 /no space between comment delimiter and comment text/
diff --git a/testdata/confusing-naming1.go b/testdata/confusing-naming1.go
index 5b06bac..1fb4d7d 100644
--- a/testdata/confusing-naming1.go
+++ b/testdata/confusing-naming1.go
@@ -70,3 +70,14 @@ type y[T any] struct{}
 
 func (y[T]) method() {
 }
+
+// issue #982
+type a[T any] struct{}
+
+func (x *a[T]) method() {
+}
+
+type b[T any] struct{}
+
+func (x *b[T]) method() {
+}
Repository: mgechev/revive
Pull request #988 (2024-05-11 17:14:58)

more precise regular expression for directive comments
Improves regular expression for handling cases like 

`//this is a regular command that's incorrectly formatted //nolint:foobar // because one two three` 





Patch:
diff --git a/rule/utils.go b/rule/utils.go
index 8ffded4..1267c2d 100644
--- a/rule/utils.go
+++ b/rule/utils.go
@@ -166,7 +166,7 @@ func checkNumberOfArguments(expected int, args lint.Arguments, ruleName string)
 	}
 }
 
-var directiveCommentRE = regexp.MustCompile("//(line |extern |export |[a-z0-9]+:[a-z0-9])") // see https://go-review.googlesource.com/c/website/+/442516/1..2/_content/doc/comment.md#494
+var directiveCommentRE = regexp.MustCompile("^//(line |extern |export |[a-z0-9]+:[a-z0-9])") // see https://go-review.googlesource.com/c/website/+/442516/1..2/_content/doc/comment.md#494
 
 func isDirectiveComment(line string) bool {
 	return directiveCommentRE.MatchString(line)


Test patch:
diff --git a/testdata/comment-spacings.go b/testdata/comment-spacings.go
index da8459b..695485f 100644
--- a/testdata/comment-spacings.go
+++ b/testdata/comment-spacings.go
@@ -52,3 +52,6 @@ type c struct {
 //export MyFunction
 
 //nolint:gochecknoglobals
+
+//this is a regular command that's incorrectly formatted //nolint:foobar // because one two three
+// MATCH:56 /no space between comment delimiter and comment text/
Repository: mgechev/revive
Pull request #1149 (2024-11-29 23:48:26)

cognitive-complexity: handle direct recursion
The PR extends `cognitive-complexity` rule with handling direct recursion.


Patch:
diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go
index ecde388..b1122d0 100644
--- a/rule/cognitive_complexity.go
+++ b/rule/cognitive_complexity.go
@@ -67,7 +67,9 @@ func (w cognitiveComplexityLinter) lintCognitiveComplexity() {
 	f := w.file
 	for _, decl := range f.AST.Decls {
 		if fn, ok := decl.(*ast.FuncDecl); ok && fn.Body != nil {
-			v := cognitiveComplexityVisitor{}
+			v := cognitiveComplexityVisitor{
+				name: fn.Name,
+			}
 			c := v.subTreeComplexity(fn.Body)
 			if c > w.maxComplexity {
 				w.onFailure(lint.Failure{
@@ -82,6 +84,7 @@ func (w cognitiveComplexityLinter) lintCognitiveComplexity() {
 }
 
 type cognitiveComplexityVisitor struct {
+	name         *ast.Ident
 	complexity   int
 	nestingLevel int
 }
@@ -125,8 +128,15 @@ func (v *cognitiveComplexityVisitor) Visit(n ast.Node) ast.Visitor {
 		if n.Label != nil {
 			v.complexity++
 		}
+	case *ast.CallExpr:
+		if ident, ok := n.Fun.(*ast.Ident); ok {
+			if ident.Obj == v.name.Obj && ident.Name == v.name.Name {
+				// called by same function directly (direct recursion)
+				v.complexity++
+				return nil
+			}
+		}
 	}
-	// TODO handle (at least) direct recursion
 
 	return v
 }
diff --git a/rule/deep_exit.go b/rule/deep_exit.go
index ef81eb8..8441b50 100644
--- a/rule/deep_exit.go
+++ b/rule/deep_exit.go
@@ -17,7 +17,7 @@ func (*DeepExitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
 		failures = append(failures, failure)
 	}
 
-	w := lintDeepExit{onFailure: onFailure, isTestFile: file.IsTest()}
+	w := &lintDeepExit{onFailure: onFailure, isTestFile: file.IsTest()}
 	ast.Walk(w, file.AST)
 	return failures
 }
@@ -32,7 +32,7 @@ type lintDeepExit struct {
 	isTestFile bool
 }
 
-func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
+func (w *lintDeepExit) Visit(node ast.Node) ast.Visitor {
 	if fd, ok := node.(*ast.FuncDecl); ok {
 		if w.mustIgnore(fd) {
 			return nil // skip analysis of this function


Test patch:
diff --git a/testdata/cognitive_complexity.go b/testdata/cognitive_complexity.go
index c20794b..17fab4e 100644
--- a/testdata/cognitive_complexity.go
+++ b/testdata/cognitive_complexity.go
@@ -281,3 +281,13 @@ func (m *Migrator) MigrateIfNeeded(target *EtcdVersionPair) error { // MATCH /fu
 
 // no regression test for issue #451
 func myFunc()
+
+// Recursive functions
+func Walk(t *Tree, ch chan int) { // MATCH /function Walk has cognitive complexity 3 (> max enabled 0)/
+	if t == nil { // +1
+		return
+	}
+	Walk(t.Left, ch) // +1
+	ch <- t.Value
+	Walk(t.Right, ch) // +1
+}