2
0
Fork 0
mirror of https://github.com/rust-lang/rust-clippy synced 2024-12-24 20:13:24 +00:00
rust-clippy/tests/ui/escape_analysis.rs

164 lines
2.8 KiB
Rust
Raw Normal View History

2018-10-06 16:18:06 +00: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.
#![feature(box_syntax)]
2017-09-18 10:47:33 +00:00
#![allow(clippy::borrowed_box, clippy::needless_pass_by_value, clippy::unused_unit)]
#![warn(clippy::boxed_local)]
2015-12-04 10:12:53 +00:00
#[derive(Clone)]
struct A;
impl A {
fn foo(&self){}
}
2016-02-01 19:37:07 +00:00
trait Z {
fn bar(&self);
}
impl Z for A {
fn bar(&self) {
//nothing
}
}
2015-12-04 10:12:53 +00:00
fn main() {
}
2016-02-01 19:37:07 +00:00
fn ok_box_trait(boxed_trait: &Box<Z>) {
let boxed_local = boxed_trait;
// done
}
2015-12-04 10:12:53 +00:00
fn warn_call() {
2017-02-08 13:58:07 +00:00
let x = box A;
2016-09-09 18:24:00 +00:00
x.foo();
2015-12-04 10:12:53 +00:00
}
2017-02-08 13:58:07 +00:00
fn warn_arg(x: Box<A>) {
2015-12-28 14:12:57 +00:00
x.foo();
}
fn nowarn_closure_arg() {
let x = Some(box A);
x.map_or((), |x| take_ref(&x));
}
2015-12-04 10:12:53 +00:00
fn warn_rename_call() {
let x = box A;
2017-02-08 13:58:07 +00:00
let y = x;
2015-12-04 10:12:53 +00:00
y.foo(); // via autoderef
}
fn warn_notuse() {
2017-02-08 13:58:07 +00:00
let bz = box A;
2015-12-04 10:12:53 +00:00
}
fn warn_pass() {
2017-02-08 13:58:07 +00:00
let bz = box A;
2015-12-04 10:12:53 +00:00
take_ref(&bz); // via deref coercion
}
fn nowarn_return() -> Box<A> {
box A // moved out, "escapes"
2015-12-04 10:12:53 +00:00
}
fn nowarn_move() {
let bx = box A;
drop(bx) // moved in, "escapes"
}
fn nowarn_call() {
let bx = box A;
bx.clone(); // method only available to Box, not via autoderef
}
fn nowarn_pass() {
let bx = box A;
take_box(&bx); // fn needs &Box
}
fn take_box(x: &Box<A>) {}
fn take_ref(x: &A) {}
fn nowarn_ref_take() {
// false positive, should actually warn
2017-02-08 13:58:07 +00:00
let x = box A;
2015-12-04 10:12:53 +00:00
let y = &x;
take_box(y);
}
fn nowarn_match() {
let x = box A; // moved into a match
match x {
y => drop(y)
}
}
fn warn_match() {
2017-02-08 13:58:07 +00:00
let x = box A;
2015-12-04 10:12:53 +00:00
match &x { // not moved
ref y => ()
}
2015-12-28 14:12:57 +00:00
}
2016-07-10 13:23:50 +00:00
fn nowarn_large_array() {
// should not warn, is large array
// and should not be on stack
let x = box [1; 10000];
match &x { // not moved
ref y => ()
}
}
/// ICE regression test
pub trait Foo {
type Item;
}
impl<'a> Foo for &'a () {
type Item = ();
}
pub struct PeekableSeekable<I: Foo> {
_peeked: I::Item,
}
2017-02-08 13:58:07 +00:00
pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {
}
/// Regression for #916, #1123
///
/// This shouldn't warn for `boxed_local`as the implementation of a trait
/// can't change much about the trait definition.
trait BoxedAction {
fn do_sth(self: Box<Self>);
}
impl BoxedAction for u64 {
fn do_sth(self: Box<Self>) {
println!("{}", *self)
}
}
/// Regression for #1478
///
/// This shouldn't warn for `boxed_local`as self itself is a box type.
trait MyTrait {
fn do_sth(self);
}
impl<T> MyTrait for Box<T> {
fn do_sth(self) {}
}