Skip to content

Conversation

scottgerring
Copy link
Contributor

This should address #13099 for the let_unit test.

changelog: [manual_assert]: Updated manual_assert to use multipart_suggestions where appropriate

@rustbot
Copy link
Collaborator

rustbot commented Dec 6, 2024

r? @y21

rustbot has assigned @y21.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 6, 2024
@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from a2618b9 to 09589ff Compare December 6, 2024 09:57
@scottgerring
Copy link
Contributor Author

scottgerring commented Dec 6, 2024

I could use a pointer on this one. We're trying to avoid displaying any comments that are captured within the span including the if/assert block, and only retain them in the tool-applied suggestion:

span_lint_and_then(
cx,
MANUAL_ASSERT,
expr.span,
"only a `panic!` in `if`-then statement",
|diag| {
// comments can be noisy, do not show them to the user
if !comments.is_empty() {
diag.tool_only_span_suggestion(
expr.span.shrink_to_lo(),
"add comments back",
comments,
applicability,
);
}
diag.span_suggestion(expr.span, "try instead", sugg, applicability);
},

I've preserved this here by keeping the tool-only suggestion, and replacing only the span_suggestion with multipart. I believe this serves the intended purpose, but the kind of "undesired upshot" is that we end up with two different outputs from the test framework:

  • manual_assert.editionX.1.fixed - user-facing suggestion only - replaces the if/assert, does not add comment back
  • manual_assert.edition.X.2.fixed - in cases where there is a comment, runs the machine-suggestion only - essentially duplicating the comment, but not replacing the if/assert.

The second case fails the testing, because the if/assert has not been removed.

I think this is the correct behaviour from the runtime perspective, but because the testing framework automatically diverges when there are multiple suggestions, i'm stuck. Options I see:

  • Stop trying to hide the comments from the user
  • Leverage some mechanism to group both machine-only and regular changes together into one multipart suggestion
  • Leverage some mechanism to tell the testing framework to ignore the .2. outputs

Any tips @y21 ?

@y21
Copy link
Member

y21 commented Dec 10, 2024

I suppose there would also be the option to have a single multipart suggestion that removes the if cond { + } and changes the panic! to assert!(cond. Then the comments in between the condition and the assert are naturally preserved and the lint wouldn't need to bother extracting them at all, but maybe that would look confusing. Just including the comments in the same suggestion also sounds fine to me though.

@scottgerring
Copy link
Contributor Author

Cheers @y21 - i'll add them back in and see where we land 🙌

@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch 3 times, most recently from bc9e5a6 to e146ead Compare December 11, 2024 10:16
@scottgerring scottgerring marked this pull request as ready for review December 11, 2024 10:48
@scottgerring
Copy link
Contributor Author

Hey @y21 , do you have a chance to look at this? We are inches away from finishing the whole issue 🤣

@y21
Copy link
Member

y21 commented Dec 17, 2024

Sorry for the delay, will try to get to this soon (probably this weekend). I have a bunch of other PRs + other stuff going on at the same time

@scottgerring
Copy link
Contributor Author

Sorry for the delay, will try to get to this soon (probably this weekend). I have a bunch of other PRs + other stuff going on at the same time

Sorry I figured it might’ve fallen through the cracks! It’s probably not a high priority so no stress. Hope you can have a good break!

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 31, 2025
@rustbot

This comment has been minimized.

@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from 7a68957 to a805a6f Compare January 31, 2025 14:24
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 31, 2025
@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from a805a6f to 5e448ee Compare January 31, 2025 14:52
Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for pushing this through!

@y21 y21 added this pull request to the merge queue Feb 11, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 11, 2025
@y21
Copy link
Member

y21 commented Feb 11, 2025

Looks like CI is failing with a compile error. The signature of reindent_multiline was changed recently so most likely you'll have to rebase it so see/fix this locally.

Comment on lines 66 to 64
let base_sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");

let indent = indent_of(cx, expr.span);
let full_sugg = reindent_multiline(format!("{comments}{base_sugg}").into(), true, indent);
Copy link
Member

Choose a reason for hiding this comment

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

While we're already here and have to change something anyway: since this base_sugg is only used once we could inline it into the format macro directly to get rid of an allocation

Suggested change
let base_sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
let indent = indent_of(cx, expr.span);
let full_sugg = reindent_multiline(format!("{comments}{base_sugg}").into(), true, indent);
let indent = indent_of(cx, expr.span);
let full_sugg = reindent_multiline(&*format!("{comments}assert!({not}{cond_sugg}, {format_args_snip}){semicolon}"), true, indent);

(may need some formatting changes if the line is too long)


Also, can you move all of this code that prepares/formats the suggestion into the span_lint_and_then closure? The lint is in pedantic, so often the lint isn't enabled and we don't need to emit/prepare any suggestion as the closure won't be called.

@y21 y21 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 15, 2025
@rustbot

This comment has been minimized.

@Jarcho
Copy link
Contributor

Jarcho commented Sep 17, 2025

Ping @scottgerring from triage. Do you plan to return to working on this? Looks like it was almost ready.

@scottgerring
Copy link
Contributor Author

hey @Jarcho sorry this dropped off my radar! I'll try rebase it this week.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Sep 17, 2025
@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from 5d8489b to 5e448ee Compare September 17, 2025 07:56
@rustbot

This comment has been minimized.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Sep 17, 2025
@rustbot

This comment has been minimized.

@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from 5e448ee to 0f8c62c Compare September 17, 2025 08:03
@rustbot
Copy link
Collaborator

rustbot commented Sep 17, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch 2 times, most recently from 3346a38 to 793c00a Compare September 17, 2025 08:26
Copy link

github-actions bot commented Sep 17, 2025

Lintcheck changes for 8ef9057

Lint Added Removed Changed
clippy::manual_assert 0 0 70

This comment will be updated if you push new changes

@scottgerring
Copy link
Contributor Author

Hey @Jarcho I reckon this is good to go now 🙏

Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

One minor nit I only now realized (sorry), other than that looks good to me. Could you also squash the commits the next push? Then this should be ready to merge

View changes since this review

Comment on lines 74 to 77
diag.multipart_suggestion(
"replace `if`-then-`panic!` with `assert!`",
vec![(expr.span, full_sugg)],
applicability,
Copy link
Member

Choose a reason for hiding this comment

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

Actually, since this is just always a single suggestion now, can't we just use .span_suggestion() now (or rather .span_suggestion_verbose() to keep having it on a separate line)? That should be identical but removes the need for creating a vec here.

Suggested change
diag.multipart_suggestion(
"replace `if`-then-`panic!` with `assert!`",
vec![(expr.span, full_sugg)],
applicability,
diag.span_suggestion_verbose(
expr.span,
"replace `if`-then-`panic!` with `assert!`",
full_sugg,
applicability,
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @y21 im AFK on holidays at the moment, but I can certainly polish it when I’m back in a couple weeks 👌

@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from 793c00a to 8ef9057 Compare October 8, 2025 10:59
@scottgerring
Copy link
Contributor Author

@y21 - let's try again :D Squashed into a single commit, using span_suggestion_verbose.

@y21 y21 added this pull request to the merge queue Oct 8, 2025
Merged via the queue into rust-lang:master with commit e70b206 Oct 8, 2025
11 checks passed
@scottgerring scottgerring deleted the chore/fix-manual-assert branch October 9, 2025 12:29
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.

4 participants