diff --git a/rules/S8200/go/metadata.json b/rules/S8200/go/metadata.json new file mode 100644 index 00000000000..44562a66b41 --- /dev/null +++ b/rules/S8200/go/metadata.json @@ -0,0 +1,26 @@ +{ + "title": "Struct literals should be used for initialization instead of field-by-field assignment", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant/Issue", + "constantCost": "5 min" + }, + "tags": [ + "idiom" + ], + "defaultSeverity": "Critical", + "ruleSpecification": "RSPEC-8200", + "sqKey": "S8200", + "scope": "All", + "defaultQualityProfiles": [ + "Sonar way" + ], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH" + }, + "attribute": "CONVENTIONAL" + } +} \ No newline at end of file diff --git a/rules/S8200/go/rule.adoc b/rules/S8200/go/rule.adoc new file mode 100644 index 00000000000..22681f2d079 --- /dev/null +++ b/rules/S8200/go/rule.adoc @@ -0,0 +1,60 @@ +This rule raises an issue when a struct variable is declared and then multiple fields are assigned individually in sequence, instead of using struct literal initialization syntax. + +== Why is this an issue? + +When initializing a struct with known values, using field-by-field assignment after declaration creates verbose and harder-to-maintain code. + +Struct literal initialization offers several advantages: + +* **Conciseness**: All field values are specified in one place, making the code more compact and readable. +* **Clarity**: The initialization intent is immediately clear, as all values are visible together. +* **Maintainability**: Adding, removing, or modifying fields is easier when all initialization happens in one location. +* **Error prevention**: Struct literals reduce the risk of forgetting to initialize a field or making assignment errors. +* **Go idioms**: Struct literal initialization is the idiomatic Go way to create and initialize structs with known values. + +Field-by-field assignment is more appropriate when values are computed conditionally or come from different sources at different times. + +=== What is the potential impact? + +Using field-by-field assignment instead of struct literals makes code more verbose and harder to maintain. It increases the likelihood of initialization errors and makes the code less idiomatic in Go. + +== How to fix it + +Replace the struct declaration and field assignments with a struct literal that initializes all fields at once. + +=== Code examples + +==== Noncompliant code example + +[source,go,diff-id=1,diff-type=noncompliant] +---- +func main() { + var student Student + student.name = "Leo" // Noncompliant + student.age = 21 // Noncompliant + student.id = 121 // Noncompliant + student.course = "CS" // Noncompliant +} +---- + +==== Compliant solution + +[source,go,diff-id=1,diff-type=compliant] +---- +func main() { + student := Student{ + name: "Leo", + age: 21, + id: 121, + course: "CS", + } +} +---- + +== Resources + +=== Documentation + + * Go Language Specification - Struct literals - https://golang.org/ref/spec#Struct_literals[Official Go specification for struct literal syntax and usage] + + * Effective Go - Composite literals - https://golang.org/doc/effective_go#composite_literals[Best practices for using composite literals in Go] diff --git a/rules/S8200/metadata.json b/rules/S8200/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8200/metadata.json @@ -0,0 +1,2 @@ +{ +}