From 4d1b697292ba932a41b7a3a82becbd69480d9d13 Mon Sep 17 00:00:00 2001 From: denis-troller Date: Sun, 9 Nov 2025 20:22:08 +0000 Subject: [PATCH 1/5] Create rule S8311 --- rules/S8311/groovy/metadata.json | 25 ++++++++++++++++++ rules/S8311/groovy/rule.adoc | 44 ++++++++++++++++++++++++++++++++ rules/S8311/metadata.json | 2 ++ 3 files changed, 71 insertions(+) create mode 100644 rules/S8311/groovy/metadata.json create mode 100644 rules/S8311/groovy/rule.adoc create mode 100644 rules/S8311/metadata.json diff --git a/rules/S8311/groovy/metadata.json b/rules/S8311/groovy/metadata.json new file mode 100644 index 00000000000..fdebf30e1d8 --- /dev/null +++ b/rules/S8311/groovy/metadata.json @@ -0,0 +1,25 @@ +{ + "title": "FIXME", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-8311", + "sqKey": "S8311", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S8311/groovy/rule.adoc b/rules/S8311/groovy/rule.adoc new file mode 100644 index 00000000000..16a0f60963c --- /dev/null +++ b/rules/S8311/groovy/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,groovy,diff-id=1,diff-type=noncompliant] +---- +FIXME +---- + +==== Compliant solution + +[source,groovy,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/S8311/metadata.json b/rules/S8311/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8311/metadata.json @@ -0,0 +1,2 @@ +{ +} From c0d89a3113052f6ab42b6e9a64a77f8688665cda Mon Sep 17 00:00:00 2001 From: denis-troller Date: Sun, 9 Nov 2025 21:49:16 +0100 Subject: [PATCH 2/5] Update rules/S8311/groovy/rule.adoc in PR #5897 --- rules/S8311/groovy/rule.adoc | 59 ++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/rules/S8311/groovy/rule.adoc b/rules/S8311/groovy/rule.adoc index 16a0f60963c..c93ebbb11b5 100644 --- a/rules/S8311/groovy/rule.adoc +++ b/rules/S8311/groovy/rule.adoc @@ -1,16 +1,28 @@ -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 method is defined with a reserved keyword name surrounded by quotes. == Why is this an issue? -FIXME: remove the unused optional headers (that are commented out) +While Groovy allows defining methods with reserved keyword names by surrounding them with quotes, this practice creates several problems: + +* *Reduced readability*: Quoted method names look unusual and can confuse developers who are not familiar with this Groovy feature. +* *Special calling syntax*: Methods with quoted keyword names require qualified calls using the `this.` prefix, making the calling code more verbose and inconsistent. +* *Maintenance burden*: Code becomes harder to understand and maintain when method names don't clearly express their purpose. +* *Inconsistent style*: Mixing quoted and unquoted method names creates inconsistent code style within a project. + +The Groovy documentation itself states that "using such names might be confusing and is often best to avoid." This feature was primarily intended for specific Java integration scenarios and Domain Specific Language (DSL) use cases, not for general application development. + +=== What is the potential impact? + +Using reserved keywords as method names can lead to: -//=== What is the potential impact? +* *Confusion for team members*: Developers may not understand why certain methods require special calling syntax +* *Increased cognitive load*: Reading and understanding code becomes more difficult +* *Maintenance issues*: Future developers may struggle to work with such code +* *Inconsistent codebase*: Mixed naming conventions reduce overall code quality == How to fix it -//== How to fix it in FRAMEWORK NAME + +Replace the quoted reserved keyword with a descriptive method name that clearly indicates the method's purpose. Use standard naming conventions like camelCase for better readability. === Code examples @@ -18,27 +30,36 @@ FIXME: remove the unused optional headers (that are commented out) [source,groovy,diff-id=1,diff-type=noncompliant] ---- -FIXME +def "abstract"() { // Noncompliant + return true +} + +// Requires special qualification when called +this.abstract() ---- ==== Compliant solution [source,groovy,diff-id=1,diff-type=compliant] ---- -FIXME +def isAbstract() { + return true +} + +// Clear and readable calling syntax +isAbstract() ---- -//=== How does this work? +== Resources + +=== Documentation + + * Groovy Language Documentation - Keywords - https://groovy-lang.org/syntax.html#_keywords[Official Groovy documentation explaining reserved keywords and their usage] -//=== Pitfalls + * Groovy Language Documentation - Identifiers - https://groovy-lang.org/syntax.html#_identifiers[Official documentation on identifiers and quoted identifiers in Groovy] -//=== Going the extra mile +=== Related rules + * RSPEC-4413 - https://rules.sonarsource.com/tsql/RSPEC-4413/[Similar rule for T-SQL about avoiding reserved keywords as identifiers] -//== Resources -//=== Documentation -//=== Articles & blog posts -//=== Conference presentations -//=== Standards -//=== External coding guidelines -//=== Benchmarks + * RSPEC-1527 - https://rules.sonarsource.com/javascript/RSPEC-1527/[JavaScript rule about avoiding future reserved words as identifiers] From 28d1e574637bfb3bdbb357cf24a42640f1a7708e Mon Sep 17 00:00:00 2001 From: denis-troller Date: Sun, 9 Nov 2025 21:49:19 +0100 Subject: [PATCH 3/5] Update rules/S8311/groovy/metadata.json in PR #5897 --- rules/S8311/groovy/metadata.json | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/rules/S8311/groovy/metadata.json b/rules/S8311/groovy/metadata.json index fdebf30e1d8..baeebe6bfef 100644 --- a/rules/S8311/groovy/metadata.json +++ b/rules/S8311/groovy/metadata.json @@ -1,25 +1,27 @@ { - "title": "FIXME", + "title": "Method names should not use reserved keywords", "type": "CODE_SMELL", "status": "ready", "remediation": { - "func": "Constant\/Issue", - "constantCost": "5min" + "func": "Constant/Issue", + "constantCost": "5 min" }, "tags": [ + "confusing", + "naming" ], - "defaultSeverity": "Major", + "defaultSeverity": "Blocker", "ruleSpecification": "RSPEC-8311", "sqKey": "S8311", - "scope": "All", - "defaultQualityProfiles": ["Sonar way"], + "scope": "Main", + "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 From 47e924b501e8d41cf23e64f04fb7b08f19b4f356 Mon Sep 17 00:00:00 2001 From: Ghislain Piot Date: Mon, 8 Dec 2025 14:54:24 +0100 Subject: [PATCH 4/5] Improvements --- rules/S8311/groovy/metadata.json | 6 +++--- rules/S8311/groovy/rule.adoc | 12 ++---------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/rules/S8311/groovy/metadata.json b/rules/S8311/groovy/metadata.json index baeebe6bfef..3c54751c5b1 100644 --- a/rules/S8311/groovy/metadata.json +++ b/rules/S8311/groovy/metadata.json @@ -1,7 +1,7 @@ { "title": "Method names should not use reserved keywords", "type": "CODE_SMELL", - "status": "ready", + "status": "beta", "remediation": { "func": "Constant/Issue", "constantCost": "5 min" @@ -10,7 +10,7 @@ "confusing", "naming" ], - "defaultSeverity": "Blocker", + "defaultSeverity": "Major", "ruleSpecification": "RSPEC-8311", "sqKey": "S8311", "scope": "Main", @@ -20,7 +20,7 @@ "quickfix": "unknown", "code": { "impacts": { - "MAINTAINABILITY": "BLOCKER" + "MAINTAINABILITY": "HIGH" }, "attribute": "CLEAR" } diff --git a/rules/S8311/groovy/rule.adoc b/rules/S8311/groovy/rule.adoc index c93ebbb11b5..9e17e3b6f07 100644 --- a/rules/S8311/groovy/rule.adoc +++ b/rules/S8311/groovy/rule.adoc @@ -22,7 +22,7 @@ Using reserved keywords as method names can lead to: == How to fix it -Replace the quoted reserved keyword with a descriptive method name that clearly indicates the method's purpose. Use standard naming conventions like camelCase for better readability. +Replace the quoted reserved keyword with a descriptive method name that clearly indicates the method's purpose. === Code examples @@ -34,7 +34,6 @@ def "abstract"() { // Noncompliant return true } -// Requires special qualification when called this.abstract() ---- @@ -46,8 +45,7 @@ def isAbstract() { return true } -// Clear and readable calling syntax -isAbstract() +this.isAbstract() ---- == Resources @@ -57,9 +55,3 @@ isAbstract() * Groovy Language Documentation - Keywords - https://groovy-lang.org/syntax.html#_keywords[Official Groovy documentation explaining reserved keywords and their usage] * Groovy Language Documentation - Identifiers - https://groovy-lang.org/syntax.html#_identifiers[Official documentation on identifiers and quoted identifiers in Groovy] - -=== Related rules - - * RSPEC-4413 - https://rules.sonarsource.com/tsql/RSPEC-4413/[Similar rule for T-SQL about avoiding reserved keywords as identifiers] - - * RSPEC-1527 - https://rules.sonarsource.com/javascript/RSPEC-1527/[JavaScript rule about avoiding future reserved words as identifiers] From 0236e3d70f1c953d6d78782ba4684e13d74e61bc Mon Sep 17 00:00:00 2001 From: Ghislain Piot Date: Mon, 15 Dec 2025 17:57:18 +0100 Subject: [PATCH 5/5] Update rule.adoc --- rules/S8311/groovy/rule.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rules/S8311/groovy/rule.adoc b/rules/S8311/groovy/rule.adoc index 9e17e3b6f07..2df51f27f92 100644 --- a/rules/S8311/groovy/rule.adoc +++ b/rules/S8311/groovy/rule.adoc @@ -52,6 +52,6 @@ this.isAbstract() === Documentation - * Groovy Language Documentation - Keywords - https://groovy-lang.org/syntax.html#_keywords[Official Groovy documentation explaining reserved keywords and their usage] + * Groovy documentation - https://groovy-lang.org/syntax.html#_keywords[Keywords] - * Groovy Language Documentation - Identifiers - https://groovy-lang.org/syntax.html#_identifiers[Official documentation on identifiers and quoted identifiers in Groovy] + * Groovy documentation - https://groovy-lang.org/syntax.html#_identifiers[Identifiers]