]> git.lizzy.rs Git - rust.git/blob - src/test/ui/trivial_casts.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / trivial_casts.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test the trivial_casts and trivial_numeric_casts lints. For each error we also
12 // check that the cast can be done using just coercion.
13
14 #![deny(trivial_casts, trivial_numeric_casts)]
15
16 trait Foo {
17     fn foo(&self) {}
18 }
19
20 pub struct Bar;
21
22 impl Foo for Bar {}
23
24 pub fn main() {
25     // Numeric
26     let _ = 42_i32 as i32; //~ ERROR trivial numeric cast: `i32` as `i32`
27     let _: i32 = 42_i32;
28
29     let _ = 42_u8 as u8; //~ ERROR trivial numeric cast: `u8` as `u8`
30     let _: u8 = 42_u8;
31
32     // & to * pointers
33     let x: &u32 = &42;
34     let _ = x as *const u32; //~ERROR trivial cast: `&u32` as `*const u32`
35     let _: *const u32 = x;
36
37     let x: &mut u32 = &mut 42;
38     let _ = x as *mut u32; //~ERROR trivial cast: `&mut u32` as `*mut u32`
39     let _: *mut u32 = x;
40
41     // unsize array
42     let x: &[u32; 3] = &[42, 43, 44];
43     let _ = x as &[u32]; //~ERROR trivial cast: `&[u32; 3]` as `&[u32]`
44     let _ = x as *const [u32]; //~ERROR trivial cast: `&[u32; 3]` as `*const [u32]`
45     let _: &[u32] = x;
46     let _: *const [u32] = x;
47
48     let x: &mut [u32; 3] = &mut [42, 43, 44];
49     let _ = x as &mut [u32]; //~ERROR trivial cast: `&mut [u32; 3]` as `&mut [u32]`
50     let _ = x as *mut [u32]; //~ERROR trivial cast: `&mut [u32; 3]` as `*mut [u32]`
51     let _: &mut [u32] = x;
52     let _: *mut [u32] = x;
53
54     let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
55     let _ = x as Box<[u32]>;
56     //~^ ERROR trivial cast: `std::boxed::Box<[u32; 3]>` as `std::boxed::Box<[u32]>`
57     let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
58     let _: Box<[u32]> = x;
59
60     // unsize trait
61     let x: &Bar = &Bar;
62     let _ = x as &Foo; //~ERROR trivial cast: `&Bar` as `&dyn Foo`
63     let _ = x as *const Foo; //~ERROR trivial cast: `&Bar` as `*const dyn Foo`
64     let _: &Foo = x;
65     let _: *const Foo = x;
66
67     let x: &mut Bar = &mut Bar;
68     let _ = x as &mut Foo; //~ERROR trivial cast: `&mut Bar` as `&mut dyn Foo`
69     let _ = x as *mut Foo; //~ERROR trivial cast: `&mut Bar` as `*mut dyn Foo`
70     let _: &mut Foo = x;
71     let _: *mut Foo = x;
72
73     let x: Box<Bar> = Box::new(Bar);
74     let _ = x as Box<Foo>; //~ERROR `std::boxed::Box<Bar>` as `std::boxed::Box<dyn Foo>`
75     let x: Box<Bar> = Box::new(Bar);
76     let _: Box<Foo> = x;
77
78     // functions
79     fn baz(_x: i32) {}
80     let _ = &baz as &Fn(i32); //~ERROR `&fn(i32) {main::baz}` as `&dyn std::ops::Fn(i32)`
81     let _: &Fn(i32) = &baz;
82     let x = |_x: i32| {};
83     let _ = &x as &Fn(i32); //~ERROR trivial cast
84     let _: &Fn(i32) = &x;
85 }
86
87 // subtyping
88 pub fn test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar) {
89     let _ = a as &'a Bar; //~ERROR trivial cast
90     let _: &'a Bar = a;
91     let _ = b as &'a Bar; //~ERROR trivial cast
92     let _: &'a Bar = b;
93     let _ = b as &'b Bar; //~ERROR trivial cast
94     let _: &'b Bar = b;
95 }