From a7522a3ca1f934497df1f3b9f92de5c867cd66a0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:17:17 +0200 Subject: [PATCH] Create rule S6810: Async methods should return void or Future (#3243) --- rules/S6810/java/metadata.json | 23 ++++++++++++++++ rules/S6810/java/rule.adoc | 48 ++++++++++++++++++++++++++++++++++ rules/S6810/metadata.json | 2 ++ 3 files changed, 73 insertions(+) create mode 100644 rules/S6810/java/metadata.json create mode 100644 rules/S6810/java/rule.adoc create mode 100644 rules/S6810/metadata.json diff --git a/rules/S6810/java/metadata.json b/rules/S6810/java/metadata.json new file mode 100644 index 0000000000..bee23a171a --- /dev/null +++ b/rules/S6810/java/metadata.json @@ -0,0 +1,23 @@ +{ + "title": "Async methods should return void or Future", + "type": "BUG", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-6810", + "sqKey": "S6810", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "RELIABILITY": "MEDIUM" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S6810/java/rule.adoc b/rules/S6810/java/rule.adoc new file mode 100644 index 0000000000..99e277e5ec --- /dev/null +++ b/rules/S6810/java/rule.adoc @@ -0,0 +1,48 @@ +== Why is this an issue? + +The Spring framework provides the annotation `Async` to mark a method (or all methods of a type) as a candidate for asynchronous execution. + +Asynchronous methods do not necessarily, by their nature, return the result of their calculation immediately. +Hence, it is unexpected and in clear breach of the `Async` contract for such methods to have a return type that is neither `void` nor a `Future` type. + +== How to fix it + +Use `void` as the return type if the method is not expected to return a result. +Otherwise, a `Future` should be returned, allowing the caller to retrieve the result once it is ready. +It is permitted to return more specific subtypes that inherit from `Future`. + +=== Code examples + +==== Noncompliant code example + +[source,java,diff-id=1,diff-type=noncompliant] +---- +@Async +public String asyncMethod() { + ... +} +---- + +==== Compliant solution + +[source,java,diff-id=1,diff-type=compliant] +---- +@Async +public Future asyncMethod() { + ... +} +---- + +Alternatively, if the method does not need to return a result: + +[source,java] +---- +@Async +public void asyncMethod() { + ... +} +---- + +== Resources +=== Documentation +* Spring Framework Documentation - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Async.html[Annotation Interface Async] diff --git a/rules/S6810/metadata.json b/rules/S6810/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S6810/metadata.json @@ -0,0 +1,2 @@ +{ +}