Skip to content

Commit c3e4026

Browse files
authored
Rollup merge of rust-lang#71256 - cuviper:must_use_replace, r=estebank
Lint must_use on mem::replace This adds a hint on `mem::replace`, "if you don't need the old value, you can just assign the new value directly". This is in similar spirit to the `must_use` on `ManuallyDrop::take`.
2 parents 136fef2 + 7fca9f8 commit c3e4026

File tree

10 files changed

+15
-13
lines changed

10 files changed

+15
-13
lines changed

src/libcore/marker.rs

+1
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
709709
/// So this, for example, can only be done on types implementing `Unpin`:
710710
///
711711
/// ```rust
712+
/// # #![allow(unused_must_use)]
712713
/// use std::mem;
713714
/// use std::pin::Pin;
714715
///

src/libcore/mem/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ pub fn take<T: Default>(dest: &mut T) -> T {
808808
/// [`Clone`]: ../../std/clone/trait.Clone.html
809809
#[inline]
810810
#[stable(feature = "rust1", since = "1.0.0")]
811+
#[must_use = "if you don't need the old value, you can just assign the new value directly"]
811812
pub fn replace<T>(dest: &mut T, mut src: T) -> T {
812813
swap(dest, &mut src);
813814
src

src/librustc_parse/parser/expr.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,7 @@ impl<'a> Parser<'a> {
547547
// Rewind to before attempting to parse the type with generics, to recover
548548
// from situations like `x as usize < y` in which we first tried to parse
549549
// `usize < y` as a type with generic arguments.
550-
let parser_snapshot_after_type = self.clone();
551-
mem::replace(self, parser_snapshot_before_type);
550+
let parser_snapshot_after_type = mem::replace(self, parser_snapshot_before_type);
552551

553552
match self.parse_path(PathStyle::Expr) {
554553
Ok(path) => {
@@ -560,7 +559,7 @@ impl<'a> Parser<'a> {
560559
// example because `parse_ty_no_plus` returns `Err` on keywords,
561560
// but `parse_path` returns `Ok` on them due to error recovery.
562561
// Return original error and parser state.
563-
mem::replace(self, parser_snapshot_after_type);
562+
*self = parser_snapshot_after_type;
564563
return Err(type_err);
565564
}
566565
};
@@ -601,7 +600,7 @@ impl<'a> Parser<'a> {
601600
Err(mut path_err) => {
602601
// Couldn't parse as a path, return original error and parser state.
603602
path_err.cancel();
604-
mem::replace(self, parser_snapshot_after_type);
603+
*self = parser_snapshot_after_type;
605604
return Err(type_err);
606605
}
607606
}

src/librustc_parse/parser/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'a> Parser<'a> {
105105
}
106106
Err(mut err) => {
107107
err.cancel();
108-
std::mem::replace(self, snapshot);
108+
*self = snapshot;
109109
break;
110110
}
111111
}

src/librustc_parse/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ impl<'a> Parser<'a> {
16501650
// Recover from attempting to parse the argument as a type without pattern.
16511651
Err(mut err) => {
16521652
err.cancel();
1653-
mem::replace(self, parser_snapshot_before_ty);
1653+
*self = parser_snapshot_before_ty;
16541654
self.recover_arg_parse()?
16551655
}
16561656
}

src/librustc_parse/parser/stmt.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ impl<'a> Parser<'a> {
163163
Ok(ty) => (None, Some(ty)),
164164
Err(mut err) => {
165165
// Rewind to before attempting to parse the type and continue parsing.
166-
let parser_snapshot_after_type = self.clone();
167-
mem::replace(self, parser_snapshot_before_type);
166+
let parser_snapshot_after_type =
167+
mem::replace(self, parser_snapshot_before_type);
168168
if let Ok(snip) = self.span_to_snippet(pat.span) {
169169
err.span_label(pat.span, format!("while parsing the type for `{}`", snip));
170170
}
@@ -201,7 +201,7 @@ impl<'a> Parser<'a> {
201201
// Couldn't parse the type nor the initializer, only raise the type error and
202202
// return to the parser state before parsing the type as the initializer.
203203
// let x: <parse_error>;
204-
mem::replace(self, snapshot);
204+
*self = snapshot;
205205
return Err(ty_err);
206206
}
207207
(Err(err), None) => {

src/librustc_resolve/late/lifetimes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_span::symbol::{kw, sym};
2626
use rustc_span::Span;
2727
use std::borrow::Cow;
2828
use std::cell::Cell;
29-
use std::mem::{replace, take};
29+
use std::mem::take;
3030

3131
use log::debug;
3232

@@ -371,7 +371,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
371371
self.with(Scope::Body { id: body.id(), s: self.scope }, |_, this| {
372372
this.visit_body(body);
373373
});
374-
replace(&mut self.labels_in_fn, saved);
374+
self.labels_in_fn = saved;
375375
}
376376

377377
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {

src/libstd/thread/local.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ mod lazy {
301301
// value (an aliasing violation). To avoid setting the "I'm running a
302302
// destructor" flag we just use `mem::replace` which should sequence the
303303
// operations a little differently and make this safe to call.
304-
mem::replace(&mut *ptr, Some(value));
304+
let _ = mem::replace(&mut *ptr, Some(value));
305305

306306
// After storing `Some` we want to get a reference to the contents of
307307
// what we just stored. While we could use `unwrap` here and it should

src/test/ui/imports/import-in-block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
pub fn main() {
55
use std::mem::replace;
66
let mut x = 5;
7-
replace(&mut x, 6);
7+
let _ = replace(&mut x, 6);
88
{
99
use std::mem::*;
1010
let mut y = 6;

src/test/ui/issues/issue-23611-enum-swap-in-drop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ impl<'a> Drop for E<'a> {
153153
}
154154
};
155155

156+
#[allow(unused_must_use)]
156157
if do_drop {
157158
mem::replace(self, E::A(GaspA(f_a, 0xA3A0, log, D::new("drop", 6, log)), true));
158159
}

0 commit comments

Comments
 (0)