From 31cc68c8d77406dbce5c628c42e8082452ec775d Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 19 Dec 2023 13:22:57 +0200 Subject: [PATCH] Unnecessary cast warning (#467) --- src/typecheck.c | 6 +++++- tests/should_succeed/unnecessary_cast_warning.jou | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/should_succeed/unnecessary_cast_warning.jou 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