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 {
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)
if (modifier != null) {
return modifier
@@ -55,7 +55,7 @@ fun <D : TypeHolder<D>> D.checkTypePosition(
var noError = true
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 newPosition = when (projectionKind) {
@@ -116,7 +116,7 @@ abstract class KotlinCommonBlock(
val childParent = child.treeParent
val childType = child.elementType
if (childParent != null && childParent.treeParent != null) {
if (childParent?.treeParent != null) {
if (childParent.elementType === KtNodeTypes.BLOCK && childParent.treeParent.elementType === KtNodeTypes.SCRIPT) {
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
else
Result.HIDE_LOOKUP
@@ -590,7 +590,7 @@ class ExpectedInfos(
if (expressionWithType != forExpression.loopRange) return null
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 }
else
null
@@ -373,7 +373,7 @@ class CodeInliner<TCallElement : KtElement>(
is VarargValueArgument -> {
val arguments = resolvedArgument.arguments
val single = arguments.singleOrNull()
if (single != null && single.getSpreadElement() != null) {
if (single?.getSpreadElement() != null) {
val expression = single.getArgumentExpression()!!.marked(USER_CODE_KEY)
return Argument(expression, bindingContext.getType(expression), isNamed = single.isNamed())
}
@@ -211,8 +211,7 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() {
val typeToInsert = valueArgumentType.approximateWithResolvableType(scope, true)
actions.add(ChangeParameterTypeFix(correspondingParameter, typeToInsert))
}
if (correspondingParameterDescriptor != null
&& correspondingParameterDescriptor.varargElementType != null
if (correspondingParameterDescriptor?.varargElementType != null
&& KotlinBuiltIns.isArray(valueArgumentType)
&& expressionType.arguments.isNotEmpty()
&& expressionType.arguments[0].type.constructor == expectedType.constructor) {
@@ -48,7 +48,7 @@ class RemoveNullableFix(element: KtNullableType,
class Factory(private val typeOfError: NullableKind) : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<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)
}
}
@@ -53,7 +53,7 @@ abstract class DeprecatedSymbolUsageFixBase(
val element = element ?: return false
if (!super.isAvailable(project, editor, file)) return false
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) {
@@ -108,7 +108,7 @@ class KotlinInplaceParameterIntroducer(
init {
val templateState = TemplateManagerImpl.getTemplateState(myEditor)
val currentType = if (templateState != null && templateState.template != null) {
val currentType = if (templateState?.template != null) {
templateState
.getVariableValue(KotlinInplaceVariableIntroducer.TYPE_REFERENCE_VARIABLE_NAME)
?.text
@@ -98,7 +98,7 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter {
override fun visitDoWhileStatement(statement: PsiDoWhileStatement) {
val condition = statement.condition
val expression = if (condition != null && condition.type != null)
val expression = if (condition?.type != null)
codeConverter.convertExpression(condition, condition.type)
else
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 allLayoutPsiFiles = allLayoutFiles.fold(ArrayList<PsiFile>(allLayoutFiles.size)) { list, file ->
val psiFile = psiManager.findFile(file)
if (psiFile != null && psiFile.parent != null) {
if (psiFile?.parent != null) {
list += psiFile
}
list