Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unnecessary_semicolon: do not lint if it may cause borrow errors #14049

Merged
merged 2 commits into from
Jan 22, 2025

Conversation

samueltardieu
Copy link
Contributor

Before edition 2024, some temporaries used in scrutinees of a match used as the last expression of a block may outlive some referenced local variables. Prevent those cases from happening by checking that alive temporaries with significant drop do have a static lifetime.

The check is performed only for edition 2021 and earlier, and for the last statement if it would become the last expression of the block.

changelog: [unnecessary_semicolon]: prevent borrow errors in editions lower than 2024

r? @y21

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 20, 2025
Comment on lines 97 to 108
&& for_each_unconsumed_temporary(cx, expr, |temporary_ty| {
if temporary_ty.has_significant_drop(cx.tcx, cx.typing_env())
&& temporary_ty
.walk()
.any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(re) if !re.is_static()))
{
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
})
.is_break()
Copy link
Member

@y21 y21 Jan 22, 2025

Choose a reason for hiding this comment

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

Suggestion: now that there are two lints that need to do this it probably makes sense to move this to clippy_utils/ty, since I can see how we might want to adjust the logic in here a little bit in the future (specifically the has_significant_drop I'm not sure is correct as it excludes some types with drop glue - seems like it should use needs_drop instead but I also can't quite make up a FP example so 🤷 not a problem for this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. The name is a bit of a mouthful, but I reflects what the function is doing (and it is quite a specialized one).

Before edition 2024, some temporaries used in scrutinees in a `match`
used as the last expression of a block may outlive some referenced
local variables. Prevent those cases from happening by checking that
alive temporaries with significant drop do have a static lifetime.

The check is performed only for edition 2021 and earlier, and for the
last statement if it would become the last expression of the block.
@y21 y21 added this pull request to the merge queue Jan 22, 2025
Merged via the queue into rust-lang:master with commit c024c13 Jan 22, 2025
11 checks passed
@samueltardieu samueltardieu deleted the push-rxokqputnuzr branch January 22, 2025 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants