Code cleanup: replace chained null-checks with safe-calls

This commit is contained in:
Mikhail Glukhikh
2017-07-11 16:35:04 +03:00
committed by Mikhail Glukhikh
parent 732d1129ab
commit 7fb78a0372
12 changed files with 12 additions and 13 deletions
@@ -809,7 +809,7 @@ class OverrideResolver(
private fun findDataModifierForDataClass(dataClass: DeclarationDescriptor): PsiElement { private fun findDataModifierForDataClass(dataClass: DeclarationDescriptor): PsiElement {
val classDeclaration = DescriptorToSourceUtils.getSourceFromDescriptor(dataClass) as KtClassOrObject? val classDeclaration = DescriptorToSourceUtils.getSourceFromDescriptor(dataClass) as KtClassOrObject?
if (classDeclaration != null && classDeclaration.modifierList != null) { if (classDeclaration?.modifierList != null) {
val modifier = classDeclaration.modifierList!!.getModifier(KtTokens.DATA_KEYWORD) val modifier = classDeclaration.modifierList!!.getModifier(KtTokens.DATA_KEYWORD)
if (modifier != null) { if (modifier != null) {
return modifier return modifier
@@ -55,7 +55,7 @@ fun <D : TypeHolder<D>> D.checkTypePosition(
var noError = true var noError = true
for (argument in arguments) { for (argument in arguments) {
if (argument == null || argument.typeParameter == null || argument.projection.isStarProjection) continue if (argument?.typeParameter == null || argument.projection.isStarProjection) continue
val projectionKind = TypeCheckingProcedure.getEffectiveProjectionKind(argument.typeParameter!!, argument.projection)!! val projectionKind = TypeCheckingProcedure.getEffectiveProjectionKind(argument.typeParameter!!, argument.projection)!!
val newPosition = when (projectionKind) { val newPosition = when (projectionKind) {
@@ -116,7 +116,7 @@ abstract class KotlinCommonBlock(
val childParent = child.treeParent val childParent = child.treeParent
val childType = child.elementType val childType = child.elementType
if (childParent != null && childParent.treeParent != null) { if (childParent?.treeParent != null) {
if (childParent.elementType === KtNodeTypes.BLOCK && childParent.treeParent.elementType === KtNodeTypes.SCRIPT) { if (childParent.elementType === KtNodeTypes.BLOCK && childParent.treeParent.elementType === KtNodeTypes.SCRIPT) {
return Indent.getNoneIndent() return Indent.getNoneIndent()
} }
@@ -71,7 +71,7 @@ class KotlinCompletionCharFilter() : CharFilter() {
} }
'{' -> { '{' -> {
if (currentItem != null && currentItem.getUserData(ACCEPT_OPENING_BRACE) != null) if (currentItem?.getUserData(ACCEPT_OPENING_BRACE) != null)
Result.SELECT_ITEM_AND_FINISH_LOOKUP Result.SELECT_ITEM_AND_FINISH_LOOKUP
else else
Result.HIDE_LOOKUP Result.HIDE_LOOKUP
@@ -590,7 +590,7 @@ class ExpectedInfos(
if (expressionWithType != forExpression.loopRange) return null if (expressionWithType != forExpression.loopRange) return null
val loopVar = forExpression.loopParameter val loopVar = forExpression.loopParameter
val loopVarType = if (loopVar != null && loopVar.typeReference != null) val loopVarType = if (loopVar?.typeReference != null)
(bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, loopVar] as VariableDescriptor).type.takeUnless { it.isError } (bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, loopVar] as VariableDescriptor).type.takeUnless { it.isError }
else else
null null
@@ -373,7 +373,7 @@ class CodeInliner<TCallElement : KtElement>(
is VarargValueArgument -> { is VarargValueArgument -> {
val arguments = resolvedArgument.arguments val arguments = resolvedArgument.arguments
val single = arguments.singleOrNull() val single = arguments.singleOrNull()
if (single != null && single.getSpreadElement() != null) { if (single?.getSpreadElement() != null) {
val expression = single.getArgumentExpression()!!.marked(USER_CODE_KEY) val expression = single.getArgumentExpression()!!.marked(USER_CODE_KEY)
return Argument(expression, bindingContext.getType(expression), isNamed = single.isNamed()) return Argument(expression, bindingContext.getType(expression), isNamed = single.isNamed())
} }
@@ -211,8 +211,7 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() {
val typeToInsert = valueArgumentType.approximateWithResolvableType(scope, true) val typeToInsert = valueArgumentType.approximateWithResolvableType(scope, true)
actions.add(ChangeParameterTypeFix(correspondingParameter, typeToInsert)) actions.add(ChangeParameterTypeFix(correspondingParameter, typeToInsert))
} }
if (correspondingParameterDescriptor != null if (correspondingParameterDescriptor?.varargElementType != null
&& correspondingParameterDescriptor.varargElementType != null
&& KotlinBuiltIns.isArray(valueArgumentType) && KotlinBuiltIns.isArray(valueArgumentType)
&& expressionType.arguments.isNotEmpty() && expressionType.arguments.isNotEmpty()
&& expressionType.arguments[0].type.constructor == expectedType.constructor) { && expressionType.arguments[0].type.constructor == expectedType.constructor) {
@@ -48,7 +48,7 @@ class RemoveNullableFix(element: KtNullableType,
class Factory(private val typeOfError: NullableKind) : KotlinSingleIntentionActionFactory() { class Factory(private val typeOfError: NullableKind) : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtNullableType>? { override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtNullableType>? {
val nullType = diagnostic.psiElement.getNonStrictParentOfType<KtNullableType>() val nullType = diagnostic.psiElement.getNonStrictParentOfType<KtNullableType>()
if (nullType == null || nullType.innerType == null) return null if (nullType?.innerType == null) return null
return RemoveNullableFix(nullType, typeOfError) return RemoveNullableFix(nullType, typeOfError)
} }
} }
@@ -53,7 +53,7 @@ abstract class DeprecatedSymbolUsageFixBase(
val element = element ?: return false val element = element ?: return false
if (!super.isAvailable(project, editor, file)) return false if (!super.isAvailable(project, editor, file)) return false
val strategy = buildUsageReplacementStrategy(element, replaceWith, recheckAnnotation = true) val strategy = buildUsageReplacementStrategy(element, replaceWith, recheckAnnotation = true)
return strategy != null && strategy.createReplacer(element) != null return strategy?.createReplacer(element) != null
} }
final override fun invoke(project: Project, editor: Editor?, file: KtFile) { final override fun invoke(project: Project, editor: Editor?, file: KtFile) {
@@ -108,7 +108,7 @@ class KotlinInplaceParameterIntroducer(
init { init {
val templateState = TemplateManagerImpl.getTemplateState(myEditor) val templateState = TemplateManagerImpl.getTemplateState(myEditor)
val currentType = if (templateState != null && templateState.template != null) { val currentType = if (templateState?.template != null) {
templateState templateState
.getVariableValue(KotlinInplaceVariableIntroducer.TYPE_REFERENCE_VARIABLE_NAME) .getVariableValue(KotlinInplaceVariableIntroducer.TYPE_REFERENCE_VARIABLE_NAME)
?.text ?.text
@@ -98,7 +98,7 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter {
override fun visitDoWhileStatement(statement: PsiDoWhileStatement) { override fun visitDoWhileStatement(statement: PsiDoWhileStatement) {
val condition = statement.condition val condition = statement.condition
val expression = if (condition != null && condition.type != null) val expression = if (condition?.type != null)
codeConverter.convertExpression(condition, condition.type) codeConverter.convertExpression(condition, condition.type)
else else
codeConverter.convertExpression(condition) codeConverter.convertExpression(condition)
@@ -73,7 +73,7 @@ abstract class AndroidLayoutXmlFileManager(val project: Project) {
val allLayoutFiles = allChildren.filter { it.parent.name.startsWith("layout") && it.name.toLowerCase().endsWith(".xml") } val allLayoutFiles = allChildren.filter { it.parent.name.startsWith("layout") && it.name.toLowerCase().endsWith(".xml") }
val allLayoutPsiFiles = allLayoutFiles.fold(ArrayList<PsiFile>(allLayoutFiles.size)) { list, file -> val allLayoutPsiFiles = allLayoutFiles.fold(ArrayList<PsiFile>(allLayoutFiles.size)) { list, file ->
val psiFile = psiManager.findFile(file) val psiFile = psiManager.findFile(file)
if (psiFile != null && psiFile.parent != null) { if (psiFile?.parent != null) {
list += psiFile list += psiFile
} }
list list