Skip to content

gccrs: add unreachable pattern lint#4624

Open
lucasly-ba wants to merge 1 commit into
Rust-GCC:masterfrom
lucasly-ba:unreachable-patterns
Open

gccrs: add unreachable pattern lint#4624
lucasly-ba wants to merge 1 commit into
Rust-GCC:masterfrom
lucasly-ba:unreachable-patterns

Conversation

@lucasly-ba

Copy link
Copy Markdown
Contributor

This patch adds the unreachable pattern lint, which warns on match arms that can never be reached because an earlier arm is irrefutable.

gcc/testsuite/ChangeLog:

* rust/compile/unreachable-patterns_0.rs: New test.

@lucasly-ba lucasly-ba force-pushed the unreachable-patterns branch 2 times, most recently from 9d057f3 to 37a906b Compare June 29, 2026 21:37
Comment on lines +339 to +342
|| (type == HIR::Pattern::PatternType::IDENTIFIER
&& !static_cast<HIR::IdentifierPattern &> (
*arm.get_pattern ())
.has_subpattern ())))

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.

This also needs a test, not just the wildcard pattern. I think this is not enough to check that an identifier pattern is irrefutable, because for example this gets parsed as an identifier pattern:

enum Option<T> {
    Some(T),
    None,
}

use Option::{Some, None};

match some_option {
    None => {},
}

and is not irrefutable

@lucasly-ba lucasly-ba force-pushed the unreachable-patterns branch 3 times, most recently from 03e99d3 to 22cf444 Compare July 9, 2026 15:43
gcc/rust/ChangeLog:

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

gcc/testsuite/ChangeLog:

	* rust/compile/unreachable-patterns_0.rs: New test.

Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
@lucasly-ba lucasly-ba force-pushed the unreachable-patterns branch from 22cf444 to 6ab88c3 Compare July 9, 2026 17:00
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