mirror of
https://github.com/irmen/prog8.git
synced 2025-01-11 13:29:45 +00:00
better warning + don't remove non-trivial initializer expression for unused variables
This commit is contained in:
parent
1860f66de5
commit
b526e132a7
@ -120,12 +120,26 @@ class UnusedCodeRemover(private val program: Program,
|
|||||||
if(singleUse is AssignTarget) {
|
if(singleUse is AssignTarget) {
|
||||||
val assignment = singleUse.parent as Assignment
|
val assignment = singleUse.parent as Assignment
|
||||||
if(assignment.origin==AssignmentOrigin.VARINIT) {
|
if(assignment.origin==AssignmentOrigin.VARINIT) {
|
||||||
if(!decl.definingModule.isLibrary)
|
if(assignment.value.isSimple) {
|
||||||
errors.warn("removing unused variable '${decl.name}'", decl.position)
|
// remove the vardecl
|
||||||
return listOf(
|
if(!decl.definingModule.isLibrary)
|
||||||
IAstModification.Remove(decl, parent as IStatementContainer),
|
errors.warn("removing unused variable '${decl.name}'", decl.position)
|
||||||
IAstModification.Remove(assignment, assignment.parent as IStatementContainer)
|
return listOf(
|
||||||
)
|
IAstModification.Remove(decl, parent as IStatementContainer),
|
||||||
|
IAstModification.Remove(assignment, assignment.parent as IStatementContainer)
|
||||||
|
)
|
||||||
|
} else if(assignment.value is IFunctionCall) {
|
||||||
|
// replace the unused variable's initializer function call by a void
|
||||||
|
errors.warn("replaced unused variable '${decl.name}' with void call, maybe this can be removed altogether", decl.position)
|
||||||
|
val fcall = assignment.value as IFunctionCall
|
||||||
|
val voidCall = FunctionCallStatement(fcall.target, fcall.args, true, fcall.position)
|
||||||
|
return listOf(
|
||||||
|
IAstModification.ReplaceNode(decl, voidCall, parent),
|
||||||
|
IAstModification.Remove(assignment, assignment.parent as IStatementContainer)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
errors.warn("variable '${decl.name}' is unused but has non-trivial initialization assignment. Leaving this in but maybe it can be removed altogether", decl.position)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user