Community
Participate
Working Groups
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.
(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]