Skip to content

gccrs: add trivial numeric casts lint - #4629

Open
lucasly-ba wants to merge 1 commit into
Rust-GCC:masterfrom
lucasly-ba:trivial-numeric-casts
Open

gccrs: add trivial numeric casts lint#4629
lucasly-ba wants to merge 1 commit into
Rust-GCC:masterfrom
lucasly-ba:trivial-numeric-casts

Conversation

@lucasly-ba

Copy link
Copy Markdown
Contributor

This patch adds the trivial numeric casts lint, which warns on a numeric cast where the source and target types are the same, e.g. x as i32 when x is already an i32.

gcc/testsuite/ChangeLog:

* rust/compile/trivial-numeric-casts_0.rs: New test.

@lucasly-ba
lucasly-ba force-pushed the trivial-numeric-casts branch 3 times, most recently from d516826 to 1a8bad4 Compare July 6, 2026 12:02
@lucasly-ba
lucasly-ba force-pushed the trivial-numeric-casts branch 3 times, most recently from 1e07909 to 2ac20b3 Compare July 9, 2026 13:35
@lucasly-ba

Copy link
Copy Markdown
Contributor Author

@P-E-P

{
TyTy::BaseType *from;
TyTy::BaseType *to;
if (context.lookup_type (expr.get_casted_expr ().get_mappings ().get_hirid (),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should split this if statement up...

bool found_from = lookup_expr;
bool found_expr = lookup
if (found_from && found_expr)
{
    bool is_numeric = ...
    bool type_names_match = ...
   // you might be able to get away with using TypeCompatable to be more generic not sure maybe this is meant to only work on numeric values...

  if (is_numeric && type_names_match)
  ... warrning
   
}

walk (expr)

This is jsut much easier to read down the line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be good @philberty

@lucasly-ba
lucasly-ba force-pushed the trivial-numeric-casts branch from 2ac20b3 to 069b04f Compare August 15, 2026 21:20
gcc/rust/ChangeLog:

	* checks/lints/unused/rust-unused-checker.cc (is_numeric): New.
	(UnusedChecker::visit): New.
	* checks/lints/unused/rust-unused-checker.h (UnusedChecker::visit):
	New.
	* rust-lang.cc (grs_langhook_init_options_struct): Enable warn_unused.

gcc/testsuite/ChangeLog:

	* rust/compile/trivial-numeric-casts_0.rs: New test.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants