From 1172161da4b661a178b07a7a50937327cd0a9be0 Mon Sep 17 00:00:00 2001 From: Anupriya Kumari Date: Mon, 20 Oct 2025 19:13:08 +0000 Subject: [PATCH 1/5] Handling useless-fallthrough rule conflict --- rule/useless_fallthrough.go | 33 ++++++++++++++++++++------------- testdata/useless_fallthrough.go | 10 +++++----- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/rule/useless_fallthrough.go b/rule/useless_fallthrough.go index 223876722..4a8959776 100644 --- a/rule/useless_fallthrough.go +++ b/rule/useless_fallthrough.go @@ -67,23 +67,30 @@ func (w *lintUselessFallthrough) Visit(node ast.Node) ast.Visitor { continue // not a fallthrough } - confidence := 1.0 - if nextCaseClause := switchStmt.Body.List[i+1].(*ast.CaseClause); nextCaseClause.List == nil { - // the next case clause is the default clause, report with lower confidence. - confidence = 0.8 + nextCaseClause := switchStmt.Body.List[i+1].(*ast.CaseClause) + if nextCaseClause.List == nil { + // The next clause is 'default:', and this is a valid pattern. + // Skip reporting this fallthrough. + continue } + if _, ok := w.commentsMap[branchStmt]; ok { - // The fallthrough has a comment, report with lower confidence. - confidence = 0.5 + // The fallthrough has a comment, still report with lower confidence. + w.onFailure(lint.Failure{ + Confidence: 0.5, + Node: branchStmt, + Category: lint.FailureCategoryCodeStyle, + Failure: `this "fallthrough" can be removed by consolidating this case clause with the next one`, + }) + } else { + w.onFailure(lint.Failure{ + Confidence: 1.0, + Node: branchStmt, + Category: lint.FailureCategoryCodeStyle, + Failure: `this "fallthrough" can be removed by consolidating this case clause with the next one`, + }) } - w.onFailure(lint.Failure{ - Confidence: confidence, - Node: branchStmt, - Category: lint.FailureCategoryCodeStyle, - Failure: `this "fallthrough" can be removed by consolidating this case clause with the next one`, - }) - ast.Walk(w, caseClause) } diff --git a/testdata/useless_fallthrough.go b/testdata/useless_fallthrough.go index 7f2b9cd31..8f623f1f4 100644 --- a/testdata/useless_fallthrough.go +++ b/testdata/useless_fallthrough.go @@ -29,14 +29,14 @@ func uselessFallthrough() { switch a { case 0: - fallthrough // json:{"MATCH": "this \"fallthrough\" can be removed by consolidating this case clause with the next one","Confidence": 0.8} + fallthrough default: println() } switch a { case 0: - fallthrough // json:{"MATCH": "this \"fallthrough\" can be removed by consolidating this case clause with the next one","Confidence": 0.8} + fallthrough default: println() case 1: @@ -47,8 +47,7 @@ func uselessFallthrough() { switch a { case 0: - // This a comment on the case, the confidence must be 0.5 - fallthrough // json:{"MATCH": "this \"fallthrough\" can be removed by consolidating this case clause with the next one","Confidence": 0.5} + fallthrough default: println() } @@ -71,9 +70,10 @@ func uselessFallthrough() { switch a { case 0: //foo:bar - fallthrough // json:{"MATCH": "this \"fallthrough\" can be removed by consolidating this case clause with the next one","Confidence": 0.5} + fallthrough default: println() } + } From dcdcbbdabdb5b609481b41552f6d9e233c42db01 Mon Sep 17 00:00:00 2001 From: Anupriya Kumari Date: Fri, 24 Oct 2025 10:21:13 +0000 Subject: [PATCH 2/5] Revert back to previous version (line 77-87) --- rule/useless_fallthrough.go | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/rule/useless_fallthrough.go b/rule/useless_fallthrough.go index 4a8959776..88880102d 100644 --- a/rule/useless_fallthrough.go +++ b/rule/useless_fallthrough.go @@ -66,33 +66,26 @@ func (w *lintUselessFallthrough) Visit(node ast.Node) ast.Visitor { if !ok || branchStmt.Tok != token.FALLTHROUGH { continue // not a fallthrough } - - nextCaseClause := switchStmt.Body.List[i+1].(*ast.CaseClause) - if nextCaseClause.List == nil { + confidence := 1.0 + if nextCaseClause := switchStmt.Body.List[i+1].(*ast.CaseClause); nextCaseClause.List == nil { // The next clause is 'default:', and this is a valid pattern. // Skip reporting this fallthrough. continue } - if _, ok := w.commentsMap[branchStmt]; ok { - // The fallthrough has a comment, still report with lower confidence. - w.onFailure(lint.Failure{ - Confidence: 0.5, - Node: branchStmt, - Category: lint.FailureCategoryCodeStyle, - Failure: `this "fallthrough" can be removed by consolidating this case clause with the next one`, - }) - } else { - w.onFailure(lint.Failure{ - Confidence: 1.0, - Node: branchStmt, - Category: lint.FailureCategoryCodeStyle, - Failure: `this "fallthrough" can be removed by consolidating this case clause with the next one`, - }) + // The fallthrough has a comment, report with lower confidence. + confidence = 0.5 } + w.onFailure(lint.Failure{ + Confidence: confidence, + Node: branchStmt, + Category: lint.FailureCategoryCodeStyle, + Failure: `this "fallthrough" can be removed by consolidating this case clause with the next one`, + }) + ast.Walk(w, caseClause) - } + } return nil } From 4aeeb0be90d356e1bfeb6f9f1afeb08c01843e62 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 24 Oct 2025 19:29:05 +0300 Subject: [PATCH 3/5] fix lint --- rule/useless_fallthrough.go | 1 - 1 file changed, 1 deletion(-) diff --git a/rule/useless_fallthrough.go b/rule/useless_fallthrough.go index 88880102d..1ec46de9d 100644 --- a/rule/useless_fallthrough.go +++ b/rule/useless_fallthrough.go @@ -85,7 +85,6 @@ func (w *lintUselessFallthrough) Visit(node ast.Node) ast.Visitor { }) ast.Walk(w, caseClause) - } return nil } From f3da85a8c017a08b22fd268dad5cb1356188a6f0 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 24 Oct 2025 19:29:43 +0300 Subject: [PATCH 4/5] revert by adding newlines --- rule/useless_fallthrough.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rule/useless_fallthrough.go b/rule/useless_fallthrough.go index 1ec46de9d..c63f07166 100644 --- a/rule/useless_fallthrough.go +++ b/rule/useless_fallthrough.go @@ -66,6 +66,7 @@ func (w *lintUselessFallthrough) Visit(node ast.Node) ast.Visitor { if !ok || branchStmt.Tok != token.FALLTHROUGH { continue // not a fallthrough } + confidence := 1.0 if nextCaseClause := switchStmt.Body.List[i+1].(*ast.CaseClause); nextCaseClause.List == nil { // The next clause is 'default:', and this is a valid pattern. @@ -86,5 +87,6 @@ func (w *lintUselessFallthrough) Visit(node ast.Node) ast.Visitor { ast.Walk(w, caseClause) } + return nil } From 46c973d839854ab302acb7b94ca8122e15045d8a Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 24 Oct 2025 19:30:16 +0300 Subject: [PATCH 5/5] revert --- testdata/useless_fallthrough.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/testdata/useless_fallthrough.go b/testdata/useless_fallthrough.go index 8f623f1f4..2484a1572 100644 --- a/testdata/useless_fallthrough.go +++ b/testdata/useless_fallthrough.go @@ -29,14 +29,14 @@ func uselessFallthrough() { switch a { case 0: - fallthrough + fallthrough default: println() } switch a { case 0: - fallthrough + fallthrough default: println() case 1: @@ -47,7 +47,7 @@ func uselessFallthrough() { switch a { case 0: - fallthrough + fallthrough default: println() } @@ -70,10 +70,9 @@ func uselessFallthrough() { switch a { case 0: //foo:bar - fallthrough + fallthrough default: println() } - }