diff --git a/rules/S8209/go/metadata.json b/rules/S8209/go/metadata.json new file mode 100644 index 00000000000..01673ffd982 --- /dev/null +++ b/rules/S8209/go/metadata.json @@ -0,0 +1,27 @@ +{ + "title": "Consecutive function parameters with the same type should be grouped", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant/Issue", + "constantCost": "5 min" + }, + "tags": [ + "idiom", + "style" + ], + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-8209", + "sqKey": "S8209", + "scope": "All", + "defaultQualityProfiles": [ + "Sonar way" + ], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S8209/go/rule.adoc b/rules/S8209/go/rule.adoc new file mode 100644 index 00000000000..5105173aab4 --- /dev/null +++ b/rules/S8209/go/rule.adoc @@ -0,0 +1,49 @@ +This is an issue when two or more consecutive function parameters have the same type but are declared with separate type annotations. + +== Why is this an issue? + +Go provides a convenient syntax feature that allows you to group consecutive parameters of the same type together. Instead of repeating the type for each parameter, you can omit the type from all but the last parameter in the group. + +This feature serves multiple purposes: + +* *Reduces visual clutter*: Eliminating redundant type declarations makes function signatures cleaner and easier to read. +* *Follows Go idioms*: The Go community and official documentation consistently use this syntax pattern, making it the expected style. +* *Improves maintainability*: When you need to change the type of grouped parameters, you only need to update it in one place. + +The Go language designers included this feature specifically to make code more concise while maintaining clarity. Using the grouped syntax demonstrates familiarity with Go conventions and produces code that feels natural to other Go developers. + +=== What is the potential impact? + +This issue affects code readability and consistency. While it doesn't impact functionality, using non-idiomatic Go syntax can make code appear less professional and may slow down code reviews as developers notice the style inconsistency. + +== How to fix it + +Group consecutive parameters of the same type by removing the type annotation from all but the last parameter in the group. + +=== Code examples + +==== Noncompliant code example + +[source,go,diff-id=1,diff-type=noncompliant] +---- +func add(x int, y int) int { // Noncompliant + return x + y +} +---- + +==== Compliant solution + +[source,go,diff-id=1,diff-type=compliant] +---- +func add(x, y int) int { + return x + y +} +---- + +== Resources + +=== Documentation + + * A Tour of Go - Functions continued - https://go.dev/tour/basics/5[Official Go tutorial explaining how to group consecutive parameters of the same type] + + * Go's Declaration Syntax - https://go.dev/blog/gos-declaration-syntax[Blog post explaining the reasoning behind Go's declaration syntax design] diff --git a/rules/S8209/metadata.json b/rules/S8209/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8209/metadata.json @@ -0,0 +1,2 @@ +{ +}