Skip to content

gccrs: add unused unsafe lint#4630

Open
lucasly-ba wants to merge 1 commit into
Rust-GCC:masterfrom
lucasly-ba:unused-unsafe
Open

gccrs: add unused unsafe lint#4630
lucasly-ba wants to merge 1 commit into
Rust-GCC:masterfrom
lucasly-ba:unused-unsafe

Conversation

@lucasly-ba

Copy link
Copy Markdown
Contributor

This patch adds the unused unsafe lint, which warns about an unsafe block that does not contain any operation requiring an unsafe context. The unsafe checker now records which unsafe blocks are actually used and reports the rest.

gcc/testsuite/ChangeLog:

* rust/compile/unused-unsafe_0.rs: New test.

Comment thread gcc/rust/checks/errors/rust-unsafe-checker.h Outdated
@lucasly-ba lucasly-ba force-pushed the unused-unsafe branch 3 times, most recently from 9ed4d9b to 9e8afb6 Compare July 9, 2026 00:37
Warn about an unsafe block that does not contain any operation requiring
an unsafe context. The unsafe checker now records which unsafe blocks are
actually used by an unsafe operation, and reports the rest. The warning
is gated behind -frust-unused-check-2.0.

gcc/rust/ChangeLog:

	* checks/errors/rust-unsafe-checker.cc (check_static_mut)
	(check_extern_static, check_unsafe_call, check_extern_call)
	(check_target_attr): Return whether the operation requires unsafe and
	only error outside of an unsafe context.
	(UnsafeChecker::mark_unsafe_used): New.
	(UnsafeChecker::check_use_of_static)
	(UnsafeChecker::check_function_call)
	(UnsafeChecker::check_function_attr): Mark the unsafe block as used.
	(UnsafeChecker::visit): Mark used and warn on unnecessary unsafe block.
	* checks/errors/rust-unsafe-checker.h (UnsafeChecker): Add
	mark_unsafe_used and used_unsafe_blocks.
	* rust-lang.cc (grs_langhook_init_options_struct): Enable warn_unused.

gcc/testsuite/ChangeLog:

	* rust/compile/unused-unsafe_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