lazy-regex icon indicating copy to clipboard operation
lazy-regex copied to clipboard

return in regex_switch triggers non relevant warning from rustc

Open Canop opened this issue 6 months ago • 1 comments

This (shortened) code does what's expected:

        regex_switch!(s,
            r"^export:(?<name>.+)$" => Self::Export(name.to_string()),
            r"^(?:internal:)?toggle-backtrace\(2\)$" => Self::ToggleBacktrace("2"),
            r"^(?:internal:)?toggle-backtrace\(full\)$" => Self::ToggleBacktrace("full"),
            r"^(?:internal:)?toggle-backtrace\((?<level>)\)$" => {
                return Err(ParseActionError::InvalidBacktraceLevel(level.to_string()));
            }
            r"^(?:internal:)?toggle-summary$" => Self::ToggleSummary,
            r"^focus[_-]file\((?<file>.*)\)$" => Self::FocusFile(FocusFileCommand::new(file)),
        ).ok_or(ParseActionError::UnknownAction(s.to_string()))

But it triggers an "unreachable_code" warning:

Image

I'm not sure yet of the best way to prevent this false positive.

Canop avatar May 26 '25 11:05 Canop

To make it clearer, the generated code looks like this:

                        if let Some(caps) = RE.captures(s) {
                            let level: &str = caps
                                .get(1usize)
                                .map_or("", |c| c.as_str());
                            let output = Some({
                                return Err(
                                    ParseActionError::InvalidBacktraceLevel(level.to_string()),
                                );
                            });
                            break 'switch output;
                        }

Canop avatar May 26 '25 12:05 Canop