Code cleanup: get rid of unnecessary !! / ?. / as

This commit is contained in:
Mikhail Glukhikh
2015-11-11 15:50:24 +03:00
parent 7e8e4e9e1b
commit 2f4dbd2084
21 changed files with 33 additions and 33 deletions
@@ -309,7 +309,7 @@ public class KotlinCoreEnvironment private constructor(
if (ourApplicationEnvironment == null) return if (ourApplicationEnvironment == null) return
val environment = ourApplicationEnvironment val environment = ourApplicationEnvironment
ourApplicationEnvironment = null ourApplicationEnvironment = null
Disposer.dispose(environment!!.getParentDisposable()) Disposer.dispose(environment.getParentDisposable())
} }
} }
@@ -359,7 +359,7 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l
} }
val classParametersSubstitutor = TypeSubstitutor.create(substitutionMap) val classParametersSubstitutor = TypeSubstitutor.create(substitutionMap)
descriptor.getMethod = getMethod.substitute(classParametersSubstitutor)!! descriptor.getMethod = getMethod.substitute(classParametersSubstitutor)
descriptor.setMethod = setMethod?.substitute(classParametersSubstitutor) descriptor.setMethod = setMethod?.substitute(classParametersSubstitutor)
return descriptor return descriptor
} }
@@ -199,7 +199,7 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : BaseImportingSc
} }
val sourceFunctionSubstitutor = TypeSubstitutor.create(substitutionMap) val sourceFunctionSubstitutor = TypeSubstitutor.create(substitutionMap)
descriptor.sourceFunction = original.sourceFunction.substitute(sourceFunctionSubstitutor)!! descriptor.sourceFunction = original.sourceFunction.substitute(sourceFunctionSubstitutor)
return descriptor return descriptor
} }
@@ -98,12 +98,12 @@ public fun PsiElement.getParentOfTypesAndPredicate<T : PsiElement>(
while (element != null) { while (element != null) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
when { when {
(parentClasses.isEmpty() || parentClasses.any { parentClass -> parentClass.isInstance(element) }) && predicate(element!! as T) -> (parentClasses.isEmpty() || parentClasses.any { parentClass -> parentClass.isInstance(element) }) && predicate(element as T) ->
return element as T return element
element is PsiFile -> element is PsiFile ->
return null return null
else -> else ->
element = element!!.getParent() element = element.getParent()
} }
} }
@@ -52,7 +52,7 @@ fun createSynthesizedInvokes(functions: Collection<FunctionDescriptor>): Collect
fakeOverride fakeOverride
} }
result.add(synthesized.substitute(TypeSubstitutor.create(invoke.getDispatchReceiverParameter()!!.getType()))!!) result.add(synthesized.substitute(TypeSubstitutor.create(invoke.getDispatchReceiverParameter()!!.getType())))
} }
return result return result
@@ -189,7 +189,7 @@ class LazyOperationsLog(
}.appendQuoted() }.appendQuoted()
} }
o is ResolutionCandidate<*> -> DescriptorRenderer.COMPACT.render(o.getDescriptor()).appendQuoted() o is ResolutionCandidate<*> -> DescriptorRenderer.COMPACT.render(o.getDescriptor()).appendQuoted()
o is ResolutionTaskHolder<*, *> -> o.field<BasicCallResolutionContext>("basicCallResolutionContext").call.getCallElement().getDebugText()?.appendQuoted() o is ResolutionTaskHolder<*, *> -> o.field<BasicCallResolutionContext>("basicCallResolutionContext").call.getCallElement().getDebugText().appendQuoted()
} }
return sb.toString() return sb.toString()
} }
@@ -54,10 +54,10 @@ public abstract class AbstractCompileKotlinAgainstInlineKotlinTest : AbstractCom
catch (e: Throwable) { catch (e: Throwable) {
var result = "" var result = ""
if (factory1 != null) { if (factory1 != null) {
result += "FIRST: \n\n" + factory1!!.createText() result += "FIRST: \n\n" + factory1.createText()
} }
if (factory2 != null) { if (factory2 != null) {
result += "\n\nSECOND: \n\n" + factory2!!.createText() result += "\n\nSECOND: \n\n" + factory2.createText()
} }
System.out.println(result) System.out.println(result)
throw e throw e
@@ -73,7 +73,7 @@ public abstract class SelfTargetingIntention<TElement : KtElement>(
for (element in elementsToCheck) { for (element in elementsToCheck) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
if (elementType.isInstance(element) && isApplicableTo(element as TElement, offset)) { if (elementType.isInstance(element) && isApplicableTo(element as TElement, offset)) {
return element as TElement return element
} }
if (!allowCaretInsideElement(element) && element.getTextRange().containsInside(offset)) break if (!allowCaretInsideElement(element) && element.getTextRange().containsInside(offset)) break
} }
@@ -342,7 +342,7 @@ class ExpectedInfos(
val alreadyHasStar = argument.getSpreadElement() != null val alreadyHasStar = argument.getSpreadElement() != null
val varargElementType = parameter!!.varargElementType val varargElementType = parameter.varargElementType
if (varargElementType != null) { if (varargElementType != null) {
if (isFunctionLiteralArgument) return if (isFunctionLiteralArgument) return
@@ -356,16 +356,16 @@ class ExpectedInfos(
} }
val starOptions = if (!alreadyHasStar) ItemOptions.STAR_PREFIX else ItemOptions.DEFAULT val starOptions = if (!alreadyHasStar) ItemOptions.STAR_PREFIX else ItemOptions.DEFAULT
add(ExpectedInfo.createForArgument(parameter!!.getType(), expectedName, varargTail, argumentPositionData, starOptions)) add(ExpectedInfo.createForArgument(parameter.getType(), expectedName, varargTail, argumentPositionData, starOptions))
} }
else { else {
if (alreadyHasStar) return if (alreadyHasStar) return
val parameterType = if (useHeuristicSignatures) val parameterType = if (useHeuristicSignatures)
resolutionFacade.ideService<HeuristicSignatures>(). resolutionFacade.ideService<HeuristicSignatures>().
correctedParameterType(descriptor, parameter!!) ?: parameter!!.getType() correctedParameterType(descriptor, parameter) ?: parameter.getType()
else else
parameter!!.getType() parameter.getType()
if (isFunctionLiteralArgument) { if (isFunctionLiteralArgument) {
if (KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(parameterType)) { if (KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(parameterType)) {
@@ -154,7 +154,7 @@ sealed class KotlinFunctionInsertHandler : KotlinCallableInsertHandler() {
} }
if (insertLambda && lambdaInfo!!.explicitParameters) { if (insertLambda && lambdaInfo!!.explicitParameters) {
insertLambdaTemplate(context, TextRange(openingBracketOffset, closeBracketOffset!! + 1), lambdaInfo!!.lambdaType) insertLambdaTemplate(context, TextRange(openingBracketOffset, closeBracketOffset!! + 1), lambdaInfo.lambdaType)
return return
} }
@@ -170,7 +170,7 @@ public abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration>
return if (baseDeclarations.isNotEmpty()) { return if (baseDeclarations.isNotEmpty()) {
baseDeclarations.flatMap { baseDeclarations.flatMap {
val handler = (FindManager.getInstance(project) as FindManagerImpl).findUsagesManager.getFindUsagesHandler(it!!, true) val handler = (FindManager.getInstance(project) as FindManagerImpl).findUsagesManager.getFindUsagesHandler(it!!, true)
handler?.findReferencesToHighlight(it!!, searchScope) ?: emptyList() handler?.findReferencesToHighlight(it, searchScope) ?: emptyList()
} }
} }
else { else {
@@ -91,7 +91,7 @@ private fun KtExpression.getRelevantFunction(): KtFunction? {
} }
for (parent in parents) { for (parent in parents) {
if (InlineUtil.canBeInlineArgument(parent) && !InlineUtil.isInlinedArgument(parent as KtFunction, parent.analyze(), false)) { if (InlineUtil.canBeInlineArgument(parent) && !InlineUtil.isInlinedArgument(parent as KtFunction, parent.analyze(), false)) {
return parent as KtFunction return parent
} }
} }
return null return null
@@ -61,7 +61,7 @@ internal fun getTargetParentByQualifier(
if (qualifierDescriptor.fqName != file.getPackageFqName()) { if (qualifierDescriptor.fqName != file.getPackageFqName()) {
JavaPsiFacade.getInstance(project).findPackage(qualifierDescriptor.fqName.asString()) JavaPsiFacade.getInstance(project).findPackage(qualifierDescriptor.fqName.asString())
} }
else file as PsiElement else file as PsiElement // KT-9972
else -> else ->
null null
} ?: return null } ?: return null
@@ -144,7 +144,7 @@ fun KotlinType.hasTypeParametersToAdd(functionDescriptor: FunctionDescriptor, co
is FunctionDescriptor -> { is FunctionDescriptor -> {
val function = functionDescriptor.source.getPsi() as? KtFunction val function = functionDescriptor.source.getPsi() as? KtFunction
function?.bodyExpression?.getResolutionScope(context, function!!.getResolutionFacade()) function?.bodyExpression?.getResolutionScope(context, function.getResolutionFacade())
} }
else -> null else -> null
@@ -553,7 +553,7 @@ fun ExtractionGeneratorConfiguration.generateDeclaration(
if (!generatorOptions.inTempFile && defaultValue != null && descriptor.controlFlow.outputValueBoxer.boxingRequired && lastExpression!!.isMultiLine()) { if (!generatorOptions.inTempFile && defaultValue != null && descriptor.controlFlow.outputValueBoxer.boxingRequired && lastExpression!!.isMultiLine()) {
val varNameValidator = NewDeclarationNameValidator(body, lastExpression, NewDeclarationNameValidator.Target.VARIABLES) val varNameValidator = NewDeclarationNameValidator(body, lastExpression, NewDeclarationNameValidator.Target.VARIABLES)
val resultVal = KotlinNameSuggester.suggestNamesByType(defaultValue.valueType, varNameValidator, null).first() val resultVal = KotlinNameSuggester.suggestNamesByType(defaultValue.valueType, varNameValidator, null).first()
val newDecl = body.addBefore(psiFactory.createDeclaration("val $resultVal = ${lastExpression!!.text}"), lastExpression) as KtProperty val newDecl = body.addBefore(psiFactory.createDeclaration("val $resultVal = ${lastExpression.text}"), lastExpression) as KtProperty
body.addBefore(psiFactory.createNewLine(), lastExpression) body.addBefore(psiFactory.createNewLine(), lastExpression)
psiFactory.createExpression(resultVal) to newDecl.initializer!! psiFactory.createExpression(resultVal) to newDecl.initializer!!
} }
@@ -68,7 +68,7 @@ public class KotlinIntroduceParameterMethodUsageProcessor : IntroduceParameterMe
data.getParametersToRemove().toNativeArray().sortedDescending().forEach { changeInfo.removeParameter(it) } data.getParametersToRemove().toNativeArray().sortedDescending().forEach { changeInfo.removeParameter(it) }
// Temporarily assume that the new parameter is of Any type. Actual type is substituted during the signature update phase // Temporarily assume that the new parameter is of Any type. Actual type is substituted during the signature update phase
val defaultValueForCall = (data.getParameterInitializer().getExpression()!! as? PsiExpression)?.let { it.j2k() } val defaultValueForCall = (data.getParameterInitializer().getExpression() as? PsiExpression)?.let { it.j2k() }
changeInfo.addParameter(KotlinParameterInfo(callableDescriptor = psiMethodDescriptor, changeInfo.addParameter(KotlinParameterInfo(callableDescriptor = psiMethodDescriptor,
name = data.getParameterName(), name = data.getParameterName(),
type = psiMethodDescriptor.builtIns.anyType, type = psiMethodDescriptor.builtIns.anyType,
@@ -146,13 +146,13 @@ abstract class KotlinDebuggerTestBase : KotlinDebuggerTestCase() {
} }
protected fun SuspendContextImpl.doStepOut() { protected fun SuspendContextImpl.doStepOut() {
val stepOutCommand = runReadAction { KotlinSteppingCommandProvider().getStepOutCommand(this, debuggerContext!!) } val stepOutCommand = runReadAction { KotlinSteppingCommandProvider().getStepOutCommand(this, debuggerContext) }
?: dp.createStepOutCommand(this) ?: dp.createStepOutCommand(this)
dp.managerThread.schedule(stepOutCommand) dp.managerThread.schedule(stepOutCommand)
} }
protected fun SuspendContextImpl.doStepOver() { protected fun SuspendContextImpl.doStepOver() {
val stepOverCommand = runReadAction { KotlinSteppingCommandProvider().getStepOverCommand(this, false, debuggerContext!!) } val stepOverCommand = runReadAction { KotlinSteppingCommandProvider().getStepOverCommand(this, false, debuggerContext) }
?: dp.createStepOverCommand(this, false) ?: dp.createStepOverCommand(this, false)
dp.managerThread.schedule(stepOverCommand) dp.managerThread.schedule(stepOverCommand)
} }
@@ -223,7 +223,7 @@ public abstract class AbstractKotlinEvaluateExpressionTest : KotlinDebuggerTestB
} }
fun getExtraVars(): Set<TextWithImports> { fun getExtraVars(): Set<TextWithImports> {
return KotlinFrameExtraVariablesProvider().collectVariables(debuggerContext!!.getSourcePosition(), evaluationContext!!, hashSetOf())!! return KotlinFrameExtraVariablesProvider().collectVariables(debuggerContext.getSourcePosition(), evaluationContext, hashSetOf())
} }
private inner class Printer() { private inner class Printer() {
@@ -264,11 +264,11 @@ public abstract class AbstractKotlinEvaluateExpressionTest : KotlinDebuggerTestB
is StackFrameDescriptor -> logDescriptor(descriptor, "$curIndent frame = $label\n") is StackFrameDescriptor -> logDescriptor(descriptor, "$curIndent frame = $label\n")
is WatchItemDescriptor -> logDescriptor(descriptor, "$curIndent extra = ${descriptor.calcValueName()}\n") is WatchItemDescriptor -> logDescriptor(descriptor, "$curIndent extra = ${descriptor.calcValueName()}\n")
is LocalVariableDescriptor -> logDescriptor(descriptor, "$curIndent local = $label" is LocalVariableDescriptor -> logDescriptor(descriptor, "$curIndent local = $label"
+ " (sp = ${render(SourcePositionProvider.getSourcePosition(descriptor, myProject, debuggerContext!!))})\n") + " (sp = ${render(SourcePositionProvider.getSourcePosition(descriptor, myProject, debuggerContext))})\n")
is StaticDescriptor -> logDescriptor(descriptor, "$curIndent static = $label\n") is StaticDescriptor -> logDescriptor(descriptor, "$curIndent static = $label\n")
is ThisDescriptorImpl -> logDescriptor(descriptor, "$curIndent this = $label\n") is ThisDescriptorImpl -> logDescriptor(descriptor, "$curIndent this = $label\n")
is FieldDescriptor -> logDescriptor(descriptor, "$curIndent field = $label" is FieldDescriptor -> logDescriptor(descriptor, "$curIndent field = $label"
+ " (sp = ${render(SourcePositionProvider.getSourcePosition(descriptor, myProject, debuggerContext!!))})\n") + " (sp = ${render(SourcePositionProvider.getSourcePosition(descriptor, myProject, debuggerContext))})\n")
is MessageDescriptor -> logDescriptor(descriptor, "$curIndent - $label\n") is MessageDescriptor -> logDescriptor(descriptor, "$curIndent - $label\n")
else -> logDescriptor(descriptor, "$curIndent unknown = $label\n") else -> logDescriptor(descriptor, "$curIndent unknown = $label\n")
} }
@@ -352,7 +352,7 @@ public abstract class AbstractKotlinEvaluateExpressionTest : KotlinDebuggerTestB
val sourcePosition = ContextUtil.getSourcePosition(this) val sourcePosition = ContextUtil.getSourcePosition(this)
val contextElement = createContextElement(this) val contextElement = createContextElement(this)
contextElement.putCopyableUserData(KotlinCodeFragmentFactory.DEBUG_FRAME_FOR_TESTS, evaluationContext?.frameProxy) contextElement.putCopyableUserData(KotlinCodeFragmentFactory.DEBUG_FRAME_FOR_TESTS, evaluationContext.frameProxy)
try { try {
@@ -199,7 +199,7 @@ class RunConfigurationTest: KotlinCodeInsightTestCase() {
val dataContext = MapDataContext() val dataContext = MapDataContext()
dataContext.put(Location.DATA_KEY, PsiLocation(getTestProject(), declaration)) dataContext.put(Location.DATA_KEY, PsiLocation(getTestProject(), declaration))
val context = ConfigurationContext.getFromContext(dataContext) val context = ConfigurationContext.getFromContext(dataContext)
val actualClass = (context?.getConfiguration()?.getConfiguration() as? JetRunConfiguration)?.getRunClass() val actualClass = (context.getConfiguration()?.getConfiguration() as? JetRunConfiguration)?.getRunClass()
if (actualClass != null) { if (actualClass != null) {
actualClasses.add(actualClass) actualClasses.add(actualClass)
} }
@@ -229,7 +229,7 @@ class RunConfigurationTest: KotlinCodeInsightTestCase() {
val dataContext = MapDataContext() val dataContext = MapDataContext()
dataContext.put(Location.DATA_KEY, PsiLocation(getTestProject(), element)) dataContext.put(Location.DATA_KEY, PsiLocation(getTestProject(), element))
val runnerAndConfigurationSettings = ConfigurationContext.getFromContext(dataContext)!!.getConfiguration() val runnerAndConfigurationSettings = ConfigurationContext.getFromContext(dataContext).getConfiguration()
if (save) { if (save) {
RunManagerEx.getInstanceEx(myProject).setTemporaryConfiguration(runnerAndConfigurationSettings) RunManagerEx.getInstanceEx(myProject).setTemporaryConfiguration(runnerAndConfigurationSettings)
} }
@@ -35,7 +35,7 @@ class AndroidXmlHandler(private val elementCallback: (String, String) -> Unit) :
val attributesMap = attributes.toMap() val attributesMap = attributes.toMap()
val idAttribute = attributesMap[AndroidConst.ID_ATTRIBUTE_NO_NAMESPACE] val idAttribute = attributesMap[AndroidConst.ID_ATTRIBUTE_NO_NAMESPACE]
val widgetType = attributesMap[AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE] ?: localName val widgetType = attributesMap[AndroidConst.CLASS_ATTRIBUTE_NO_NAMESPACE] ?: localName
val name = idAttribute?.let { idToName(idAttribute!!) } val name = idAttribute?.let { idToName(idAttribute) }
if (name != null) elementCallback(name, widgetType) if (name != null) elementCallback(name, widgetType)
} }
@@ -132,8 +132,8 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
oldPropName: String, oldPropName: String,
processor: SyntheticFileGenerator processor: SyntheticFileGenerator
) { ) {
val props = processor.getSyntheticFiles()?.flatMap { it.findChildrenByClass(javaClass<KtProperty>()).toList() } val props = processor.getSyntheticFiles().flatMap { it.findChildrenByClass(javaClass<KtProperty>()).toList() }
val matchedProps = props?.filter { it.getName() == oldPropName } ?: listOf() val matchedProps = props.filter { it.getName() == oldPropName } ?: listOf()
for (prop in matchedProps) { for (prop in matchedProps) {
allRenames[prop] = newPropName allRenames[prop] = newPropName
} }