Skip to content

Handle two edge cases in the unconditional recursion lint #26666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1973,8 +1973,13 @@ impl LintPass for UnconditionalRecursion {
fn_id: ast::NodeId,
_: ast::Ident,
id: ast::NodeId) -> bool {
tcx.def_map.borrow().get(&id)
.map_or(false, |def| def.def_id() == local_def(fn_id))
match tcx.map.get(id) {
ast_map::NodeExpr(&ast::Expr { node: ast::ExprCall(ref callee, _), .. }) => {
tcx.def_map.borrow().get(&callee.id)
.map_or(false, |def| def.def_id() == local_def(fn_id))
}
_ => false
}
}

// check if the method call `id` refers to method `method_id`
Expand Down Expand Up @@ -2002,6 +2007,15 @@ impl LintPass for UnconditionalRecursion {
// method instead.
ty::MethodTypeParam(
ty::MethodParam { ref trait_ref, method_num, impl_def_id: None, }) => {

let on_self = m.substs.self_ty().map_or(false, |t| t.is_self());
if !on_self {
// we can only be recurring in a default
// method if we're being called literally
// on the `Self` type.
return false
}

tcx.trait_item(trait_ref.def_id, method_num).def_id()
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/compile-fail/lint-unconditional-recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@ impl Baz {
}
}

fn all_fine() {
let _f = all_fine;
}

// issue 26333
trait Bar {
fn method<T: Bar>(&self, x: &T) {
x.method(x)
}
}

fn main() {}