From 1f8f608e90101639c3089c72e9b636dfeee0b718 Mon Sep 17 00:00:00 2001 From: denis-troller Date: Tue, 21 Oct 2025 17:22:20 +0000 Subject: [PATCH 1/3] Create rule S8204 --- rules/S8204/go/metadata.json | 25 ++++++++++++++++++++ rules/S8204/go/rule.adoc | 44 ++++++++++++++++++++++++++++++++++++ rules/S8204/metadata.json | 2 ++ 3 files changed, 71 insertions(+) create mode 100644 rules/S8204/go/metadata.json create mode 100644 rules/S8204/go/rule.adoc create mode 100644 rules/S8204/metadata.json diff --git a/rules/S8204/go/metadata.json b/rules/S8204/go/metadata.json new file mode 100644 index 00000000000..74e163efc2c --- /dev/null +++ b/rules/S8204/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-8204", + "sqKey": "S8204", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S8204/go/rule.adoc b/rules/S8204/go/rule.adoc new file mode 100644 index 00000000000..7193b5561c7 --- /dev/null +++ b/rules/S8204/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/S8204/metadata.json b/rules/S8204/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8204/metadata.json @@ -0,0 +1,2 @@ +{ +} From 29613e6bff71cc0da0a2b74ff059e7431e16dd9f Mon Sep 17 00:00:00 2001 From: denis-troller Date: Tue, 21 Oct 2025 19:27:16 +0200 Subject: [PATCH 2/3] Update rules/S8204/go/rule.adoc in PR #5765 --- rules/S8204/go/rule.adoc | 44 +++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/rules/S8204/go/rule.adoc b/rules/S8204/go/rule.adoc index 7193b5561c7..01e47f0f4b1 100644 --- a/rules/S8204/go/rule.adoc +++ b/rules/S8204/go/rule.adoc @@ -1,16 +1,22 @@ -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 a variable is declared with an initial value and then immediately reassigned without the original value being used. == Why is this an issue? -FIXME: remove the unused optional headers (that are commented out) +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? +=== 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 -//== How to fix it in FRAMEWORK NAME + +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 @@ -18,27 +24,27 @@ FIXME: remove the unused optional headers (that are commented out) [source,go,diff-id=1,diff-type=noncompliant] ---- -FIXME +ctx := context.Background() +ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) // Noncompliant ---- ==== Compliant solution [source,go,diff-id=1,diff-type=compliant] ---- -FIXME +ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) ---- -//=== How does this work? +== Resources + +=== Documentation + + * Go Language Specification - Variable declarations - https://golang.org/ref/spec#Variable_declarations[Official Go documentation on variable declarations and initialization] -//=== Pitfalls + * Effective Go - Variables - https://golang.org/doc/effective_go#variables[Best practices for variable usage in Go] -//=== Going the extra mile +=== Related rules + * 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 4a6c419e2b377e20294cdc63abe415f0b5d75e05 Mon Sep 17 00:00:00 2001 From: denis-troller Date: Tue, 21 Oct 2025 19:27:20 +0200 Subject: [PATCH 3/3] Update rules/S8204/go/metadata.json in PR #5765 --- rules/S8204/go/metadata.json | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/rules/S8204/go/metadata.json b/rules/S8204/go/metadata.json index 74e163efc2c..a89f09b1e64 100644 --- a/rules/S8204/go/metadata.json +++ b/rules/S8204/go/metadata.json @@ -1,25 +1,27 @@ { - "title": "FIXME", + "title": "Variables should not be initialized and then immediately reassigned", "type": "CODE_SMELL", "status": "ready", "remediation": { - "func": "Constant\/Issue", + "func": "Constant/Issue", "constantCost": "5min" }, "tags": [ + "confusing", + "redundant" ], - "defaultSeverity": "Major", + "defaultSeverity": "Blocker", "ruleSpecification": "RSPEC-8204", "sqKey": "S8204", "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