Skip to content

Restore fix_unused_unsafe transform and fix AST printing bug#1595

Open
randomPoison wants to merge 4 commits intomasterfrom
legare/unused-unsafe
Open

Restore fix_unused_unsafe transform and fix AST printing bug#1595
randomPoison wants to merge 4 commits intomasterfrom
legare/unused-unsafe

Conversation

@randomPoison
Copy link
Contributor

@randomPoison randomPoison commented Feb 13, 2026

Add a test case for the fix_unused_unsafe transform, and fix an issue with how we were converting AST nodes to strings that was causing the transform to panic.

The issue with converting AST nodes to strings came from a subtle issue with how the pretty printer in pprust is meant to be used. pprust::to_string constructs a pprust::State, gives you that State object to interact with, and then extracts the printed string from the State at the end. This is meant to be used in combination with some print_foo methods on State that write into the state's internal buffer. However, we were using the foo_to_string methods, which directly return the printed string without updating the State's buffer (or at least resets the state after the string is generated). This caused pprust::to_string to return an empty string, which then caused a panic in the rewriting process. The foo_to_string methods are what we want to use, and we can bypass pprust::to_string and construct a State object directly in order to use these methods. I looked at all the places where we were using pprust::to_string and fixed the ones that were using the API wrong.

The test reflects the current functionality of the transform, which only removes the unsafe keyword but leaves the block in place. I would like the further extend the transform pass to fully remove the block where safe to do so, but I'd like to do that in a separate PR to keep this one simple. I've added #1596 to track that pending work.

We were incorrectly using `pprust::to_string` for methods that directly returned the string, causing us to generate empty strings. For the various `x_to_string` methods we want to construct a `pprust::State` directly and then call the method

This comment was marked as resolved.

@randomPoison randomPoison added the refactorer This issue relates to the refactoring tool label Feb 13, 2026
@ahomescu
Copy link
Contributor

So the tl;dr of this PR is that we're switching from the foo_to_string methods to print_foo which is what we wanted anyway?

_ => pprust::to_string(|s| {
s.stmt_to_string(self);
}),
_ => pprust::State::new().stmt_to_string(self),
Copy link
Contributor

Choose a reason for hiding this comment

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

Reading the callee here, it seems to me that the difference is we're switching from calling s.stmt_to_string() to s.print_stmt() instead. Is that correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactorer This issue relates to the refactoring tool

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants