KT-16264 Forbid usage of _ without backticks
Forbid underscore-only (_, __, ___, ...) names as callees and as types. If CHECK_TYPE directive is on, filter out UNDERSCORE_USAGE_WITHOUT_BACKTICKS messages.
This commit is contained in:
+2
@@ -58,6 +58,8 @@ With that, an exact type of an expression can be checked in the following way:
|
||||
expr checkType { _<A>() }
|
||||
}
|
||||
|
||||
`CHECK_TYPE` directive also disables `UNDERSCORE_USAGE_WITHOUT_BACKTICKS` diagnostics output.
|
||||
|
||||
#### Usage:
|
||||
|
||||
// !CHECK_TYPE
|
||||
|
||||
+7
-7
@@ -5,14 +5,14 @@
|
||||
@___("") data class Pair(val x: Int, val y: Int)
|
||||
|
||||
class <!UNDERSCORE_IS_RESERVED!>_<!><<!UNDERSCORE_IS_RESERVED!>________<!>>
|
||||
val <!UNDERSCORE_IS_RESERVED!>______<!> = _<Int>()
|
||||
val <!UNDERSCORE_IS_RESERVED!>______<!> = <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!><Int>()
|
||||
|
||||
fun <!UNDERSCORE_IS_RESERVED!>__<!>(<!UNDERSCORE_IS_RESERVED!>___<!>: Int, y: _<Int>?): Int {
|
||||
val (_, <!UNUSED_VARIABLE!>z<!>) = Pair(___ - 1, 42)
|
||||
val (x, <!UNDERSCORE_IS_RESERVED!>__________<!>) = Pair(___ - 1, 42)
|
||||
fun <!UNDERSCORE_IS_RESERVED!>__<!>(<!UNDERSCORE_IS_RESERVED!>___<!>: Int, y: <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!><Int>?): Int {
|
||||
val (_, <!UNUSED_VARIABLE!>z<!>) = Pair(<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>___<!> - 1, 42)
|
||||
val (x, <!UNDERSCORE_IS_RESERVED!>__________<!>) = Pair(<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>___<!> - 1, 42)
|
||||
val <!UNDERSCORE_IS_RESERVED!>____<!> = x
|
||||
// in backquotes: allowed
|
||||
val `_` = __________
|
||||
val `_` = <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__________<!>
|
||||
|
||||
val q = fun(_: Int, <!UNDERSCORE_IS_RESERVED, UNUSED_PARAMETER!>__<!>: Int) {}
|
||||
q(1, 2)
|
||||
@@ -21,7 +21,7 @@ fun <!UNDERSCORE_IS_RESERVED!>__<!>(<!UNDERSCORE_IS_RESERVED!>___<!>: Int, y: _<
|
||||
|
||||
fun localFun(<!UNDERSCORE_IS_RESERVED!>_<!>: String) = 1
|
||||
|
||||
<!UNDERSCORE_IS_RESERVED!>__<!>@ return if (y != null) __(____, y) else __(`_`, ______)
|
||||
<!UNDERSCORE_IS_RESERVED!>__<!>@ return if (y != null) <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>(<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>____<!>, y) else <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>(`_`, <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>______<!>)
|
||||
}
|
||||
|
||||
|
||||
@@ -42,5 +42,5 @@ fun oneUnderscore(<!UNDERSCORE_IS_RESERVED!>_<!>: Int) {}
|
||||
|
||||
fun doIt(f: (Any?) -> Any?) = f(null)
|
||||
|
||||
val something = doIt { <!UNDERSCORE_IS_RESERVED!>__<!> -> __ }
|
||||
val something = doIt { <!UNDERSCORE_IS_RESERVED!>__<!> -> <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!> }
|
||||
val something2 = doIt { _ -> 1 }
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package test
|
||||
|
||||
annotation class `__`(val value: String)
|
||||
|
||||
@<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>("") class TestAnn
|
||||
@`__`("") class TestAnn2
|
||||
@@ -0,0 +1,26 @@
|
||||
package
|
||||
|
||||
package test {
|
||||
|
||||
@test.__(value = "") public final class TestAnn {
|
||||
public constructor TestAnn()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
@test.__(value = "") public final class TestAnn2 {
|
||||
public constructor TestAnn2()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public final annotation class __ : kotlin.Annotation {
|
||||
public constructor __(/*0*/ value: kotlin.String)
|
||||
public final val value: kotlin.String
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// !DIAGNOSTICS: -DEPRECATION -TOPLEVEL_TYPEALIASES_ONLY
|
||||
|
||||
fun test(`_`: Int) {
|
||||
<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!> + 1
|
||||
`_` + 1
|
||||
}
|
||||
|
||||
fun `__`() {}
|
||||
|
||||
fun testCall() {
|
||||
<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>()
|
||||
`__`()
|
||||
}
|
||||
|
||||
val testCallableRef = ::<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>
|
||||
val testCallableRef2 = ::`__`
|
||||
|
||||
|
||||
object Host {
|
||||
val `_` = 42
|
||||
object `__` {
|
||||
val bar = 4
|
||||
}
|
||||
}
|
||||
|
||||
val testQualified = Host.<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!>
|
||||
val testQualified2 = Host.`_`
|
||||
|
||||
object `___` {
|
||||
val test = 42
|
||||
}
|
||||
|
||||
val testQualifier = <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>___<!>.test
|
||||
val testQualifier2 = `___`.test
|
||||
val testQualifier3 = Host.<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>.bar
|
||||
val testQualifier4 = Host.`__`.bar
|
||||
|
||||
fun testCallableRefLHSValue(`_`: Any) = <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!>::toString
|
||||
fun testCallableRefLHSValue2(`_`: Any) = `_`::toString
|
||||
|
||||
val testCallableRefLHSObject = <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>___<!>::toString
|
||||
val testCallableRefLHSObject2 = `___`::toString
|
||||
@@ -0,0 +1,41 @@
|
||||
package
|
||||
|
||||
public val testCallableRef: kotlin.reflect.KFunction0<kotlin.Unit>
|
||||
public val testCallableRef2: kotlin.reflect.KFunction0<kotlin.Unit>
|
||||
public val testCallableRefLHSObject: kotlin.reflect.KFunction0<kotlin.String>
|
||||
public val testCallableRefLHSObject2: kotlin.reflect.KFunction0<kotlin.String>
|
||||
public val testQualified: kotlin.Int = 42
|
||||
public val testQualified2: kotlin.Int = 42
|
||||
public val testQualifier: kotlin.Int = 42
|
||||
public val testQualifier2: kotlin.Int = 42
|
||||
public val testQualifier3: kotlin.Int = 4
|
||||
public val testQualifier4: kotlin.Int = 4
|
||||
public fun __(): kotlin.Unit
|
||||
public fun test(/*0*/ _: kotlin.Int): kotlin.Unit
|
||||
public fun testCall(): kotlin.Unit
|
||||
public fun testCallableRefLHSValue(/*0*/ _: kotlin.Any): kotlin.reflect.KFunction0<kotlin.String>
|
||||
public fun testCallableRefLHSValue2(/*0*/ _: kotlin.Any): kotlin.reflect.KFunction0<kotlin.String>
|
||||
|
||||
public object Host {
|
||||
private constructor Host()
|
||||
public final val _: kotlin.Int = 42
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public object __ {
|
||||
private constructor __()
|
||||
public final val bar: kotlin.Int = 4
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
|
||||
public object ___ {
|
||||
private constructor ___()
|
||||
public final val test: kotlin.Int = 42
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class `___` {
|
||||
class `____`
|
||||
}
|
||||
|
||||
val testCallableRefLHSType = <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>___<!>::toString
|
||||
val testCallableRefLHSType2 = `___`::toString
|
||||
|
||||
val testClassLiteralLHSType = <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>___<!>::class
|
||||
val testClassLiteralLHSType2 = `___`::class
|
||||
|
||||
val tesLHSTypeFQN = `___`.<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>____<!>::class
|
||||
@@ -0,0 +1,21 @@
|
||||
package
|
||||
|
||||
public val tesLHSTypeFQN: kotlin.reflect.KClass<___.____>
|
||||
public val testCallableRefLHSType: kotlin.reflect.KFunction1<___, kotlin.String>
|
||||
public val testCallableRefLHSType2: kotlin.reflect.KFunction1<___, kotlin.String>
|
||||
public val testClassLiteralLHSType: kotlin.reflect.KClass<___>
|
||||
public val testClassLiteralLHSType2: kotlin.reflect.KClass<___>
|
||||
|
||||
public final class ___ {
|
||||
public constructor ___()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public final class ____ {
|
||||
public constructor ____()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// !DIAGNOSTICS: -DEPRECATION -TOPLEVEL_TYPEALIASES_ONLY
|
||||
|
||||
class `_`<`__`> {
|
||||
fun testTypeArgument(x: List<<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>__<!>>) = x
|
||||
fun testTypeArgument2(x: List<`__`>) = x
|
||||
}
|
||||
|
||||
fun <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!><Any>.testTypeConstructor() {}
|
||||
fun `_`<Any>.testTypeConstructor2() {}
|
||||
|
||||
val testConstructor = <!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!><Any>()
|
||||
val testConstructor2 = `_`<Any>()
|
||||
@@ -0,0 +1,15 @@
|
||||
package
|
||||
|
||||
public val testConstructor: _<kotlin.Any>
|
||||
public val testConstructor2: _<kotlin.Any>
|
||||
public fun _<kotlin.Any>.testTypeConstructor(): kotlin.Unit
|
||||
public fun _<kotlin.Any>.testTypeConstructor2(): kotlin.Unit
|
||||
|
||||
public final class _</*0*/ __> {
|
||||
public constructor _</*0*/ __>()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public final fun testTypeArgument(/*0*/ x: kotlin.collections.List<__>): kotlin.collections.List<__>
|
||||
public final fun testTypeArgument2(/*0*/ x: kotlin.collections.List<__>): kotlin.collections.List<__>
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
object Host {
|
||||
val `____` = { -> }
|
||||
fun testFunTypeVal() {
|
||||
<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>____<!>()
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package
|
||||
|
||||
public object Host {
|
||||
private constructor Host()
|
||||
public final val ____: () -> kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public final fun testFunTypeVal(): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
Vendored
+2
-2
@@ -38,11 +38,11 @@ fun test() {
|
||||
|
||||
val (<!REDECLARATION!>`_`<!>, z) = A()
|
||||
|
||||
foo(_, z)
|
||||
foo(<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!>, z)
|
||||
|
||||
val (_, <!NAME_SHADOWING, REDECLARATION!>`_`<!>) = A()
|
||||
|
||||
foo(<!TYPE_MISMATCH!>_<!>, y)
|
||||
foo(<!UNDERSCORE_USAGE_WITHOUT_BACKTICKS, TYPE_MISMATCH!>_<!>, y)
|
||||
|
||||
val (<!UNUSED_VARIABLE!>unused<!>, _) = A()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user