Skip to content

Commit af91d99

Browse files
committed
Use into for casting when possible
1 parent aec1623 commit af91d99

File tree

7 files changed

+205
-172
lines changed

7 files changed

+205
-172
lines changed

src/librustc/hir/mod.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,37 +1173,6 @@ impl fmt::Debug for Expr {
11731173
}
11741174
}
11751175

1176-
impl Expr {
1177-
1178-
/// If casting this expression to a given numeric type would be appropriate in case of a type
1179-
/// mismatch.
1180-
///
1181-
/// We want to minimize the amount of casting operations that are suggested, as it can be a
1182-
/// lossy operation with potentially bad side effects, so we only suggest when encountering an
1183-
/// expression that indicates that the original type couldn't be directly changed.
1184-
pub fn could_cast_in_type_mismatch(&self) -> bool {
1185-
match self.node {
1186-
ExprCall(..) |
1187-
ExprMethodCall(..) |
1188-
ExprBinary(..) |
1189-
ExprField(..) |
1190-
ExprTupField(..) |
1191-
ExprIndex(..) |
1192-
ExprPath(..) => true,
1193-
_ => false,
1194-
}
1195-
}
1196-
1197-
pub fn needs_parens_around_cast(&self) -> bool {
1198-
match self.node {
1199-
ExprBinary(..) |
1200-
ExprCast(..) |
1201-
ExprType(..) => true,
1202-
_ => false,
1203-
}
1204-
}
1205-
}
1206-
12071176
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
12081177
pub enum Expr_ {
12091178
/// A `box x` expression.

src/librustc_typeck/check/demand.rs

Lines changed: 154 additions & 92 deletions
Large diffs are not rendered by default.

src/test/ui/mismatched_types/issue-26480.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0308]: mismatched types
88
| -------------- in this macro invocation
99
help: you can cast an `usize` to `u64`, which will truncate or zero-extend depending on the bit width of `usize`
1010
|
11-
26 | ($arr.len() * size_of($arr[0]) as )u64); //~ ERROR mismatched types
11+
26 | ($arr.len() * size_of($arr[0])) as u64); //~ ERROR mismatched types
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error[E0605]: non-primitive cast: `{integer}` as `()`

src/test/ui/suggestions/numeric-cast-2.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ fn foo() -> i32 {
1313
}
1414
fn main() {
1515
let x: u32 = foo();
16+
//~^ ERROR mismatched types
1617
let z: i32 = x + x;
18+
//~^ ERROR mismatched types
1719
}

src/test/ui/suggestions/numeric-cast-2.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ help: you can cast an `i32` to `u32`, which will sign-extend the source value
99
| ^^^^^^^^^^^^
1010

1111
error[E0308]: mismatched types
12-
--> $DIR/numeric-cast-2.rs:16:18
12+
--> $DIR/numeric-cast-2.rs:17:18
1313
|
14-
16 | let z: i32 = x + x;
14+
17 | let z: i32 = x + x;
1515
| ^^^^^ expected i32, found u32
1616
help: you can cast an `u32` to `i32`, which will truncate the source value
1717
|
18-
16 | let z: i32 = (x + x as )i32;
18+
17 | let z: i32 = (x + x) as i32;
1919
| ^^^^^^^^^^^^^^
2020

2121
error: aborting due to 2 previous errors

src/test/ui/suggestions/numeric-cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,6 @@ fn main() {
331331
//~^ ERROR mismatched types
332332
foo::<f32>(x_f64);
333333
//~^ ERROR mismatched types
334-
//~| WARN casting here will cause Undefined Behavior
334+
//~| WARN casting here will cause undefined behavior
335335
foo::<f32>(x_f32);
336336
}

src/test/ui/suggestions/numeric-cast.stderr

Lines changed: 44 additions & 44 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)