diff --git a/src/typecheck.c b/src/typecheck.c index a7d034e3..473a4f05 100644 --- a/src/typecheck.c +++ b/src/typecheck.c @@ -571,8 +571,12 @@ static void do_explicit_cast(ExpressionTypes *types, const Type *to, Location lo { assert(!types->implicit_cast_type); const Type *from = types->type; + + if (from == to) + show_warning(location, "unnecessary cast from %s to %s", from->name, to->name); + if ( - from != to // TODO: should probably be error if it's the same type. + from != to && !(from->kind == TYPE_ARRAY && to->kind == TYPE_POINTER && from->data.array.membertype == to->data.valuetype) && !(is_pointer_type(from) && is_pointer_type(to)) && !(is_number_type(from) && is_number_type(to)) diff --git a/tests/should_succeed/unnecessary_cast_warning.jou b/tests/should_succeed/unnecessary_cast_warning.jou new file mode 100644 index 00000000..7dc9e829 --- /dev/null +++ b/tests/should_succeed/unnecessary_cast_warning.jou @@ -0,0 +1,6 @@ +import "stdlib/io.jou" + +def main() -> int: + x = 1 as int # Warning: unnecessary cast from int to int + printf("%d\n", x) # Output: 1 + return 0