Skip to content

Commit

Permalink
Create rule S7050: Void functions should not return null (avoid_retur…
Browse files Browse the repository at this point in the history
…ning_null_for_void)

Co-authored-by: antonioaversa <[email protected]>
  • Loading branch information
github-actions[bot] and antonioaversa authored Aug 28, 2024
1 parent 689d026 commit 9d18621
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
23 changes: 23 additions & 0 deletions rules/S7050/dart/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"title": "Void functions should not return null",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "1min"
},
"tags": [
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-7050",
"sqKey": "S7050",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CLEAR"
}
}
71 changes: 71 additions & 0 deletions rules/S7050/dart/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Functions and methods with `void` return type should not return `null`.

== Why is this an issue?

Dart allows to `return null;` from a function or a method with `void` return type. That happens because, from a technical standpoint, unlike other languages like Java, every function in Dart returns a value: if you don't return a value, `null` will be returned.

From a semantical standpoint, however, this can be misleading, as it may suggest that the function does return an intended value, which is not the case.

It is better to consistently use `return;` and make it clear that in fact the function does not return any intended value.

== How to fix it

Replace `return null;` with `return;`.

=== Code examples

==== Noncompliant code example

[source,dart,diff-id=1,diff-type=noncompliant]
----
void logIf(bool condition, String message) {
if (!condition) {
return null; // Noncompliant
}
print(message);
}
----

==== Compliant solution

[source,dart,diff-id=1,diff-type=compliant]
----
void logIf(bool condition, String message) {
if (!condition) {
return;
}
print(message);
}
----

== Resources

=== Documentation

* Dart Docs - https://dart.dev/tools/linter-rules/avoid_returning_null_for_void[Dart Linter rule - avoid_returning_null_for_void]


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

* Don't return 'null' from a function with a return type of 'void'.
* Don't return 'null' from a method with a return type of 'void'.

=== Highlighting

* In the case of a function or method with body: the `return null;` expression, including `;`.
* In the case of an arrow function or an arrow method: the `=> null;` expression, including `;`.


'''
== Comments And Links
(visible only on this page)

endif::env-github,rspecator-view[]
2 changes: 2 additions & 0 deletions rules/S7050/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 9d18621

Please sign in to comment.