Skip to content

Commit

Permalink
Better error message for semicolon on the last line of a function
Browse files Browse the repository at this point in the history
  • Loading branch information
fhahn committed Jan 13, 2014
1 parent caf316a commit c74c854
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ fn visit_fn(v: &mut LivenessVisitor,

// check for various error conditions
lsets.visit_block(body, ());
lsets.check_ret(id, sp, fk, entry_ln);
lsets.check_ret(id, sp, fk, entry_ln, body);
lsets.warn_about_unused_args(decl, entry_ln);
}

Expand Down Expand Up @@ -1575,7 +1575,8 @@ impl Liveness {
id: NodeId,
sp: Span,
_fk: &FnKind,
entry_ln: LiveNode) {
entry_ln: LiveNode,
body: &Block) {
if self.live_on_entry(entry_ln, self.s.no_ret_var).is_some() {
// if no_ret_var is live, then we fall off the end of the
// function without any kind of return expression:
Expand All @@ -1588,9 +1589,30 @@ impl Liveness {
self.tcx.sess.span_err(
sp, "some control paths may return");
} else {
let ends_with_stmt = match body.expr {
None if body.stmts.len() > 0 =>
match body.stmts.last().node {
StmtSemi(e, _) => {
let t_stmt = ty::expr_ty(self.tcx, e);
ty::get(t_stmt).sty == ty::get(t_ret).sty
},
_ => false
},
_ => false
};
if ends_with_stmt {
let last_stmt = body.stmts.last();
let span_semicolon = Span {
lo: last_stmt.span.hi,
hi: last_stmt.span.hi,
expn_info: last_stmt.span.expn_info
};
self.tcx.sess.span_note(
span_semicolon, "consider removing this semicolon:");
}
self.tcx.sess.span_err(
sp, "not all control paths return a value");
}
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/test/compile-fail/liveness-return-last-stmt-semi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2014 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.
//
// regression test for #8005

#[feature(macro_rules)];

macro_rules! test ( () => { fn foo() -> int { 1i; } } ) //~ ERROR not all control paths return a value
//~^ NOTE consider removing this semicolon

fn no_return() -> int {} //~ ERROR not all control paths return a value

fn bar(x: u32) -> u32 { //~ ERROR not all control paths return a value
x * 2; //~ NOTE consider removing this semicolon
}

fn baz(x: u64) -> u32 { //~ ERROR not all control paths return a value
x * 2;
}

fn main() {
test!();
}

1 comment on commit c74c854

@alexcrichton
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+, thanks!

Please sign in to comment.