about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2016-04-05 16:43:22 +0530
committerManish Goregaokar <manishsmail@gmail.com>2016-04-05 16:43:22 +0530
commit7d63422bb279f2fc430f38fb1d6c90987c86e0bf (patch)
tree1a6068bf7ddaefd62b007434ba2bb4d411527688
parentb6c1f1cf3f392af6bad6018a9e8fd25cf608c475 (diff)
parent99b6247166f8a0119b446e8877f5fca9b04187b0 (diff)
downloadrust-7d63422bb279f2fc430f38fb1d6c90987c86e0bf.tar.gz
rust-7d63422bb279f2fc430f38fb1d6c90987c86e0bf.zip
Rollup merge of #32710 - jonas-schievink:consider-last-semi, r=nagisa
Fix "consider removing this semicolon" help

Check last statement in a block, not the first.

Example of current weirdness: http://is.gd/w80J9h

The help was only rarely emitted, and if so, often incorrectly (see above playpen). It was basically only useful with single-statement functions.
-rw-r--r--src/librustc/middle/liveness.rs4
-rw-r--r--src/test/compile-fail/consider-removing-last-semi.rs23
2 files changed, 25 insertions, 2 deletions
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..02148a138c9
--- /dev/null
+++ b/src/test/compile-fail/consider-removing-last-semi.rs
@@ -0,0 +1,23 @@
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 g() -> String {  //~ ERROR E0269
+                    //~^ HELP detailed explanation
+    "this won't work".to_string();
+    "removeme".to_string(); //~ HELP consider removing this semicolon
+}
+
+fn main() {}