Skip to content

Commit faa04a8

Browse files
committed
Auto merge of #26666 - huonw:unc-rec, r=alexcrichton
This fixes two false positives for the unconditional recursion lint, when functions use themselves (or almost-themselves) internally, without actually being recursive. ````rust fn main() { let _ = main; } ``` ```rust trait Bar { fn method<T: Bar>(&self, x: &T) { x.method(x) } } ```
2 parents 9a26e49 + 900af2c commit faa04a8

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/librustc_lint/builtin.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,8 +1973,13 @@ impl LintPass for UnconditionalRecursion {
19731973
fn_id: ast::NodeId,
19741974
_: ast::Ident,
19751975
id: ast::NodeId) -> bool {
1976-
tcx.def_map.borrow().get(&id)
1977-
.map_or(false, |def| def.def_id() == local_def(fn_id))
1976+
match tcx.map.get(id) {
1977+
ast_map::NodeExpr(&ast::Expr { node: ast::ExprCall(ref callee, _), .. }) => {
1978+
tcx.def_map.borrow().get(&callee.id)
1979+
.map_or(false, |def| def.def_id() == local_def(fn_id))
1980+
}
1981+
_ => false
1982+
}
19781983
}
19791984

19801985
// check if the method call `id` refers to method `method_id`
@@ -2002,6 +2007,15 @@ impl LintPass for UnconditionalRecursion {
20022007
// method instead.
20032008
ty::MethodTypeParam(
20042009
ty::MethodParam { ref trait_ref, method_num, impl_def_id: None, }) => {
2010+
2011+
let on_self = m.substs.self_ty().map_or(false, |t| t.is_self());
2012+
if !on_self {
2013+
// we can only be recurring in a default
2014+
// method if we're being called literally
2015+
// on the `Self` type.
2016+
return false
2017+
}
2018+
20052019
tcx.trait_item(trait_ref.def_id, method_num).def_id()
20062020
}
20072021

src/test/compile-fail/lint-unconditional-recursion.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,15 @@ impl Baz {
6363
}
6464
}
6565

66+
fn all_fine() {
67+
let _f = all_fine;
68+
}
69+
70+
// issue 26333
71+
trait Bar {
72+
fn method<T: Bar>(&self, x: &T) {
73+
x.method(x)
74+
}
75+
}
76+
6677
fn main() {}

0 commit comments

Comments
 (0)