diff --git a/src/typecheck.c b/src/typecheck.c index ecbc0033..473a4f05 100644 --- a/src/typecheck.c +++ b/src/typecheck.c @@ -573,10 +573,11 @@ static void do_explicit_cast(ExpressionTypes *types, const Type *to, Location lo const Type *from = types->type; if (from == to) - fail(location, "unnecessary cast from %s to %s", from->name, to->name); + show_warning(location, "unnecessary cast from %s to %s", from->name, to->name); if ( - !(from->kind == TYPE_ARRAY && to->kind == TYPE_POINTER && from->data.array.membertype == to->data.valuetype) + 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)) && !(is_integer_type(from) && to->kind == TYPE_ENUM) diff --git a/tests/other_errors/unnecessary_cast.jou b/tests/other_errors/unnecessary_cast.jou deleted file mode 100644 index 498fc934..00000000 --- a/tests/other_errors/unnecessary_cast.jou +++ /dev/null @@ -1,3 +0,0 @@ -def foo() -> None: - x: int = 1 # no error - x = 1 as int # Error: unnecessary cast from int to int 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