Skip to content

Commit b1931e4

Browse files
committed
lint: only consider actual calls as unconditional recursion.
Specifically, just mentioning the function name as a value is fine, as long as it isn't called, e.g. `fn main() { let _ = main; }`. Closes #21705.
1 parent fe283b4 commit b1931e4

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/librustc_lint/builtin.rs

Lines changed: 7 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`

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

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

66+
fn all_fine() {
67+
let _f = all_fine;
68+
}
69+
6670
fn main() {}

0 commit comments

Comments
 (0)