Skip to content

Commit 21f3248

Browse files
author
Dylan McKay
committed
Print missing semicolon warning for type-inferred ints
We only printed the extra semicolon warning if the type of the last statement was the same as the return type of the function. If a semicolon was appended, then the code fn foo() -> u16 { 1234; } Would not print the extra semicolon warning because without knowing that 1234 is the return value, the inference engine would take 1234 to be an i32. This adds extra logic to recognize this case.
1 parent cb591e5 commit 21f3248

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/librustc/middle/liveness.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,14 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14831483
None if !body.stmts.is_empty() =>
14841484
match body.stmts.first().unwrap().node {
14851485
hir::StmtSemi(ref e, _) => {
1486-
self.ir.tcx.expr_ty(&**e) == t_ret
1486+
match (&self.ir.tcx.expr_ty(&**e).sty, &t_ret.sty) {
1487+
// Type inference causes unsuffixed integer literals
1488+
// to be evaluated as i32 values. We still want to
1489+
// inform the user of a semicolon in this case.
1490+
(&ty::TyInt(ast::TyI32),&ty::TyInt(..)) => true,
1491+
(&ty::TyInt(ast::TyI32),&ty::TyUint(..)) => true,
1492+
(a,b) => (a == b),
1493+
}
14871494
},
14881495
_ => false
14891496
},
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012 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+
// error-pattern:consider removing this semicolon
12+
13+
fn foo() -> u16 {
14+
1234;
15+
}
16+
17+
fn main() { }

0 commit comments

Comments
 (0)