Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
f36d6ce5
K
kotlin
项目概览
硅谷海盗
/
kotlin
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kotlin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f36d6ce5
编写于
2月 05, 2015
作者:
A
Andrey Breslav
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reporting USELESS_ELVIS and UNNECESSARY_NOT_NULL_ASSERTION on Java's @NotNull values
上级
30f5c1b9
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
257 addition
and
5 deletion
+257
-5
compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmCheckerProvider.kt
.../jetbrains/kotlin/load/kotlin/KotlinJvmCheckerProvider.kt
+38
-3
compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java
...otlin/types/expressions/BasicExpressionTypingVisitor.java
+16
-2
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/elvis.kt
...gnostics/tests/platformTypes/nullabilityWarnings/elvis.kt
+45
-0
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/elvis.txt
...nostics/tests/platformTypes/nullabilityWarnings/elvis.txt
+3
-0
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertion.kt
...sts/platformTypes/nullabilityWarnings/notNullAssertion.kt
+43
-0
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertion.txt
...ts/platformTypes/nullabilityWarnings/notNullAssertion.txt
+3
-0
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertionInCall.kt
...atformTypes/nullabilityWarnings/notNullAssertionInCall.kt
+30
-0
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertionInCall.txt
...tformTypes/nullabilityWarnings/notNullAssertionInCall.txt
+12
-0
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/uselessElvisInCall.kt
...s/platformTypes/nullabilityWarnings/uselessElvisInCall.kt
+31
-0
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/uselessElvisInCall.txt
.../platformTypes/nullabilityWarnings/uselessElvisInCall.txt
+12
-0
compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java
...etbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java
+24
-0
未找到文件。
compiler/frontend.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmCheckerProvider.kt
浏览文件 @
f36d6ce5
...
...
@@ -59,6 +59,9 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
import
org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import
org.jetbrains.kotlin.resolve.calls.context.CallResolutionContext
import
org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability
import
org.jetbrains.kotlin.psi.JetPostfixExpression
import
org.jetbrains.kotlin.psi.JetBinaryExpression
import
org.jetbrains.kotlin.resolve.BindingContext
public
object
KotlinJvmCheckerProvider
:
AdditionalCheckerProvider
(
annotationCheckers
=
listOf
(
PlatformStaticAnnotationChecker
(),
LocalFunInlineChecker
(),
ReifiedTypeParameterAnnotationChecker
(),
NativeFunChecker
()),
...
...
@@ -193,6 +196,39 @@ public class JavaNullabilityWarningsChecker : AdditionalTypeChecker {
actualMayBeNull
->
c
.
trace
.
report
(
ErrorsJvm
.
NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS
.
on
(
expression
,
expectedMustNotBeNull
,
actualMayBeNull
))
}
when
(
expression
)
{
is
JetPostfixExpression
->
if
(
expression
.
getOperationToken
()
==
JetTokens
.
EXCLEXCL
)
{
val
baseExpression
=
expression
.
getBaseExpression
()
val
baseExpressionType
=
c
.
trace
.
get
(
BindingContext
.
EXPRESSION_TYPE
,
baseExpression
)
?:
return
warnIfNotNull
(
DataFlowValueFactory
.
createDataFlowValue
(
baseExpression
,
baseExpressionType
,
c
.
trace
.
getBindingContext
()),
c
)
{
c
.
trace
.
report
(
Errors
.
UNNECESSARY_NOT_NULL_ASSERTION
.
on
(
expression
.
getOperationReference
(),
baseExpressionType
))
}
}
is
JetBinaryExpression
->
if
(
expression
.
getOperationToken
()
==
JetTokens
.
ELVIS
)
{
val
baseExpression
=
expression
.
getLeft
()
val
baseExpressionType
=
c
.
trace
.
get
(
BindingContext
.
EXPRESSION_TYPE
,
baseExpression
)
?:
return
warnIfNotNull
(
DataFlowValueFactory
.
createDataFlowValue
(
baseExpression
,
baseExpressionType
,
c
.
trace
.
getBindingContext
()),
c
)
{
c
.
trace
.
report
(
Errors
.
USELESS_ELVIS
.
on
(
expression
.
getOperationReference
(),
baseExpressionType
))
}
}
}
}
private
fun
warnIfNotNull
(
dataFlowValue
:
DataFlowValue
,
c
:
ResolutionContext
<
*
>,
reportWarning
:
()
->
Unit
)
{
if
(
c
.
dataFlowInfo
.
getNullability
(
dataFlowValue
).
canBeNull
()
&&
dataFlowValue
.
getType
().
mustNotBeNull
()
==
NullabilityInformationSource
.
JAVA
)
{
reportWarning
()
}
}
override
fun
checkReceiver
(
...
...
@@ -224,10 +260,9 @@ public class JavaNullabilityWarningsChecker : AdditionalTypeChecker {
}
}
else
{
if
(
c
.
dataFlowInfo
.
getNullability
(
dataFlowValue
).
canBeNull
()
&&
receiverArgument
.
getType
().
mustNotBeNull
()
==
NullabilityInformationSource
.
JAVA
)
{
// TODO: Compiler bug
warnIfNotNull
(
dataFlowValue
,
c
as
ResolutionContext
<
*
>
)
{
c
.
trace
.
report
(
Errors
.
UNNECESSARY_SAFE_CALL
.
on
(
c
.
call
.
getCallOperationNode
().
getPsi
(),
receiverArgument
.
getType
()))
}
}
}
...
...
compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java
浏览文件 @
f36d6ce5
...
...
@@ -74,6 +74,7 @@ import static org.jetbrains.kotlin.lexer.JetTokens.AS_KEYWORD;
import
static
org
.
jetbrains
.
kotlin
.
lexer
.
JetTokens
.
AS_SAFE
;
import
static
org
.
jetbrains
.
kotlin
.
resolve
.
BindingContext
.*;
import
static
org
.
jetbrains
.
kotlin
.
resolve
.
DescriptorUtils
.
getStaticNestedClassesScope
;
import
static
org
.
jetbrains
.
kotlin
.
resolve
.
calls
.
context
.
ContextDependency
.
DEPENDENT
;
import
static
org
.
jetbrains
.
kotlin
.
resolve
.
calls
.
context
.
ContextDependency
.
INDEPENDENT
;
import
static
org
.
jetbrains
.
kotlin
.
resolve
.
calls
.
smartcasts
.
DataFlowValueFactory
.
createDataFlowValue
;
import
static
org
.
jetbrains
.
kotlin
.
resolve
.
scopes
.
receivers
.
ReceiverValue
.
NO_RECEIVER
;
...
...
@@ -792,7 +793,17 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
DataFlowValue
value
=
createDataFlowValue
(
baseExpression
,
baseType
,
context
.
trace
.
getBindingContext
());
dataFlowInfo
=
dataFlowInfo
.
disequate
(
value
,
DataFlowValue
.
NULL
);
}
return
JetTypeInfo
.
create
(
TypeUtils
.
makeNotNullable
(
baseType
),
dataFlowInfo
);
// The call to checkType() is only needed here to execute additionalTypeCheckers, hence the NO_EXPECTED_TYPE
JetType
resultingType
=
TypeUtils
.
makeNotNullable
(
baseType
);
if
(
context
.
contextDependency
==
DEPENDENT
)
{
return
JetTypeInfo
.
create
(
resultingType
,
dataFlowInfo
);
}
return
DataFlowUtils
.
checkType
(
resultingType
,
expression
,
context
.
replaceExpectedType
(
NO_EXPECTED_TYPE
),
dataFlowInfo
);
}
@Override
...
...
@@ -1083,7 +1094,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if
(!
TypeUtils
.
isNullableType
(
rightType
)
&&
TypeUtils
.
isNullableType
(
type
))
{
type
=
TypeUtils
.
makeNotNullable
(
type
);
}
return
JetTypeInfo
.
create
(
type
,
dataFlowInfo
);
if
(
context
.
contextDependency
==
DEPENDENT
)
{
return
JetTypeInfo
.
create
(
type
,
dataFlowInfo
);
}
return
DataFlowUtils
.
checkType
(
type
,
expression
,
context
,
dataFlowInfo
);
}
@NotNull
...
...
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/elvis.kt
0 → 100644
浏览文件 @
f36d6ce5
// !DIAGNOSTICS: -UNUSED_VARIABLE
// FILE: p/J.java
package
p;
import
org.jetbrains.annotations.*;
public
class
J
{
@NotNull
public
static
J
staticNN
;
@Nullable
public
static
J
staticN
;
public
static
J
staticJ
;
}
// FILE: k.kt
import
p.*
fun
test
()
{
// @NotNull platform type
val
platformNN
=
J
.
staticNN
// @Nullable platform type
val
platformN
=
J
.
staticN
// platform type with no annotation
val
platformJ
=
J
.
staticJ
val
v0
=
platformNN
<!
USELESS_ELVIS
!>
?:
<!>
J
()
platformNN
<!
USELESS_ELVIS
!>
?:
<!>
J
()
platformN
?:
J
()
platformJ
?:
J
()
if
(
platformNN
!=
null
)
{
<!
USELESS_ELVIS
!>
platformNN
<!>
?:
J
()
}
if
(
platformN
!=
null
)
{
<!
USELESS_ELVIS
!>
platformN
<!>
?:
J
()
}
if
(
platformJ
!=
null
)
{
<!
USELESS_ELVIS
!>
platformJ
<!>
?:
J
()
}
}
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/elvis.txt
0 → 100644
浏览文件 @
f36d6ce5
package
internal fun test(): kotlin.Unit
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertion.kt
0 → 100644
浏览文件 @
f36d6ce5
// !DIAGNOSTICS: -UNUSED_EXPRESSION -SENSELESS_COMPARISON -UNUSED_PARAMETER
// FILE: p/J.java
package
p;
import
org.jetbrains.annotations.*;
public
class
J
{
@NotNull
public
static
J
staticNN
;
@Nullable
public
static
J
staticN
;
public
static
J
staticJ
;
}
// FILE: k.kt
import
p.*
fun
test
()
{
// @NotNull platform type
val
platformNN
=
J
.
staticNN
// @Nullable platform type
val
platformN
=
J
.
staticN
// platform type with no annotation
val
platformJ
=
J
.
staticJ
if
(
platformNN
!=
null
)
{
platformNN
<!
UNNECESSARY_NOT_NULL_ASSERTION
!>
!!
<!>
}
if
(
platformN
!=
null
)
{
platformN
<!
UNNECESSARY_NOT_NULL_ASSERTION
!>
!!
<!>
}
if
(
platformJ
!=
null
)
{
platformJ
<!
UNNECESSARY_NOT_NULL_ASSERTION
!>
!!
<!>
}
platformNN
<!
UNNECESSARY_NOT_NULL_ASSERTION
!>
!!
<!>
platformN
!!
platformJ
!!
}
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertion.txt
0 → 100644
浏览文件 @
f36d6ce5
package
internal fun test(): kotlin.Unit
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertionInCall.kt
0 → 100644
浏览文件 @
f36d6ce5
// !DIAGNOSTICS: -UNUSED_PARAMETER
// FILE: p/J.java
package
p;
import
org.jetbrains.annotations.*;
public
class
J
{
@NotNull
public
static
J
staticNN
;
}
// FILE: k.kt
import
p.*
fun
test
()
{
// @NotNull platform type
val
platformNN
=
J
.
staticNN
foo
(
platformNN
<!
UNNECESSARY_NOT_NULL_ASSERTION
!>
!!
<!>)
val
bar
=
Bar
()
bar
(
platformNN
<!
UNNECESSARY_NOT_NULL_ASSERTION
!>
!!
<!>)
}
fun
foo
(
a
:
Any
)
{}
class
Bar
{
fun
invoke
(
a
:
Any
)
{}
}
\ No newline at end of file
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertionInCall.txt
0 → 100644
浏览文件 @
f36d6ce5
package
internal fun foo(/*0*/ a: kotlin.Any): kotlin.Unit
internal fun test(): kotlin.Unit
internal final class Bar {
public constructor Bar()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal final fun invoke(/*0*/ a: kotlin.Any): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/uselessElvisInCall.kt
0 → 100644
浏览文件 @
f36d6ce5
// !DIAGNOSTICS: -UNUSED_PARAMETER
// FILE: p/J.java
package
p;
import
org.jetbrains.annotations.*;
public
class
J
{
@NotNull
public
static
J
staticNN
;
}
// FILE: k.kt
import
p.*
fun
test
()
{
// @NotNull platform type
val
platformNN
=
J
.
staticNN
foo
(
platformNN
<!
USELESS_ELVIS
!>
?:
<!>
""
)
val
bar
=
Bar
()
bar
(
platformNN
<!
USELESS_ELVIS
!>
?:
<!>
""
)
}
fun
foo
(
a
:
Any
)
{}
class
Bar
{
fun
invoke
(
a
:
Any
)
{}
}
\ No newline at end of file
compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/uselessElvisInCall.txt
0 → 100644
浏览文件 @
f36d6ce5
package
internal fun foo(/*0*/ a: kotlin.Any): kotlin.Unit
internal fun test(): kotlin.Unit
internal final class Bar {
public constructor Bar()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal final fun invoke(/*0*/ a: kotlin.Any): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java
浏览文件 @
f36d6ce5
...
...
@@ -8598,6 +8598,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("elvis.kt")
public void testElvis() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/elvis.kt");
doTest(fileName);
}
@TestMetadata("expectedType.kt")
public void testExpectedType() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/expectedType.kt");
...
...
@@ -8628,6 +8634,18 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("notNullAssertion.kt")
public void testNotNullAssertion() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertion.kt");
doTest(fileName);
}
@TestMetadata("notNullAssertionInCall.kt")
public void testNotNullAssertionInCall() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/notNullAssertionInCall.kt");
doTest(fileName);
}
@TestMetadata("passToJava.kt")
public void testPassToJava() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/passToJava.kt");
...
...
@@ -8651,6 +8669,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/throw.kt");
doTest(fileName);
}
@TestMetadata("uselessElvisInCall.kt")
public void testUselessElvisInCall() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/uselessElvisInCall.kt");
doTest(fileName);
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录