From 21f3248f2c33918129db9622904d3dc6976bd244 Mon Sep 17 00:00:00 2001 From: Dylan McKay Date: Tue, 27 Oct 2015 20:20:40 +1300 Subject: [PATCH 1/2] 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. --- src/librustc/middle/liveness.rs | 9 ++++++++- src/test/compile-fail/missing-semicolon.rs | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/missing-semicolon.rs diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index 3b56597d353a3..40859725e5334 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -1483,7 +1483,14 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { None if !body.stmts.is_empty() => match body.stmts.first().unwrap().node { hir::StmtSemi(ref e, _) => { - self.ir.tcx.expr_ty(&**e) == t_ret + match (&self.ir.tcx.expr_ty(&**e).sty, &t_ret.sty) { + // Type inference causes unsuffixed integer literals + // to be evaluated as i32 values. We still want to + // inform the user of a semicolon in this case. + (&ty::TyInt(ast::TyI32),&ty::TyInt(..)) => true, + (&ty::TyInt(ast::TyI32),&ty::TyUint(..)) => true, + (a,b) => (a == b), + } }, _ => false }, diff --git a/src/test/compile-fail/missing-semicolon.rs b/src/test/compile-fail/missing-semicolon.rs new file mode 100644 index 0000000000000..8104f89bcded8 --- /dev/null +++ b/src/test/compile-fail/missing-semicolon.rs @@ -0,0 +1,17 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// error-pattern:consider removing this semicolon + +fn foo() -> u16 { + 1234; +} + +fn main() { } From b2f72c351c0a9af80acee7b618bcf876f2ee58d6 Mon Sep 17 00:00:00 2001 From: Dylan McKay Date: Tue, 27 Oct 2015 22:29:19 +1300 Subject: [PATCH 2/2] Add spaces after commas --- src/librustc/middle/liveness.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index 40859725e5334..45ccbea206ba5 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -1487,9 +1487,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { // Type inference causes unsuffixed integer literals // to be evaluated as i32 values. We still want to // inform the user of a semicolon in this case. - (&ty::TyInt(ast::TyI32),&ty::TyInt(..)) => true, - (&ty::TyInt(ast::TyI32),&ty::TyUint(..)) => true, - (a,b) => (a == b), + (&ty::TyInt(ast::TyI32), &ty::TyInt(..)) => true, + (&ty::TyInt(ast::TyI32), &ty::TyUint(..)) => true, + (a, b) => (a == b), } }, _ => false