From 0d443014370039c85ad877d52874f12dbd826abf Mon Sep 17 00:00:00 2001 From: denis-troller Date: Tue, 21 Oct 2025 17:23:02 +0000 Subject: [PATCH 1/3] Create rule S8210 --- rules/S8210/go/metadata.json | 25 ++++++++++++++++++++ rules/S8210/go/rule.adoc | 44 ++++++++++++++++++++++++++++++++++++ rules/S8210/metadata.json | 2 ++ 3 files changed, 71 insertions(+) create mode 100644 rules/S8210/go/metadata.json create mode 100644 rules/S8210/go/rule.adoc create mode 100644 rules/S8210/metadata.json diff --git a/rules/S8210/go/metadata.json b/rules/S8210/go/metadata.json new file mode 100644 index 00000000000..ff53c17352b --- /dev/null +++ b/rules/S8210/go/metadata.json @@ -0,0 +1,25 @@ +{ + "title": "FIXME", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-8210", + "sqKey": "S8210", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S8210/go/rule.adoc b/rules/S8210/go/rule.adoc new file mode 100644 index 00000000000..7193b5561c7 --- /dev/null +++ b/rules/S8210/go/rule.adoc @@ -0,0 +1,44 @@ +FIXME: add a description + +// If you want to factorize the description uncomment the following line and create the file. +//include::../description.adoc[] + +== Why is this an issue? + +FIXME: remove the unused optional headers (that are commented out) + +//=== What is the potential impact? + +== How to fix it +//== How to fix it in FRAMEWORK NAME + +=== Code examples + +==== Noncompliant code example + +[source,go,diff-id=1,diff-type=noncompliant] +---- +FIXME +---- + +==== Compliant solution + +[source,go,diff-id=1,diff-type=compliant] +---- +FIXME +---- + +//=== How does this work? + +//=== Pitfalls + +//=== Going the extra mile + + +//== Resources +//=== Documentation +//=== Articles & blog posts +//=== Conference presentations +//=== Standards +//=== External coding guidelines +//=== Benchmarks diff --git a/rules/S8210/metadata.json b/rules/S8210/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8210/metadata.json @@ -0,0 +1,2 @@ +{ +} From dd60f45e7ee8d920aa85582e38b27b805082c7cc Mon Sep 17 00:00:00 2001 From: denis-troller Date: Tue, 21 Oct 2025 19:30:23 +0200 Subject: [PATCH 2/3] Update rules/S8210/go/rule.adoc in PR #5771 --- rules/S8210/go/rule.adoc | 58 +++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/rules/S8210/go/rule.adoc b/rules/S8210/go/rule.adoc index 7193b5561c7..4573fa7e1cc 100644 --- a/rules/S8210/go/rule.adoc +++ b/rules/S8210/go/rule.adoc @@ -1,16 +1,24 @@ -FIXME: add a description - -// If you want to factorize the description uncomment the following line and create the file. -//include::../description.adoc[] +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? -FIXME: remove the unused optional headers (that are commented out) +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? +=== 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 -//== How to fix it in FRAMEWORK NAME + +Remove the unused variable declaration entirely when the variable serves no purpose. === Code examples @@ -18,27 +26,39 @@ FIXME: remove the unused optional headers (that are commented out) [source,go,diff-id=1,diff-type=noncompliant] ---- -FIXME +func processData() { + result := calculateValue() // Noncompliant + fmt.Println("Processing complete") +} ---- ==== Compliant solution [source,go,diff-id=1,diff-type=compliant] ---- -FIXME +func processData() { + fmt.Println("Processing complete") +} ---- -//=== How does this work? +== 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] -//=== Pitfalls +=== Related rules -//=== Going the extra mile + * 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)] -//== Resources -//=== Documentation -//=== Articles & blog posts -//=== Conference presentations -//=== Standards -//=== External coding guidelines -//=== Benchmarks + * RSPEC-1854 - https://rules.sonarsource.com/python/RSPEC-1854/[Unused assignments should be removed (Python)] From d4b206445899d2de2e5860642950b7df55215237 Mon Sep 17 00:00:00 2001 From: denis-troller Date: Tue, 21 Oct 2025 19:30:26 +0200 Subject: [PATCH 3/3] Update rules/S8210/go/metadata.json in PR #5771 --- rules/S8210/go/metadata.json | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/rules/S8210/go/metadata.json b/rules/S8210/go/metadata.json index ff53c17352b..38902a6a552 100644 --- a/rules/S8210/go/metadata.json +++ b/rules/S8210/go/metadata.json @@ -1,25 +1,27 @@ { - "title": "FIXME", + "title": "Variables should not be unused", "type": "CODE_SMELL", "status": "ready", "remediation": { - "func": "Constant\/Issue", - "constantCost": "5min" + "func": "Constant/Issue", + "constantCost": "5 min" }, "tags": [ + "unused", + "dead-code" ], - "defaultSeverity": "Major", + "defaultSeverity": "Blocker", "ruleSpecification": "RSPEC-8210", "sqKey": "S8210", "scope": "All", - "defaultQualityProfiles": ["Sonar way"], + "defaultQualityProfiles": [ + "Sonar way" + ], "quickfix": "unknown", "code": { "impacts": { - "MAINTAINABILITY": "HIGH", - "RELIABILITY": "MEDIUM", - "SECURITY": "LOW" + "MAINTAINABILITY": "BLOCKER" }, - "attribute": "CONVENTIONAL" + "attribute": "CLEAR" } -} +} \ No newline at end of file