-
-
Notifications
You must be signed in to change notification settings - Fork 821
HasPlatformType rule #1938
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
HasPlatformType rule #1938
Conversation
schalkms
left a comment
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.
Cool! This rule is beneficial for all library developers. I like it.
| * Platform types must be declared explicitly in public APIs to prevent unexpected errors. | ||
| * | ||
| * Based on code from Kotlin project: | ||
| * https://github.com/JetBrains/kotlin/blob/1.3.50/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt#L86-L107 |
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.
I'm not sure if we should really reference the source location.
Source code can be changed easily. Thus, this reference could become out of date.
It's better IMHO to include the license in our project.
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.
relates to #1917
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.
The url is fixed to v1.3.50 of Kotlin, so the link won't break.
detekt-rules/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/HasPlatformType.kt
Outdated
Show resolved
Hide resolved
| val callable = | ||
| bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this] as? CallableDescriptor ?: return false | ||
| if (!callable.visibility.isPublicAPI) return false | ||
| return callable.returnType?.isFlexible() ?: return false |
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.
What does isFlexible() check for?
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.
| import org.spekframework.spek2.Spek | ||
| import org.spekframework.spek2.style.specification.describe | ||
|
|
||
| object HasPlatformTypeSpec : Spek({ |
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.
Could we please make sure that the tests cover all branches (if possible)?
if (containingClassOrObject?.isLocal == true) return false
val callable = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this] as? CallableDescriptor ?: return false
if (!callable.visibility.isPublicAPI) return false
return callable.returnType?.isFlexible() ?: return falseCo-Authored-By: M Schalk <[email protected]>
* HasPlatformType rule * Merge ifs Co-Authored-By: M Schalk <[email protected]>
* HasPlatformType rule * Merge ifs Co-Authored-By: M Schalk <[email protected]>
Closes #556
Rule is quite conservative and picks up usage in public APIs only. Future enhancement could be to add a config option to detect implicit usage of platform types anywhere they're found.