Skip to content

Commit e6bfe4b

Browse files
committed
also lint diverging methods
1 parent a225728 commit e6bfe4b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clippy_lints/src/eval_order_dependence.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,15 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DivergenceVisitor<'a, 'tcx> {
133133
},
134134
_ => {},
135135
},
136-
ExprMethodCall(..) => { /* TODO */ },
136+
ExprMethodCall(..) => {
137+
let method_call = ty::MethodCall::expr(e.id);
138+
let borrowed_table = self.0.tcx.tables.borrow();
139+
let method_type = borrowed_table.method_map.get(&method_call).expect("This should never happen.");
140+
let result_ty = method_type.ty.fn_ret();
141+
if let ty::TyNever = self.0.tcx.erase_late_bound_regions(&result_ty).sty {
142+
self.report_diverging_sub_expr(e);
143+
}
144+
},
137145
_ => {
138146
// do not lint expressions referencing objects of type `!`, as that required a diverging expression to begin with
139147
},

tests/compile-fail/diverging_sub_expression.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
#[allow(empty_loop)]
66
fn diverge() -> ! { loop {} }
77

8+
struct A;
9+
10+
impl A {
11+
fn foo(&self) -> ! { diverge() }
12+
}
13+
814
#[allow(unused_variables, unnecessary_operation)]
915
fn main() {
1016
let b = true;
1117
b || diverge(); //~ ERROR sub-expression diverges
18+
b || A.foo(); //~ ERROR sub-expression diverges
1219
let y = (5, diverge(), 6); //~ ERROR sub-expression diverges
1320
println!("{}", y.1);
1421
}

0 commit comments

Comments
 (0)