Skip to content

Commit

Permalink
Unnecessary cast warning (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Dec 19, 2023
1 parent 6d382d8 commit 31cc68c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/typecheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions tests/should_succeed/unnecessary_cast_warning.jou
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 31cc68c

Please sign in to comment.