Cleanup: apply "lift out..." inspection (+ some others)

This commit is contained in:
Mikhail Glukhikh
2017-06-28 14:30:52 +03:00
committed by Mikhail Glukhikh
parent 0c41ceea9d
commit 9c06739594
52 changed files with 294 additions and 311 deletions
@@ -78,11 +78,11 @@ class AnonymousObjectTransformer(
override fun visitField(access: Int, name: String, desc: String, signature: String?, value: Any?): FieldVisitor? {
addUniqueField(name)
if (isCapturedFieldName(name)) {
return null
return if (isCapturedFieldName(name)) {
null
}
else {
return classBuilder.newField(JvmDeclarationOrigin.NO_ORIGIN, access, name, desc, signature, value)
classBuilder.newField(JvmDeclarationOrigin.NO_ORIGIN, access, name, desc, signature, value)
}
}
@@ -95,12 +95,12 @@ class AnonymousObjectTransformer(
}, ClassReader.SKIP_FRAMES)
if (!inliningContext.isInliningLambda) {
if (debugInfo != null && !debugInfo!!.isEmpty()) {
sourceMapper = SourceMapper.createFromSmap(SMAPParser.parse(debugInfo!!))
sourceMapper = if (debugInfo != null && !debugInfo!!.isEmpty()) {
SourceMapper.createFromSmap(SMAPParser.parse(debugInfo!!))
}
else {
//seems we can't do any clever mapping cause we don't know any about original class name
sourceMapper = IdenticalSourceMapper
IdenticalSourceMapper
}
if (sourceInfo != null && !GENERATE_SMAP) {
classBuilder.visitSource(sourceInfo!!, debugInfo)
@@ -458,12 +458,12 @@ class AnonymousObjectTransformer(
private fun getNewFieldName(oldName: String, originalField: Boolean): String {
if (THIS_0 == oldName) {
if (!originalField) {
return oldName
return if (!originalField) {
oldName
}
else {
//rename original 'this$0' in declaration site lambda (inside inline function) to use this$0 only for outer lambda/object access on call site
return addUniqueField(oldName + INLINE_FUN_THIS_0_SUFFIX)
addUniqueField(oldName + INLINE_FUN_THIS_0_SUFFIX)
}
}
return addUniqueField(oldName + INLINE_TRANSFORMATION_SUFFIX)
@@ -61,13 +61,13 @@ open class BasicReplStageHistory<T>(override val lock: ReentrantReadWriteLock =
lock.write {
val idx = indexOfFirst { it.id == id }
if (idx < 0) throw java.util.NoSuchElementException("Cannot rest to inexistent line ${id.no}")
if (idx < lastIndex) {
return if (idx < lastIndex) {
val removed = asSequence().drop(idx + 1).map { it.id }.toList()
removeRange(idx + 1, size)
currentGeneration.incrementAndGet()
return removed
removed
}
else return emptyList()
else emptyList()
}
}
}
@@ -75,7 +75,7 @@ class ConstructorConsistencyChecker private constructor(
return true
}
if (descriptor.containingDeclaration != classDescriptor) return true
if (insideLValue(reference)) return descriptor.setter?.isDefault != false else return descriptor.getter?.isDefault != false
return if (insideLValue(reference)) descriptor.setter?.isDefault != false else descriptor.getter?.isDefault != false
}
return true
}
@@ -1119,23 +1119,21 @@ class ControlFlowInformationProvider private constructor(
}
private fun combineKinds(kind: TailRecursionKind, existingKind: TailRecursionKind?): TailRecursionKind {
val resultingKind: TailRecursionKind
if (existingKind == null || existingKind == kind) {
resultingKind = kind
return if (existingKind == null || existingKind == kind) {
kind
}
else {
if (check(kind, existingKind, IN_TRY, TAIL_CALL)) {
resultingKind = IN_TRY
IN_TRY
}
else if (check(kind, existingKind, IN_TRY, NON_TAIL)) {
resultingKind = IN_TRY
IN_TRY
}
else {
// TAIL_CALL, NON_TAIL
resultingKind = NON_TAIL
NON_TAIL
}
}
return resultingKind
}
private fun check(a: Any, b: Any, x: Any, y: Any) = a === x && b === y || a === y && b === x
@@ -518,13 +518,12 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
val incrementOrDecrement = isIncrementOrDecrement(operationType)
val resolvedCall = expression.getResolvedCall(trace.bindingContext)
val rhsValue: PseudoValue?
if (resolvedCall != null) {
rhsValue = generateCall(resolvedCall).outputValue
val rhsValue: PseudoValue? = if (resolvedCall != null) {
generateCall(resolvedCall).outputValue
}
else {
generateInstructions(baseExpression)
rhsValue = createNonSyntheticValue(expression, MagicKind.UNRESOLVED_CALL, baseExpression)
createNonSyntheticValue(expression, MagicKind.UNRESOLVED_CALL, baseExpression)
}
if (incrementOrDecrement) {
@@ -866,12 +865,12 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
if (labelName != null) {
val targetLabel = expression.getTargetLabel()!!
val labeledElement = trace.get(BindingContext.LABEL_TARGET, targetLabel)
if (labeledElement is KtLoopExpression) {
loop = labeledElement
loop = if (labeledElement is KtLoopExpression) {
labeledElement
}
else {
trace.report(NOT_A_LOOP_LABEL.on(expression, targetLabel.text))
loop = null
null
}
}
else {
@@ -946,18 +945,18 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
val labelElement = expression.getTargetLabel()
val subroutine: KtElement?
val labelName = expression.getLabelName()
if (labelElement != null && labelName != null) {
subroutine = if (labelElement != null && labelName != null) {
val labeledElement = trace.get(BindingContext.LABEL_TARGET, labelElement)
if (labeledElement != null) {
assert(labeledElement is KtElement)
subroutine = labeledElement as KtElement?
labeledElement as KtElement?
}
else {
subroutine = null
null
}
}
else {
subroutine = builder.returnSubroutine
builder.returnSubroutine
// TODO : a context check
}
@@ -1147,15 +1146,15 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
val resolvedCall = trace.get(BindingContext.COMPONENT_RESOLVED_CALL, entry)
val writtenValue: PseudoValue?
if (resolvedCall != null) {
writtenValue = builder.call(
writtenValue = if (resolvedCall != null) {
builder.call(
entry,
resolvedCall,
getReceiverValues(resolvedCall),
emptyMap<PseudoValue, ValueParameterDescriptor>()).outputValue
}
else {
writtenValue = initializer?.let { createSyntheticValue(entry, MagicKind.UNRESOLVED_CALL, it) }
initializer?.let { createSyntheticValue(entry, MagicKind.UNRESOLVED_CALL, it) }
}
if (generateWriteForEntries) {
@@ -56,11 +56,11 @@ class ControlFlowInstructionsGenerator : ControlFlowBuilderAdapter() {
private fun popBuilder(): ControlFlowInstructionsGeneratorWorker {
val worker = builders.pop()
if (!builders.isEmpty()) {
builder = builders.peek()
builder = if (!builders.isEmpty()) {
builders.peek()
}
else {
builder = null
null
}
return worker
}
@@ -400,13 +400,10 @@ class ControlFlowInstructionsGenerator : ControlFlowBuilderAdapter() {
return magic(expression, expression, inputValues, getMagicKind(operation))
}
private fun getMagicKind(operation: ControlFlowBuilder.PredefinedOperation): MagicKind {
when (operation) {
ControlFlowBuilder.PredefinedOperation.AND -> return MagicKind.AND
ControlFlowBuilder.PredefinedOperation.OR -> return MagicKind.OR
ControlFlowBuilder.PredefinedOperation.NOT_NULL_ASSERTION -> return MagicKind.NOT_NULL_ASSERTION
else -> throw IllegalArgumentException("Invalid operation: " + operation)
}
private fun getMagicKind(operation: ControlFlowBuilder.PredefinedOperation) = when (operation) {
ControlFlowBuilder.PredefinedOperation.AND -> MagicKind.AND
ControlFlowBuilder.PredefinedOperation.OR -> MagicKind.OR
ControlFlowBuilder.PredefinedOperation.NOT_NULL_ASSERTION -> MagicKind.NOT_NULL_ASSERTION
}
override fun read(
@@ -68,11 +68,7 @@ private object PsiChildRangeArgumentType : PsiElementPlaceholderArgumentType<Psi
val project = placeholder.project
val codeStyleManager = CodeStyleManager.getInstance(project)
if (argument.isEmpty) {
placeholder.delete()
return PsiChildRange.EMPTY
}
else {
return if (!argument.isEmpty) {
val first = placeholder.parent.addRangeBefore(argument.first!!, argument.last!!, placeholder)
val last = placeholder.prevSibling
placeholder.delete()
@@ -81,7 +77,11 @@ private object PsiChildRangeArgumentType : PsiElementPlaceholderArgumentType<Psi
if (last != first) {
codeStyleManager.reformatNewlyAddedElement(last.node.treeParent, last.node)
}
return PsiChildRange(first, last)
PsiChildRange(first, last)
}
else {
placeholder.delete()
PsiChildRange.EMPTY
}
}
}
@@ -155,8 +155,8 @@ fun <TElement : KtElement> createByPattern(pattern: String, vararg args: Any, re
.sortedByDescending { it.startOffset }
// reformat whole text except for String arguments (as they can contain user's formatting to be preserved)
if (stringPlaceholderRanges.none()) {
resultElement = codeStyleManager.reformat(resultElement, true) as TElement
resultElement = if (stringPlaceholderRanges.none()) {
codeStyleManager.reformat(resultElement, true) as TElement
}
else {
var bound = resultElement.endOffset - 1
@@ -165,7 +165,7 @@ fun <TElement : KtElement> createByPattern(pattern: String, vararg args: Any, re
resultElement = codeStyleManager.reformatRange(resultElement, range.endOffset + start, bound + 1, true) as TElement
bound = range.startOffset + start
}
resultElement = codeStyleManager.reformatRange(resultElement, start, bound + 1, true) as TElement
codeStyleManager.reformatRange(resultElement, start, bound + 1, true) as TElement
}
// do not reformat the whole expression in PostprocessReformattingAspect
@@ -164,18 +164,17 @@ object CastDiagnosticsUtil {
val variables = subtypeWithVariables.constructor.parameters
val variableConstructors = variables.map { descriptor -> descriptor.typeConstructor }.toSet()
val substitution: MutableMap<TypeConstructor, TypeProjection>
if (supertypeWithVariables != null) {
val substitution: MutableMap<TypeConstructor, TypeProjection> = if (supertypeWithVariables != null) {
// Now, let's try to unify Collection<T> and Collection<Foo> solution is a map from T to Foo
val solution = TypeUnifier.unify(
TypeProjectionImpl(supertype), TypeProjectionImpl(supertypeWithVariables), variableConstructors::contains
)
substitution = Maps.newHashMap(solution.substitution)
Maps.newHashMap(solution.substitution)
}
else {
// If there's no corresponding supertype, no variables are determined
// This may be OK, e.g. in case 'Any as List<*>'
substitution = Maps.newHashMapWithExpectedSize<TypeConstructor, TypeProjection>(variables.size)
Maps.newHashMapWithExpectedSize<TypeConstructor, TypeProjection>(variables.size)
}
// If some of the parameters are not determined by unification, it means that these parameters are lost,
@@ -65,11 +65,11 @@ class ConstAndJvmFieldPropertiesLowering : IrElementTransformerVoid(), FileLower
val property = descriptor.correspondingProperty
if (JvmCodegenUtil.isConstOrHasJvmFieldAnnotation(property)) {
if (descriptor is PropertyGetterDescriptor) {
return substituteGetter(descriptor, expression)
return if (descriptor is PropertyGetterDescriptor) {
substituteGetter(descriptor, expression)
}
else {
return substituteSetter(descriptor, expression)
substituteSetter(descriptor, expression)
}
}
else if (property is SyntheticJavaPropertyDescriptor) {
@@ -141,23 +141,23 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
}
private fun generateWhenBody(expression: KtWhenExpression, irSubject: IrVariable?, irWhen: IrWhen): IrExpression {
if (irSubject == null) {
return if (irSubject == null) {
if (irWhen.branches.isEmpty())
return IrBlockImpl(expression.startOffset, expression.endOffset, context.builtIns.unitType, IrStatementOrigin.WHEN)
IrBlockImpl(expression.startOffset, expression.endOffset, context.builtIns.unitType, IrStatementOrigin.WHEN)
else
return irWhen
irWhen
}
else {
if (irWhen.branches.isEmpty()) {
val irBlock = IrBlockImpl(expression.startOffset, expression.endOffset, context.builtIns.unitType, IrStatementOrigin.WHEN)
irBlock.statements.add(irSubject)
return irBlock
irBlock
}
else {
val irBlock = IrBlockImpl(expression.startOffset, expression.endOffset, irWhen.type, IrStatementOrigin.WHEN)
irBlock.statements.add(irSubject)
irBlock.statements.add(irWhen)
return irBlock
irBlock
}
}
}