Skip to content

Commit c2fde5a

Browse files
committed
Add tests
1 parent b8c75e5 commit c2fde5a

File tree

3 files changed

+167
-2
lines changed

3 files changed

+167
-2
lines changed

src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1091,14 +1091,14 @@ fn check_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cast: &CastCheck<'tcx>) {
10911091
fcx.tcx().sess.add_lint(lint::builtin::TRIVIAL_NUMERIC_CAST,
10921092
e.id,
10931093
span,
1094-
format!("trivial numeric cast: {} as {}",
1094+
format!("trivial numeric cast: `{}` as `{}`",
10951095
fcx.infcx().ty_to_string(t_e),
10961096
fcx.infcx().ty_to_string(t_1)));
10971097
} else {
10981098
fcx.tcx().sess.add_lint(lint::builtin::TRIVIAL_CAST,
10991099
e.id,
11001100
span,
1101-
format!("trivial cast: {} as {}",
1101+
format!("trivial cast: `{}` as `{}`",
11021102
fcx.infcx().ty_to_string(t_e),
11031103
fcx.infcx().ty_to_string(t_1)));
11041104
}
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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_cast and trivial_numeric_cast lints. For each error we also
12+
// check that the cast can be done using just coercion.
13+
14+
#![deny(trivial_cast, trivial_numeric_cast)]
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]>; //~ERROR trivial cast: `Box<[u32; 3]>` as `Box<[u32]>`
56+
let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
57+
let _: Box<[u32]> = x;
58+
59+
// unsize trait
60+
let x: &Bar = &Bar;
61+
let _ = x as &Foo; //~ERROR trivial cast: `&Bar` as `&Foo`
62+
let _ = x as *const Foo; //~ERROR trivial cast: `&Bar` as `*const Foo`
63+
let _: &Foo = x;
64+
let _: *const Foo = x;
65+
66+
let x: &mut Bar = &mut Bar;
67+
let _ = x as &mut Foo; //~ERROR trivial cast: `&mut Bar` as `&mut Foo`
68+
let _ = x as *mut Foo; //~ERROR trivial cast: `&mut Bar` as `*mut Foo`
69+
let _: &mut Foo = x;
70+
let _: *mut Foo = x;
71+
72+
let x: Box<Bar> = Box::new(Bar);
73+
let _ = x as Box<Foo>; //~ERROR trivial cast: `Box<Bar>` as `Box<Foo>`
74+
let x: Box<Bar> = Box::new(Bar);
75+
let _: Box<Foo> = x;
76+
77+
// functions
78+
fn baz(_x: i32) {}
79+
let _ = &baz as &Fn(i32); //~ERROR trivial cast: `&fn(i32) {main::baz}` as `&core::ops::Fn(i32)`
80+
let _: &Fn(i32) = &baz;
81+
let x = |_x: i32| {};
82+
let _ = &x as &Fn(i32); //~ERROR trivial cast
83+
let _: &Fn(i32) = &x;
84+
}
85+
86+
// subtyping
87+
pub fn test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar) {
88+
let _ = a as &'a Bar; //~ERROR trivial cast
89+
let _: &'a Bar = a;
90+
let _ = b as &'a Bar; //~ERROR trivial cast
91+
let _: &'a Bar = b;
92+
let _ = b as &'b Bar; //~ERROR trivial cast
93+
let _: &'b Bar = b;
94+
}

src/test/run-pass/trivial_casts.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 that all coercions can actually be done using casts (modulo the lints).
12+
13+
#![allow(trivial_cast, trivial_numeric_cast)]
14+
15+
trait Foo {
16+
fn foo(&self) {}
17+
}
18+
19+
pub struct Bar;
20+
21+
impl Foo for Bar {}
22+
23+
pub fn main() {
24+
// Numeric
25+
let _ = 42_i32 as i32;
26+
let _ = 42_u8 as u8;
27+
28+
// & to * pointers
29+
let x: &u32 = &42;
30+
let _ = x as *const u32;
31+
32+
let x: &mut u32 = &mut 42;
33+
let _ = x as *mut u32;
34+
35+
// unsize array
36+
let x: &[u32; 3] = &[42, 43, 44];
37+
let _ = x as &[u32];
38+
let _ = x as *const [u32];
39+
40+
let x: &mut [u32; 3] = &mut [42, 43, 44];
41+
let _ = x as &mut [u32];
42+
let _ = x as *mut [u32];
43+
44+
let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
45+
let _ = x as Box<[u32]>;
46+
47+
// unsize trait
48+
let x: &Bar = &Bar;
49+
let _ = x as &Foo;
50+
let _ = x as *const Foo;
51+
52+
let x: &mut Bar = &mut Bar;
53+
let _ = x as &mut Foo;
54+
let _ = x as *mut Foo;
55+
56+
let x: Box<Bar> = Box::new(Bar);
57+
let _ = x as Box<Foo>;
58+
59+
// functions
60+
fn baz(_x: i32) {}
61+
let _ = &baz as &Fn(i32);
62+
let x = |_x: i32| {};
63+
let _ = &x as &Fn(i32);
64+
}
65+
66+
// subtyping
67+
pub fn test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar) {
68+
let _ = a as &'a Bar;
69+
let _ = b as &'a Bar;
70+
let _ = b as &'b Bar;
71+
}

0 commit comments

Comments
 (0)