motoko icon indicating copy to clipboard operation
motoko copied to clipboard

Feature Request: Warning for extra fields in new record

Open Gekctek opened this issue 1 year ago • 1 comments

I run into a 'problem' a lot in Motoko where I have leftover/extra fields specified on records that i dont clean up. Doesn't hurt anything but is annoying. My suggestion is to have warnings on fields that are never used when creating a new record like the following:


type Record1 = {
    f1 : Int;
    f2 : Int;
};

func function1(r : Record1) {
    // ...
};
let r1 : Record1 = {
    f1 = 1;
    f2 = 2;
    f3 = 3; // Warning here for unused field
};
// OR
function1({
    f1 = 1;
    f2 = 2;
    f3 = 3;  // Warning here for unused field
});

So something like this would be ok potentially because Record2 is specified, even though function1() only takes a Record1

type Record2 = Record1 and {
    f3 : Int;
};

let r2 : Record2 = {
    f1 = 1;
    f2 = 2;
    f3 = 3;
};
function1(r2);

But maybe if f3 is never used after function1(), maybe it can have a warning, but im only concerned about the first examples

Minor, but would be nice to have

Gekctek avatar Feb 02 '24 23:02 Gekctek

FWIW, a warning for literal cases like the first two would be trivial to implement in Typing.check_exp_field.

rossberg avatar Feb 05 '24 21:02 rossberg