Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@3flex
Copy link
Member

@3flex 3flex commented Sep 19, 2019

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.

Copy link
Member

@schalkms schalkms left a 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
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relates to #1917

Copy link
Member Author

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.

val callable =
bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this] as? CallableDescriptor ?: return false
if (!callable.visibility.isPublicAPI) return false
return callable.returnType?.isFlexible() ?: return false
Copy link
Member

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?

Copy link
Member Author

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({
Copy link
Member

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 false

Co-Authored-By: M Schalk <[email protected]>
@arturbosch arturbosch merged commit 0706bb3 into detekt:master Oct 4, 2019
@arturbosch arturbosch added this to the 1.1.0 milestone Oct 4, 2019
@3flex 3flex deleted the 556-fix branch October 4, 2019 23:38
smyachenkov pushed a commit to smyachenkov/detekt that referenced this pull request Dec 9, 2019
* HasPlatformType rule

* Merge ifs

Co-Authored-By: M Schalk <[email protected]>
smyachenkov pushed a commit to smyachenkov/detekt that referenced this pull request Dec 9, 2019
* HasPlatformType rule

* Merge ifs

Co-Authored-By: M Schalk <[email protected]>
@3flex 3flex restored the 556-fix branch July 29, 2021 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rule: Require explicit types when using platform types

3 participants