From 6d20f5f52cb597ba02f08b2c02537b3ff9ef2f15 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sun, 28 Dec 2025 19:04:14 +0200 Subject: [PATCH] Added error "Cannot return a value from a method with void result type" --- .../util/script/interpreter/MethodInfo.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodInfo.java index 5614c2fec..484155472 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodInfo.java @@ -29,6 +29,7 @@ public final class MethodInfo { NONE, MISSING_RETURN, // Non-void method missing return statement RETURN_TYPE_MISMATCH, // Return statement type doesn't match method return type + VOID_METHOD_RETURNS_VALUE, // Void method returns a value DUPLICATE_PARAMETER, // Two parameters have the same name PARAMETER_UNDEFINED // Parameter type cannot be resolved } @@ -454,9 +455,9 @@ public final class MethodInfo { */ private void validateReturnTypes(String bodyText, TypeResolver typeResolver) { if (bodyText == null || bodyText.isEmpty()) return; - if (TypeChecker.isVoidType(returnType)) return; - String expectedTypeName = returnType.getSimpleName(); + boolean isVoid = TypeChecker.isVoidType(returnType); + String expectedTypeName = isVoid ? "void" : returnType.getSimpleName(); // Remove comments but keep strings (we need accurate positions) String cleanBody = CodeParser.removeComments(bodyText); @@ -478,12 +479,17 @@ public final class MethodInfo { // Extract the return expression String returnExpr = cleanBody.substring(returnPos + 6, semiPos).trim(); - // Skip empty returns (handled by missing return check) - if (!returnExpr.isEmpty()) { - // Calculate absolute position (bodyStart + 1 is after the opening brace) - int absoluteReturnStart = bodyStart + 1 + returnPos; - int absoluteSemiEnd = bodyStart + 1 + semiPos + 1; // +1 to include semicolon - + // Calculate absolute position (bodyStart + 1 is after the opening brace) + int absoluteReturnStart = bodyStart + 1 + returnPos; + int absoluteSemiEnd = bodyStart + 1 + semiPos + 1; // +1 to include semicolon + + // Check void method returning a value + if (isVoid && !returnExpr.isEmpty()) { + String message = "Cannot return a value from a method with void result type"; + addReturnStatementError(absoluteReturnStart, absoluteSemiEnd, message, returnType, null); + } + // Check non-void method return type compatibility + else if (!isVoid && !returnExpr.isEmpty() && typeResolver != null) { // Resolve the expression type TypeInfo actualType = typeResolver.resolveExpression(returnExpr, absoluteReturnStart);