Skip to content

Use std::mem::take instead of std::mem::replace with default values#157307

Closed
theherrovn-sys wants to merge 1 commit into
rust-lang:mainfrom
theherrovn-sys:fix-157245-mem-take
Closed

Use std::mem::take instead of std::mem::replace with default values#157307
theherrovn-sys wants to merge 1 commit into
rust-lang:mainfrom
theherrovn-sys:fix-157245-mem-take

Conversation

@theherrovn-sys
Copy link
Copy Markdown

@theherrovn-sys theherrovn-sys commented Jun 2, 2026

Replace std::mem::replace(&mut x, Default::default()) and equivalent patterns (e.g. String::new(), Vec::new(), false) with the more idiomatic std::mem::take(&mut x) across the compiler source.

This improves code readability and aligns with Rust best practices, matching the clippy lint mem_replace_with_default.

Closes #157245.

Changes

File Change
compiler/rustc_span/src/lib.rs std::mem::replace(src, String::new())std::mem::take(src)
compiler/rustc_middle/src/dep_graph/serialized.rs mem::replace(&mut local.encoder.data, Vec::new())mem::take(&mut local.encoder.data)
compiler/rustc_passes/src/dead.rs mem::replace(&mut self.in_pat, false)mem::take(&mut self.in_pat)
compiler/rustc_passes/src/check_export.rs std::mem::replace(&mut self.seen_exportable_in_mod, false)std::mem::take(...)
compiler/rustc_mir_build/src/check_unsafety.rs std::mem::replace(&mut self.inside_adt, false)std::mem::take(...)
compiler/rustc_middle/src/ty/print/pretty.rs std::mem::replace(&mut self.in_value, false)std::mem::take(...)
compiler/rustc_builtin_macros/src/proc_macro_harness.rs mem::replace(&mut self.in_root, false)mem::take(&mut self.in_root)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 2, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 2, 2026

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @mejrs (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 17 candidates

@rustbot

This comment has been minimized.

Replace std::mem::take(&mut x, Default::default()) and equivalent
patterns (e.g. String::new(), Vec::new(), false) with the idiomatic
std::mem::take(&mut x) in the compiler source.

See rust-lang/rust issue 157245.
@oli-obk
Copy link
Copy Markdown
Contributor

oli-obk commented Jun 2, 2026

Hi,

For the last few months we have been receiving an increased amount of slop by users running bots that generate contributions, and even misconfiguring their bot so it will even send generated apology emails on top. We have no way to distinguish your PR from your other generated ones and no way to determine whether you actually want to contribute.

We are a community of contributors, not just a code repository. Thus we focus on contributors who desire to stay around and put in the work to produce high quality contributions or learn to do so.

We are thus banning you as per our policies (1) and contribution standards (2). You can contact the moderation team to discuss and possibly reconsider your ban.

Thanks for understanding

Oli in the name of the mod team

@oli-obk oli-obk closed this Jun 2, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 2, 2026
@mejrs mejrs removed their assignment Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use idiomatic std::mem::take instead of std::mem::replace

4 participants