rust/tests/ui/drop_forget_ref.rs

71 lines
1.7 KiB
Rust
Raw Normal View History

2018-10-06 11:18:06 -05:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-10-11 05:16:22 -05:00
2017-09-18 05:47:33 -05:00
2018-07-28 10:34:52 -05:00
#![warn(clippy::drop_ref, clippy::forget_ref)]
#![allow(clippy::toplevel_ref_arg, clippy::similar_names, clippy::needless_pass_by_value)]
use std::mem::{drop, forget};
struct SomeStruct;
fn main() {
2017-02-08 07:58:07 -06:00
drop(&SomeStruct);
forget(&SomeStruct);
let mut owned1 = SomeStruct;
2017-02-08 07:58:07 -06:00
drop(&owned1);
drop(&&owned1);
drop(&mut owned1);
drop(owned1); //OK
let mut owned2 = SomeStruct;
2017-02-08 07:58:07 -06:00
forget(&owned2);
forget(&&owned2);
forget(&mut owned2);
forget(owned2); //OK
let reference1 = &SomeStruct;
2017-02-08 07:58:07 -06:00
drop(reference1);
forget(&*reference1);
let reference2 = &mut SomeStruct;
2017-02-08 07:58:07 -06:00
drop(reference2);
let reference3 = &mut SomeStruct;
2017-02-08 07:58:07 -06:00
forget(reference3);
let ref reference4 = SomeStruct;
2017-02-08 07:58:07 -06:00
drop(reference4);
forget(reference4);
}
#[allow(dead_code)]
fn test_generic_fn_drop<T>(val: T) {
2017-02-08 07:58:07 -06:00
drop(&val);
drop(val); //OK
}
#[allow(dead_code)]
fn test_generic_fn_forget<T>(val: T) {
2017-02-08 07:58:07 -06:00
forget(&val);
forget(val); //OK
}
#[allow(dead_code)]
fn test_similarly_named_function() {
fn drop<T>(_val: T) {}
drop(&SomeStruct); //OK; call to unrelated function which happens to have the same name
2017-02-08 07:58:07 -06:00
std::mem::drop(&SomeStruct);
fn forget<T>(_val: T) {}
forget(&SomeStruct); //OK; call to unrelated function which happens to have the same name
2017-02-08 07:58:07 -06:00
std::mem::forget(&SomeStruct);
}