Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions rules/S8200/go/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
60 changes: 60 additions & 0 deletions rules/S8200/go/rule.adoc
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 2 additions & 0 deletions rules/S8200/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}