From ee9458f01a3b7a1060ab37aad419597718675a6b Mon Sep 17 00:00:00 2001 From: Martin Jobst Date: Mon, 29 Jul 2024 16:09:37 +0200 Subject: [PATCH] Fix unnecessary conversion warning from integer to real --- .../structuredtextcore/validation/STCoreValidator.java | 3 +++ .../tests/STFunctionValidatorTest.xtend | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/plugins/org.eclipse.fordiac.ide.structuredtextcore/src/org/eclipse/fordiac/ide/structuredtextcore/validation/STCoreValidator.java b/plugins/org.eclipse.fordiac.ide.structuredtextcore/src/org/eclipse/fordiac/ide/structuredtextcore/validation/STCoreValidator.java index e886cd69e8..296984bd19 100644 --- a/plugins/org.eclipse.fordiac.ide.structuredtextcore/src/org/eclipse/fordiac/ide/structuredtextcore/validation/STCoreValidator.java +++ b/plugins/org.eclipse.fordiac.ide.structuredtextcore/src/org/eclipse/fordiac/ide/structuredtextcore/validation/STCoreValidator.java @@ -50,6 +50,7 @@ import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.fordiac.ide.model.data.AnyBitType; import org.eclipse.fordiac.ide.model.data.AnyIntType; +import org.eclipse.fordiac.ide.model.data.AnyRealType; import org.eclipse.fordiac.ide.model.data.AnySignedType; import org.eclipse.fordiac.ide.model.data.AnyStringType; import org.eclipse.fordiac.ide.model.data.AnyUnsignedType; @@ -475,8 +476,10 @@ && isBetterCastPossible(argumentDataType, expectedReturnDataType)) { private static boolean isCastSemanticallyRelevant(final DataType argumentDataType, final DataType returnDataType) { // semantically relevant casts: // - signed to unsigned + // - integer to real // - involves bit types return (argumentDataType instanceof AnySignedType && returnDataType instanceof AnyUnsignedType) + || (argumentDataType instanceof AnyIntType && returnDataType instanceof AnyRealType) || argumentDataType instanceof AnyBitType || returnDataType instanceof AnyBitType; } diff --git a/plugins/org.eclipse.fordiac.ide.structuredtextfunctioneditor.tests/src/org/eclipse/fordiac/ide/structuredtextfunctioneditor/tests/STFunctionValidatorTest.xtend b/plugins/org.eclipse.fordiac.ide.structuredtextfunctioneditor.tests/src/org/eclipse/fordiac/ide/structuredtextfunctioneditor/tests/STFunctionValidatorTest.xtend index 7b5ec91ed3..7e7d594ac3 100644 --- a/plugins/org.eclipse.fordiac.ide.structuredtextfunctioneditor.tests/src/org/eclipse/fordiac/ide/structuredtextfunctioneditor/tests/STFunctionValidatorTest.xtend +++ b/plugins/org.eclipse.fordiac.ide.structuredtextfunctioneditor.tests/src/org/eclipse/fordiac/ide/structuredtextfunctioneditor/tests/STFunctionValidatorTest.xtend @@ -1607,6 +1607,16 @@ class STFunctionValidatorTest { END_FUNCTION '''.parse.assertWarning(STCorePackage.eINSTANCE.STFeatureExpression, STCoreValidator.UNNECESSARY_LITERAL_CONVERSION, "Unnecessary conversion of literal to USINT") + ''' + FUNCTION test + VAR_TEMP + INT_VAR: INT; + INT_VAR2: INT; + REAL_VAR: REAL; + END_VAR + REAL_VAR := INT_TO_REAL(INT_VAR) / INT_VAR2; + END_FUNCTION + '''.parse.assertNoIssues } @Test