From e6bfe4b514c9b7201c47726f5dde4c5aee53d054 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 13 Sep 2016 12:41:37 +0200 Subject: [PATCH] also lint diverging methods --- clippy_lints/src/eval_order_dependence.rs | 10 +++++++++- tests/compile-fail/diverging_sub_expression.rs | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/eval_order_dependence.rs b/clippy_lints/src/eval_order_dependence.rs index f357cd8f6f9..e53c830e11d 100644 --- a/clippy_lints/src/eval_order_dependence.rs +++ b/clippy_lints/src/eval_order_dependence.rs @@ -133,7 +133,15 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DivergenceVisitor<'a, 'tcx> { }, _ => {}, }, - ExprMethodCall(..) => { /* TODO */ }, + ExprMethodCall(..) => { + let method_call = ty::MethodCall::expr(e.id); + let borrowed_table = self.0.tcx.tables.borrow(); + let method_type = borrowed_table.method_map.get(&method_call).expect("This should never happen."); + let result_ty = method_type.ty.fn_ret(); + if let ty::TyNever = self.0.tcx.erase_late_bound_regions(&result_ty).sty { + self.report_diverging_sub_expr(e); + } + }, _ => { // do not lint expressions referencing objects of type `!`, as that required a diverging expression to begin with }, diff --git a/tests/compile-fail/diverging_sub_expression.rs b/tests/compile-fail/diverging_sub_expression.rs index 20b284732b0..d82832ef11f 100644 --- a/tests/compile-fail/diverging_sub_expression.rs +++ b/tests/compile-fail/diverging_sub_expression.rs @@ -5,10 +5,17 @@ #[allow(empty_loop)] fn diverge() -> ! { loop {} } +struct A; + +impl A { + fn foo(&self) -> ! { diverge() } +} + #[allow(unused_variables, unnecessary_operation)] fn main() { let b = true; b || diverge(); //~ ERROR sub-expression diverges + b || A.foo(); //~ ERROR sub-expression diverges let y = (5, diverge(), 6); //~ ERROR sub-expression diverges println!("{}", y.1); }