Skip to content

Recursion lint #428

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

Open
Pyriphlegethon opened this issue Nov 1, 2015 · 2 comments
Open

Recursion lint #428

Pyriphlegethon opened this issue Nov 1, 2015 · 2 comments
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types

Comments

@Pyriphlegethon
Copy link
Contributor

Although recursion isn't bad in and of itself, it can often lead to infinite loops. Also some companies don't allow their programmers to use recursion (See this well known paper by NASA).
Jonathan Blow implemented a similar lint for his new programming language in this video.
This lint should probably be allow by default, because in most cases recursion is fine to use.

@Manishearth Manishearth added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types A-lint Area: New lints labels Nov 1, 2015
@rgiot
Copy link

rgiot commented Jan 22, 2019

I guess this issue has not been solved because it is quite complex to do.
There is however a simple pattern that may be detected without too much difficulties (I personnaly do quite often this mistake):

struct MyStruct {
 a_field: Vec<Something>
}

impl MyStruct {
 pub fn a_field(&self) -> &[Something] {
   &self.a_field()
 }
}

instead of

struct MyStruct {
 a_field: Vec<Something>
}

impl MyStruct {
 pub fn a_field(&self) -> &[Something] {
   &self.a_field
 }
}

@oli-obk
Copy link
Contributor

oli-obk commented Jan 23, 2019

direct recursion should already be caught by rustc: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0c6c409030e91d68f66607292b736332

warning: function cannot return without recursing
 --> src/lib.rs:8:5
  |
8 |     pub fn a_field(&self) -> &[Something] {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
9 |         &self.a_field()
  |          -------------- recursive call site
  |
  = note: #[warn(unconditional_recursion)] on by default
  = help: a `loop` may express intention better if this is on purpose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

4 participants