KT-22274 report warning on labels that can't be referenced
Labels are meaningful only if they can be referenced by 'break', 'continue', or 'return' expressions.
This commit is contained in:
@@ -298,6 +298,17 @@ class ControlFlowProcessor(
|
||||
generateInstructions(baseExpression)
|
||||
copyValue(baseExpression, expression)
|
||||
}
|
||||
|
||||
val labelNameExpression = expression.getTargetLabel()
|
||||
if (labelNameExpression != null) {
|
||||
val deparenthesizedBaseExpression = KtPsiUtil.deparenthesize(expression)
|
||||
if (deparenthesizedBaseExpression !is KtLambdaExpression &&
|
||||
deparenthesizedBaseExpression !is KtLoopExpression &&
|
||||
deparenthesizedBaseExpression !is KtNamedFunction
|
||||
) {
|
||||
trace.report(Errors.REDUNDANT_LABEL_WARNING.on(labelNameExpression))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitBinaryExpression(expression: KtBinaryExpression) {
|
||||
|
||||
@@ -822,6 +822,8 @@ public interface Errors {
|
||||
DiagnosticFactory0<KtExpressionWithLabel> NOT_A_FUNCTION_LABEL = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<KtExpressionWithLabel> NOT_A_FUNCTION_LABEL_WARNING = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
DiagnosticFactory0<KtElement> REDUNDANT_LABEL_WARNING = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
// Control flow / Data flow
|
||||
|
||||
DiagnosticFactory1<KtElement, List<TextRange>> UNREACHABLE_CODE = DiagnosticFactory1.create(
|
||||
|
||||
+2
@@ -602,6 +602,8 @@ public class DefaultErrorMessages {
|
||||
MAP.put(NOT_A_FUNCTION_LABEL, "Target label does not denote a function");
|
||||
MAP.put(NOT_A_FUNCTION_LABEL_WARNING, "Target label does not denote a function");
|
||||
|
||||
MAP.put(REDUNDANT_LABEL_WARNING, "Label is redundant, because it can not be referenced in either ''break'', ''continue'', or ''return'' expression");
|
||||
|
||||
MAP.put(ANONYMOUS_INITIALIZER_IN_INTERFACE, "Anonymous initializers are not allowed in interfaces");
|
||||
MAP.put(NULLABLE_SUPERTYPE, "A supertype cannot be nullable");
|
||||
MAP.put(DYNAMIC_SUPERTYPE, "A supertype cannot be dynamic");
|
||||
|
||||
Reference in New Issue
Block a user