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

Lint must_use on mem::replace #71256

Merged
merged 3 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libcore/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
/// So this, for example, can only be done on types implementing `Unpin`:
///
/// ```rust
/// # #![allow(unused_must_use)]
/// use std::mem;
/// use std::pin::Pin;
///
Expand Down
1 change: 1 addition & 0 deletions src/libcore/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ pub fn take<T: Default>(dest: &mut T) -> T {
/// [`Clone`]: ../../std/clone/trait.Clone.html
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "if you don't need the old value, you can just assign the new value directly"]
pub fn replace<T>(dest: &mut T, mut src: T) -> T {
swap(dest, &mut src);
src
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ impl<'a> Parser<'a> {
// Rewind to before attempting to parse the type with generics, to recover
// from situations like `x as usize < y` in which we first tried to parse
// `usize < y` as a type with generic arguments.
let parser_snapshot_after_type = self.clone();
mem::replace(self, parser_snapshot_before_type);
let parser_snapshot_after_type = mem::replace(self, parser_snapshot_before_type);

match self.parse_path(PathStyle::Expr) {
Ok(path) => {
Expand All @@ -560,7 +559,7 @@ impl<'a> Parser<'a> {
// example because `parse_ty_no_plus` returns `Err` on keywords,
// but `parse_path` returns `Ok` on them due to error recovery.
// Return original error and parser state.
mem::replace(self, parser_snapshot_after_type);
*self = parser_snapshot_after_type;
return Err(type_err);
}
};
Expand Down Expand Up @@ -601,7 +600,7 @@ impl<'a> Parser<'a> {
Err(mut path_err) => {
// Couldn't parse as a path, return original error and parser state.
path_err.cancel();
mem::replace(self, parser_snapshot_after_type);
*self = parser_snapshot_after_type;
return Err(type_err);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<'a> Parser<'a> {
}
Err(mut err) => {
err.cancel();
std::mem::replace(self, snapshot);
*self = snapshot;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ impl<'a> Parser<'a> {
// Recover from attempting to parse the argument as a type without pattern.
Err(mut err) => {
err.cancel();
mem::replace(self, parser_snapshot_before_ty);
*self = parser_snapshot_before_ty;
self.recover_arg_parse()?
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_parse/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ impl<'a> Parser<'a> {
Ok(ty) => (None, Some(ty)),
Err(mut err) => {
// Rewind to before attempting to parse the type and continue parsing.
let parser_snapshot_after_type = self.clone();
mem::replace(self, parser_snapshot_before_type);
let parser_snapshot_after_type =
mem::replace(self, parser_snapshot_before_type);
if let Ok(snip) = self.span_to_snippet(pat.span) {
err.span_label(pat.span, format!("while parsing the type for `{}`", snip));
}
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<'a> Parser<'a> {
// Couldn't parse the type nor the initializer, only raise the type error and
// return to the parser state before parsing the type as the initializer.
// let x: <parse_error>;
mem::replace(self, snapshot);
*self = snapshot;
return Err(ty_err);
}
(Err(err), None) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use std::borrow::Cow;
use std::cell::Cell;
use std::mem::{replace, take};
use std::mem::take;

use log::debug;

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

fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ mod lazy {
// value (an aliasing violation). To avoid setting the "I'm running a
// destructor" flag we just use `mem::replace` which should sequence the
// operations a little differently and make this safe to call.
mem::replace(&mut *ptr, Some(value));
let _ = mem::replace(&mut *ptr, Some(value));

// After storing `Some` we want to get a reference to the contents of
// what we just stored. While we could use `unwrap` here and it should
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/imports/import-in-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub fn main() {
use std::mem::replace;
let mut x = 5;
replace(&mut x, 6);
let _ = replace(&mut x, 6);
{
use std::mem::*;
let mut y = 6;
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-23611-enum-swap-in-drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl<'a> Drop for E<'a> {
}
};

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