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
27 changes: 27 additions & 0 deletions rules/S8204/go/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Variables should not be initialized and then immediately reassigned",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5min"
},
"tags": [
"confusing",
"redundant"
],
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-8204",
"sqKey": "S8204",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "BLOCKER"
},
"attribute": "CLEAR"
}
}
50 changes: 50 additions & 0 deletions rules/S8204/go/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
This rule raises an issue when a variable is declared with an initial value and then immediately reassigned without the original value being used.

== Why is this an issue?

When a variable is initialized and then immediately reassigned without using the original value, the first initialization becomes redundant code.

This pattern reduces code readability and can confuse other developers who might wonder why the variable was initialized twice. It also suggests that the code may have been refactored without proper cleanup, or that the developer was uncertain about the correct initial value.

In Go, this commonly happens with context variables and HTTP clients where developers first assign a default value and then immediately replace it with a more specific configuration.

Removing the redundant initialization makes the code more direct and easier to understand. It also eliminates any confusion about which value the variable actually holds.

=== What is the potential impact?

This issue affects code maintainability by making the code less readable and potentially confusing to other developers. While it doesn't cause runtime errors, it can slow down code reviews and make the codebase harder to maintain.

== How to fix it

Remove the redundant initialization and use only the final assignment. If the variable needs to be declared in a specific scope, use the short variable declaration syntax with the final value directly.

=== Code examples

==== Noncompliant code example

[source,go,diff-id=1,diff-type=noncompliant]
----
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) // Noncompliant
----

==== Compliant solution

[source,go,diff-id=1,diff-type=compliant]
----
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
----

== Resources

=== Documentation

* Go Language Specification - Variable declarations - https://golang.org/ref/spec#Variable_declarations[Official Go documentation on variable declarations and initialization]

* Effective Go - Variables - https://golang.org/doc/effective_go#variables[Best practices for variable usage in Go]

=== Related rules

* RSPEC-1854 - https://rules.sonarsource.com/javascript/RSPEC-1854/[Unused assignments should be removed (JavaScript)]

* RSPEC-1854 - https://rules.sonarsource.com/python/RSPEC-1854/[Unused assignments should be removed (Python)]
2 changes: 2 additions & 0 deletions rules/S8204/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}