From 0163ccc36bdd16f0d5c92dfb5625eb1121d16479 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 3 Apr 2016 22:10:21 +0200 Subject: [PATCH 1/2] Fix "consider removing this semicolon" help Check last statement in a block, not the first --- src/librustc/middle/liveness.rs | 4 ++-- .../compile-fail/consider-removing-last-semi.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/test/compile-fail/consider-removing-last-semi.rs diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index cc37ee7dbda..4451e7ac472 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -1502,7 +1502,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { } else { let ends_with_stmt = match body.expr { None if !body.stmts.is_empty() => - match body.stmts.first().unwrap().node { + match body.stmts.last().unwrap().node { hir::StmtSemi(ref e, _) => { self.ir.tcx.expr_ty(&e) == t_ret }, @@ -1515,7 +1515,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { E0269, "not all control paths return a value"); if ends_with_stmt { - let last_stmt = body.stmts.first().unwrap(); + let last_stmt = body.stmts.last().unwrap(); let original_span = original_sp(self.ir.tcx.sess.codemap(), last_stmt.span, sp); let span_semicolon = Span { diff --git a/src/test/compile-fail/consider-removing-last-semi.rs b/src/test/compile-fail/consider-removing-last-semi.rs new file mode 100644 index 00000000000..a25f0b17335 --- /dev/null +++ b/src/test/compile-fail/consider-removing-last-semi.rs @@ -0,0 +1,17 @@ +// Copyright 2016 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. + +fn f() -> String { //~ ERROR E0269 + //~^ HELP detailed explanation + 0u8; + "bla".to_string(); //~ HELP consider removing this semicolon +} + +fn main() {} From 99b6247166f8a0119b446e8877f5fca9b04187b0 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 4 Apr 2016 13:53:04 +0200 Subject: [PATCH 2/2] Beef up test --- src/test/compile-fail/consider-removing-last-semi.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/compile-fail/consider-removing-last-semi.rs b/src/test/compile-fail/consider-removing-last-semi.rs index a25f0b17335..02148a138c9 100644 --- a/src/test/compile-fail/consider-removing-last-semi.rs +++ b/src/test/compile-fail/consider-removing-last-semi.rs @@ -14,4 +14,10 @@ fn f() -> String { //~ ERROR E0269 "bla".to_string(); //~ HELP consider removing this semicolon } +fn g() -> String { //~ ERROR E0269 + //~^ HELP detailed explanation + "this won't work".to_string(); + "removeme".to_string(); //~ HELP consider removing this semicolon +} + fn main() {}