Bug 472929 - Produce warnings for unnecessary casting (oclAsType)
Summary: Produce warnings for unnecessary casting (oclAsType)
Status: UNCONFIRMED
Alias: None
Product: QVTo
Classification: Modeling
Component: Engine (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-17 05:29 EDT by Dennis Hendriks CLA
Modified: 2015-07-17 05:30 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dennis Hendriks CLA 2015-07-17 05:29:44 EDT
Consider the following QVTo transformation:

[code]
modeltype ecore uses "http://www.eclipse.org/emf/2002/Ecore";

transformation test(in in1: ecore, out out1: ecore);

main() {
    // Object creation.
    var obj1a: EClassifier := object EClass{};
    var obj1b: EClass      := object EClass{};

    // Assignment to specific type.
    var obj2a: EClass      := obj1a; // Fails as expected.
    var obj2b: EClass      := obj1b;

    // Assignment to general type.
    var obj3a: EClassifier := obj1a;
    var obj3b: EClassifier := obj1b;

    // Casting to specific type.
    var obj4a: EClass      := obj1a.oclAsType(EClass);
    var obj4b: EClass      := obj1b.oclAsType(EClass); // Unnecessary cast.
    var obj5a: EClassifier := obj1a.oclAsType(EClass); // Unnecessary cast? (might fail at runtime, but not in this case).
    var obj5b: EClassifier := obj1b.oclAsType(EClass); // Unnecessary cast.

    // Casting to general type.
    var obj6a: EClassifier := obj1a.oclAsType(EClassifier); // Unnecessary cast.
    var obj6b: EClassifier := obj1b.oclAsType(EClassifier); // Unnecessary cast.
}
[/code]

The comments indicate where unnecessary casting (unnecessary use of oclAstType) occurs. Warnings could be produced for them.

There is one exception:

[code]
    var obj5a: EClassifier := obj1a.oclAsType(EClass); // Necessary cast (might fail at runtime, but not in this case).
[/code]

There, the cast might fail, if 'obj1a' contains an EClassifier that is not an EClass. In this case it is OK, but if it would contain something else, the cast might fail. In that sense removing it might lead to different behavior. While the cast is always unnecessary type-wise, I'm unsure whether there should be a warning in this case.

Note that there may be many more operations for which warnings could be produced if they are unnecessary, or if they always generate the same result (e.g. 'true.oclIsTypeOf(Boolean)'). However, this issue is only about the unnecessary casting.
Comment 1 Dennis Hendriks CLA 2015-07-17 05:30:38 EDT
(In reply to Dennis Hendriks from comment #0)
> [code]
>     var obj5a: EClassifier := obj1a.oclAsType(EClass); // Necessary cast
> (might fail at runtime, but not in this case).
> [/code]

Correction. It should be:

> [code]
>     var obj5a: EClassifier := obj1a.oclAsType(EClass); // Unnecessary cast?
> (might fail at runtime, but not in this case).
> [/code]