2018-07-28 17:34:52 +02:00
|
|
|
#![warn(clippy::drop_ref, clippy::forget_ref)]
|
|
|
|
#![allow(clippy::toplevel_ref_arg, clippy::similar_names, clippy::needless_pass_by_value)]
|
2016-12-30 03:45:41 +01:00
|
|
|
|
|
|
|
use std::mem::{drop, forget};
|
|
|
|
|
|
|
|
struct SomeStruct;
|
|
|
|
|
|
|
|
fn main() {
|
2017-02-08 14:58:07 +01:00
|
|
|
drop(&SomeStruct);
|
|
|
|
forget(&SomeStruct);
|
2016-12-30 03:45:41 +01:00
|
|
|
|
|
|
|
let mut owned1 = SomeStruct;
|
2017-02-08 14:58:07 +01:00
|
|
|
drop(&owned1);
|
|
|
|
drop(&&owned1);
|
|
|
|
drop(&mut owned1);
|
2016-12-30 03:45:41 +01:00
|
|
|
drop(owned1); //OK
|
|
|
|
let mut owned2 = SomeStruct;
|
2017-02-08 14:58:07 +01:00
|
|
|
forget(&owned2);
|
|
|
|
forget(&&owned2);
|
|
|
|
forget(&mut owned2);
|
2016-12-30 03:45:41 +01:00
|
|
|
forget(owned2); //OK
|
|
|
|
|
|
|
|
let reference1 = &SomeStruct;
|
2017-02-08 14:58:07 +01:00
|
|
|
drop(reference1);
|
|
|
|
forget(&*reference1);
|
2016-12-30 03:45:41 +01:00
|
|
|
|
|
|
|
let reference2 = &mut SomeStruct;
|
2017-02-08 14:58:07 +01:00
|
|
|
drop(reference2);
|
2016-12-30 03:45:41 +01:00
|
|
|
let reference3 = &mut SomeStruct;
|
2017-02-08 14:58:07 +01:00
|
|
|
forget(reference3);
|
2016-12-30 03:45:41 +01:00
|
|
|
|
|
|
|
let ref reference4 = SomeStruct;
|
2017-02-08 14:58:07 +01:00
|
|
|
drop(reference4);
|
|
|
|
forget(reference4);
|
2016-12-30 03:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn test_generic_fn_drop<T>(val: T) {
|
2017-02-08 14:58:07 +01:00
|
|
|
drop(&val);
|
2016-12-30 03:45:41 +01:00
|
|
|
drop(val); //OK
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn test_generic_fn_forget<T>(val: T) {
|
2017-02-08 14:58:07 +01:00
|
|
|
forget(&val);
|
2016-12-30 03:45:41 +01:00
|
|
|
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 14:58:07 +01:00
|
|
|
std::mem::drop(&SomeStruct);
|
2016-12-30 03:45:41 +01:00
|
|
|
fn forget<T>(_val: T) {}
|
|
|
|
forget(&SomeStruct); //OK; call to unrelated function which happens to have the same name
|
2017-02-08 14:58:07 +01:00
|
|
|
std::mem::forget(&SomeStruct);
|
2016-12-30 03:45:41 +01:00
|
|
|
}
|
2019-07-31 17:38:08 +02:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Error;
|
|
|
|
fn produce_half_owl_error() -> Result<(), Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn produce_half_owl_ok() -> Result<bool, ()> {
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn test_owl_result() -> Result<(), ()> {
|
|
|
|
produce_half_owl_error().map_err(|_| ())?;
|
|
|
|
produce_half_owl_ok().map(|_| ())?;
|
|
|
|
// the following should not be linted,
|
|
|
|
// we should not force users to use toilet closures
|
|
|
|
// to produce owl results when drop is more convenient
|
|
|
|
produce_half_owl_error().map_err(drop)?;
|
|
|
|
produce_half_owl_ok().map_err(drop)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn test_owl_result_2() -> Result<u8, ()> {
|
|
|
|
produce_half_owl_error().map_err(|_| ())?;
|
|
|
|
produce_half_owl_ok().map(|_| ())?;
|
|
|
|
// the following should not be linted,
|
|
|
|
// we should not force users to use toilet closures
|
|
|
|
// to produce owl results when drop is more convenient
|
|
|
|
produce_half_owl_error().map_err(drop)?;
|
|
|
|
produce_half_owl_ok().map(drop)?;
|
|
|
|
Ok(1)
|
|
|
|
}
|