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
26 changes: 26 additions & 0 deletions rules/S8316/groovy/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"title": "Maps should use explicit emptiness checks instead of boolean conversion",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5 min"
},
"tags": [
"confusing"
],
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-8316",
"sqKey": "S8316",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "BLOCKER"
},
"attribute": "CLEAR"
}
}
56 changes: 56 additions & 0 deletions rules/S8316/groovy/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
This rule raises an issue when `Map.asBoolean()` is called explicitly or when a map is used in a boolean context through implicit conversion.

== Why is this an issue?

Using `Map.asBoolean()` or implicit boolean conversion of maps can be misleading and confusing for developers. These methods only check whether the map is empty, not whether it contains meaningful or valid data.

The confusion arises because:

* The method name `asBoolean()` doesn't clearly indicate it's checking emptiness
* Implicit boolean conversion in conditional statements can be misunderstood
* Developers might expect these methods to validate the content quality, not just presence
* Code reviewers and maintainers may misinterpret the intent

For example, a map containing only null values or placeholder data would still return `true` with `asBoolean()`, even though the data might not be meaningful for the application's logic.

Explicit methods like `isEmpty()` or `size()` checks make the code's intent crystal clear and improve readability.

=== What is the potential impact?

Using implicit boolean conversion can lead to logical errors where the code appears to check for meaningful data but only verifies the map isn't empty. This can cause bugs when maps contain placeholder or invalid data that still passes the boolean check.

The unclear intent also reduces code maintainability, as future developers may misunderstand what the condition is actually testing.

== How to fix it

Replace explicit calls to `asBoolean()` with clear emptiness checks using `isEmpty()` or `size()`.

=== Code examples

==== Noncompliant code example

[source,groovy,diff-id=1,diff-type=noncompliant]
----
def map = [:]
if (map.asBoolean()) { // Noncompliant
println "Map has data"
}
----

==== Compliant solution

[source,groovy,diff-id=1,diff-type=compliant]
----
def map = [:]
if (!map.isEmpty()) {
println "Map has data"
}
----

== Resources

=== Documentation

* Groovy Map Documentation - http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Map.html[Official Groovy documentation for Map methods including asBoolean()]

* Groovy Truth - https://groovy-lang.org/semantics.html#Groovy-Truth[Documentation explaining Groovy's truth evaluation rules]
2 changes: 2 additions & 0 deletions rules/S8316/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}