nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: mgechev/revive
Pull request #642 (2022-02-26 19:35:14)

Incorrect handling of token.{LEQ,GEQ} for constant-logical-expr
Fixes the implementation for `constant-logical-expr` where logic handling for `token.LEQ` and `token.GEQ` operators is incorrect.

Fixes: #641


Patch:
diff --git a/rule/constant-logical-expr.go b/rule/constant-logical-expr.go
index 6a91561..ba15412 100644
--- a/rule/constant-logical-expr.go
+++ b/rule/constant-logical-expr.go
@@ -1,9 +1,10 @@
 package rule
 
 import (
-	"github.com/mgechev/revive/lint"
 	"go/ast"
 	"go/token"
+
+	"github.com/mgechev/revive/lint"
 )
 
 // ConstantLogicalExprRule warns on constant logical expressions.
@@ -44,11 +45,13 @@ func (w *lintConstantLogicalExpr) Visit(node ast.Node) ast.Visitor {
 			return w
 		}
 
-		if n.Op == token.EQL {
+		// Handles cases like: a <= a, a == a, a >= a
+		if w.isEqualityOperator(n.Op) {
 			w.newFailure(n, "expression always evaluates to true")
 			return w
 		}
 
+		// Handles cases like: a < a, a > a, a != a
 		if w.isInequalityOperator(n.Op) {
 			w.newFailure(n, "expression always evaluates to false")
 			return w
@@ -69,9 +72,18 @@ func (w *lintConstantLogicalExpr) isOperatorWithLogicalResult(t token.Token) boo
 	return false
 }
 
+func (w *lintConstantLogicalExpr) isEqualityOperator(t token.Token) bool {
+	switch t {
+	case token.EQL, token.LEQ, token.GEQ:
+		return true
+	}
+
+	return false
+}
+
 func (w *lintConstantLogicalExpr) isInequalityOperator(t token.Token) bool {
 	switch t {
-	case token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ:
+	case token.LSS, token.GTR, token.NEQ:
 		return true
 	}
 


Test patch:
diff --git a/testdata/constant-logical-expr.go b/testdata/constant-logical-expr.go
index 5adb404..f3e1b98 100644
--- a/testdata/constant-logical-expr.go
+++ b/testdata/constant-logical-expr.go
@@ -1,5 +1,7 @@
 package fixtures
 
+import "fmt"
+
 // from github.com/ugorji/go/codec/helper.go
 func isNaN(f float64) bool { return f != f } // MATCH /expression always evaluates to false/
 
@@ -9,9 +11,9 @@ func foo1(f float64) bool { return foo2(2.) > foo2(2.) } // MATCH /expression al
 
 func foo2(f float64) bool { return f < f } // MATCH /expression always evaluates to false/
 
-func foo3(f float64) bool { return f <= f } // MATCH /expression always evaluates to false/
+func foo3(f float64) bool { return f <= f } // MATCH /expression always evaluates to true/
 
-func foo4(f float64) bool { return f >= f } // MATCH /expression always evaluates to false/
+func foo4(f float64) bool { return f >= f } // MATCH /expression always evaluates to true/
 
 func foo5(f float64) bool { return f == f } // MATCH /expression always evaluates to true/
Repository: mgechev/revive
Pull request #647 (2022-03-08 14:11:37)

range-val-address: improve detection
<!-- ### CHECKLIST ### -->
<!-- Please, describe in details what's your motivation for this PR -->
<!-- Did you add tests? -->
<!-- Does your code follows the coding style of the rest of the repository? -->
<!-- Does the Travis build passes? -->

This pr will improve the range-val-address rule. i added tests to display the issue.

#513

chavacava: Hi @bernhardreisenberger, thanks for th PR

Patch:
diff --git a/rule/range-val-address.go b/rule/range-val-address.go
index bad26e5..1412955 100644
--- a/rule/range-val-address.go
+++ b/rule/range-val-address.go
@@ -95,16 +95,34 @@ func (bw rangeBodyVisitor) Visit(node ast.Node) ast.Visitor {
 		case *ast.CallExpr:
 			if fun, ok := e.Fun.(*ast.Ident); ok && fun.Name == "append" { // e.g. ...append(arr, &value)
 				for _, v := range e.Args {
-					if bw.isAccessingRangeValueAddress(v) {
-						bw.onFailure(bw.newFailure(e))
+					if lit, ok := v.(*ast.CompositeLit); ok { // e.g. ...append(arr, v{id:&value})
+						bw.checkCompositeLit(lit)
+						continue
+					}
+					if bw.isAccessingRangeValueAddress(v) { // e.g. ...append(arr, &value)
+						bw.onFailure(bw.newFailure(v))
 					}
 				}
 			}
+		case *ast.CompositeLit: // e.g. ...v{id:&value}
+			bw.checkCompositeLit(e)
 		}
 	}
 	return bw
 }
 
+func (bw rangeBodyVisitor) checkCompositeLit(comp *ast.CompositeLit) {
+	for _, exp := range comp.Elts {
+		e, ok := exp.(*ast.KeyValueExpr)
+		if !ok {
+			continue
+		}
+		if bw.isAccessingRangeValueAddress(e.Value) {
+			bw.onFailure(bw.newFailure(e.Value))
+		}
+	}
+}
+
 func (bw rangeBodyVisitor) isAccessingRangeValueAddress(exp ast.Expr) bool {
 	u, ok := exp.(*ast.UnaryExpr)
 	if !ok {


Test patch:
diff --git a/testdata/range-val-address.go b/testdata/range-val-address.go
index e8eb756..ebf7fcd 100644
--- a/testdata/range-val-address.go
+++ b/testdata/range-val-address.go
@@ -138,3 +138,26 @@ func rangeValAddress13() {
 		m = append(m, &value.id)
 	}
 }
+
+func rangeValAddress14() {
+	type v struct {
+		id *string
+	}
+
+	m := []v{}
+	for _, value := range []string{"A", "B", "C"} {
+		a := v{id: &value} // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
+		m = append(m, a)
+	}
+}
+
+func rangeValAddress15() {
+	type v struct {
+		id *string
+	}
+
+	m := []v{}
+	for _, value := range []string{"A", "B", "C"} {
+		m = append(m, v{id: &value}) // MATCH /suspicious assignment of 'value'. range-loop variables always have the same address/
+	}
+}
Repository: mgechev/revive
Pull request #819 (2023-05-03 14:00:09)

Docs: Fix invalid toml in rule.add-constant example
<!-- ### IMPORTANT ### -->
<!-- Please do not create a Pull Request without creating an issue first.** -->
_< !-- If you're fixing a typo or improving the documentation, you may not have to open an issue. -->_

The example for `[rule.add-constant]` was invalid TOML.
Instead of `"ignoreFuncs": "foo"` it should be `ignoreFuncs = "foo"`.

(Also added some spacing for improved readability.)


Patch:
diff --git a/RULES_DESCRIPTIONS.md b/RULES_DESCRIPTIONS.md
index e37640b..c52719b 100644
--- a/RULES_DESCRIPTIONS.md
+++ b/RULES_DESCRIPTIONS.md
@@ -92,7 +92,7 @@ Example:
 
 ```toml
 [rule.add-constant]
-  arguments = [{maxLitCount = "3",allowStrs ="\"\"",allowInts="0,1,2",allowFloats="0.0,0.,1.0,1.,2.0,2.","ignoreFuncs": "os\\.*,fmt\\.Println,make"}]
+  arguments = [{ maxLitCount = "3", allowStrs = "\"\"", allowInts = "0,1,2", allowFloats = "0.0,0.,1.0,1.,2.0,2.", ignoreFuncs = "os\\.*,fmt\\.Println,make" }]
 ```
 
 ## argument-limit
diff --git a/rule/comment-spacings.go b/rule/comment-spacings.go
index e0898c3..0d75c55 100644
--- a/rule/comment-spacings.go
+++ b/rule/comment-spacings.go
@@ -23,6 +23,7 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) {
 		r.allowList = []string{
 			"//go:",
 			"//revive:",
+			"//nolint:",
 		}
 
 		for _, arg := range arguments {


Test patch:
diff --git a/testdata/comment-spacings.go b/testdata/comment-spacings.go
index 5b1d281..6d94137 100644
--- a/testdata/comment-spacings.go
+++ b/testdata/comment-spacings.go
@@ -39,3 +39,6 @@ Should be valid
 
 /* valid
  */
+
+//nolint:staticcheck // nolint should be in the default list of acceptable comments.
+var b string
Repository: mgechev/revive
Pull request #873 (2023-08-18 03:35:26)

fix: unnecesary alert for use-any when comments inside interface{}
See https://github.com/mgechev/revive/issues/872



Patch:
diff --git a/rule/use-any.go b/rule/use-any.go
index bdf3c93..9c6d5cc 100644
--- a/rule/use-any.go
+++ b/rule/use-any.go
@@ -1,9 +1,8 @@
 package rule
 
 import (
-	"go/ast"
-
 	"github.com/mgechev/revive/lint"
+	"go/ast"
 )
 
 // UseAnyRule lints given else constructs.
@@ -14,6 +13,7 @@ func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
 	var failures []lint.Failure
 
 	walker := lintUseAny{
+		commentPositions: getCommentsPositions(file.AST.Comments),
 		onFailure: func(failure lint.Failure) {
 			failures = append(failures, failure)
 		},
@@ -30,7 +30,8 @@ func (*UseAnyRule) Name() string {
 }
 
 type lintUseAny struct {
-	onFailure func(lint.Failure)
+	commentPositions []int
+	onFailure        func(lint.Failure)
 }
 
 func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
@@ -40,7 +41,13 @@ func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
 	}
 
 	if len(it.Methods.List) != 0 {
-		return w // it is not and empty interface
+		return w // it is not an empty interface
+	}
+
+	for _, pos := range w.commentPositions {
+		if pos > int(it.Pos()) && pos < int(it.End()) {
+			return w // it is a comment inside the interface
+		}
 	}
 
 	w.onFailure(lint.Failure{
@@ -52,3 +59,14 @@ func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
 
 	return w
 }
+
+func getCommentsPositions(commentGroups []*ast.CommentGroup) []int {
+	result := []int{}
+	for _, commentGroup := range commentGroups {
+		for _, comment := range commentGroup.List {
+			result = append(result, int(comment.Pos()))
+		}
+	}
+
+	return result
+}


Test patch:
diff --git a/testdata/use-any.go b/testdata/use-any.go
index bc6e647..a624d5e 100644
--- a/testdata/use-any.go
+++ b/testdata/use-any.go
@@ -17,6 +17,10 @@ func any2(a int) interface{} {} // MATCH /since GO 1.18 'interface{}' can be rep
 
 var ni interface{ Close() }
 
+var ni interface {
+	// Close()
+}
+
 type nt interface{ Close() }
 type na = interface{ Close() }