-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow unit return type for suspend functions #293
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package slack.lint.annotations | ||
|
||
/** | ||
* Annotation to allow suspend Retrofit functions to return Unit. | ||
* | ||
* When applied to a suspend Retrofit function, this annotation permits the function | ||
* to have a return type of Unit, which otherwise will be flagged as an issue. | ||
*/ | ||
@Target(AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class AllowUnitResponse |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,11 +39,15 @@ class RetrofitUsageDetector : Detector(), SourceCodeScanner { | |
HTTP_ANNOTATIONS.firstNotNullOfOrNull { node.findAnnotation(it) } ?: return | ||
|
||
val returnType = node.safeReturnType(context) | ||
if ( | ||
returnType == null || | ||
returnType == PsiTypes.voidType() || | ||
returnType.canonicalText == "kotlin.Unit" | ||
) { | ||
val isSuspend = context.evaluator.isSuspend(node) | ||
val hasAllowUnitResponseAnnotation = node.findAnnotation("slack.lint.annotations.AllowUnitResponse") != null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is false we should be able to skip needing the isVoidOrUnitReturnType variable right? Let's make that logic lazier |
||
val isVoidOrUnitReturnType = returnType == PsiTypes.voidType() || | ||
returnType?.canonicalText == "kotlin.Unit" | ||
val shouldDisallowUnitReturnType = !isSuspend || !hasAllowUnitResponseAnnotation | ||
val hasInvalidUnitReturnTypeUsage = returnType == null || | ||
(isVoidOrUnitReturnType && shouldDisallowUnitReturnType) | ||
|
||
if (hasInvalidUnitReturnTypeUsage) { | ||
node.report( | ||
"Retrofit endpoints should return something other than Unit/void.", | ||
context.getNameLocation(node), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,31 @@ package slack.lint.retrofit | |
|
||
import com.android.tools.lint.detector.api.Detector | ||
import org.junit.Test | ||
import retrofit2.http.GET | ||
import slack.lint.BaseSlackLintTest | ||
|
||
class RetrofitUsageDetectorTest : BaseSlackLintTest() { | ||
|
||
private companion object { | ||
private val allowUnitResponse = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed other examples of how to make the test recognise this import. Don't know if there is a better way. |
||
kotlin( | ||
""" | ||
package slack.lint.annotations | ||
|
||
/** | ||
* Annotation to allow suspend Retrofit functions to return Unit. | ||
* | ||
* When applied to a suspend Retrofit function, this annotation permits the function | ||
* to have a return type of Unit, which otherwise will be flagged as an issue. | ||
*/ | ||
@Target(AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class AllowUnitResponse | ||
""" | ||
) | ||
.indented() | ||
} | ||
|
||
private val retrofit2Jar = retrofit2Jar() | ||
|
||
override fun getDetector(): Detector = RetrofitUsageDetector() | ||
|
@@ -132,21 +153,34 @@ class RetrofitUsageDetectorTest : BaseSlackLintTest() { | |
lint() | ||
.files( | ||
retrofit2Jar, | ||
allowUnitResponse, | ||
kotlin( | ||
""" | ||
package test | ||
|
||
import retrofit2.http.GET | ||
import slack.lint.annotations.AllowUnitResponse | ||
|
||
interface Example { | ||
@GET("/") | ||
fun unitMethod() | ||
|
||
@AllowUnitResponse | ||
@PUT("/") | ||
suspend fun suspendUnitMethodAllowUnitResponse() | ||
|
||
@GET("/") | ||
fun unitMethodExplicit(): Unit | ||
|
||
@GET("/") | ||
suspend fun suspendUnitMethod() | ||
|
||
@AllowUnitResponse | ||
@DELETE("/") | ||
suspend fun suspendUnitMethodExplicitAllowUnitResponse(): Unit | ||
|
||
@GET("/") | ||
fun unitMethodExplicit(): Unit | ||
suspend fun suspendUnitMethodExplicit(): Unit | ||
} | ||
""" | ||
) | ||
|
@@ -155,16 +189,19 @@ class RetrofitUsageDetectorTest : BaseSlackLintTest() { | |
.run() | ||
.expect( | ||
""" | ||
src/test/Example.kt:7: Error: Retrofit endpoints should return something other than Unit/void. [RetrofitUsage] | ||
src/test/Example.kt:8: Error: Retrofit endpoints should return something other than Unit/void. [RetrofitUsage] | ||
fun unitMethod() | ||
~~~~~~~~~~ | ||
src/test/Example.kt:10: Error: Retrofit endpoints should return something other than Unit/void. [RetrofitUsage] | ||
suspend fun suspendUnitMethod() | ||
~~~~~~~~~~~~~~~~~ | ||
src/test/Example.kt:13: Error: Retrofit endpoints should return something other than Unit/void. [RetrofitUsage] | ||
src/test/Example.kt:15: Error: Retrofit endpoints should return something other than Unit/void. [RetrofitUsage] | ||
fun unitMethodExplicit(): Unit | ||
~~~~~~~~~~~~~~~~~~ | ||
3 errors, 0 warnings | ||
src/test/Example.kt:18: Error: Retrofit endpoints should return something other than Unit/void. [RetrofitUsage] | ||
suspend fun suspendUnitMethod() | ||
~~~~~~~~~~~~~~~~~ | ||
src/test/Example.kt:25: Error: Retrofit endpoints should return something other than Unit/void. [RetrofitUsage] | ||
suspend fun suspendUnitMethodExplicit(): Unit | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
4 errors, 0 warnings | ||
""" | ||
.trimIndent() | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marked as Binary retention as we don't need this at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it just be SOURCE?