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/S8210/go/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Variables should not be unused",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5 min"
},
"tags": [
"unused",
"dead-code"
],
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-8210",
"sqKey": "S8210",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "BLOCKER"
},
"attribute": "CLEAR"
}
}
64 changes: 64 additions & 0 deletions rules/S8210/go/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
This rule raises an issue when variables are declared but never used, assigned but never read, or when pointer variables are never dereferenced.

== Why is this an issue?

Unused variables and ineffectual assignments create dead code that serves no purpose in program execution. This dead code:

* Reduces code readability by cluttering the codebase with unnecessary declarations
* Can confuse other developers who might assume the variable serves a purpose
* May indicate incomplete implementations or leftover code from refactoring
* Increases maintenance burden without providing any value
* Can mask real issues where a variable was intended to be used but was forgotten

In Go, the compiler will catch completely unused variables, but it won't detect cases where variables are assigned but never read, or where pointer variables are never dereferenced. These patterns often indicate logic errors or incomplete code that should be addressed.

=== What is the potential impact?

While unused variables don't directly impact runtime performance or security, they reduce code quality and maintainability. Dead code can hide bugs, make code reviews more difficult, and create confusion for developers working on the codebase.

== How to fix it

Remove the unused variable declaration entirely when the variable serves no purpose.

=== Code examples

==== Noncompliant code example

[source,go,diff-id=1,diff-type=noncompliant]
----
func processData() {
result := calculateValue() // Noncompliant
fmt.Println("Processing complete")
}
----

==== Compliant solution

[source,go,diff-id=1,diff-type=compliant]
----
func processData() {
fmt.Println("Processing complete")
}
----

== Resources

=== Documentation

* Go Tour - Variables - https://go.dev/tour/basics/8[Official Go tutorial covering variable declarations and usage]

* Go Tour - Pointers - https://go.dev/tour/moretypes/1[Official Go tutorial explaining pointer syntax and dereferencing]

* Effective Go - Blank identifier - https://go.dev/doc/effective_go#blank[Official documentation on using the blank identifier for unused values]

=== Standards

* Go Code Review Comments - https://github.com/golang/go/wiki/CodeReviewComments[Official Go code review guidelines that emphasize clean, readable code]

=== Related rules

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

* 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/S8210/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading