-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S7050: Void functions should not return null (avoid_retur…
…ning_null_for_void) Co-authored-by: antonioaversa <[email protected]>
- Loading branch information
1 parent
689d026
commit 9d18621
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |