Skip to content
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

rustc should probably warn on anonymous, unused closure statement #74691

Closed
rphmeier opened this issue Jul 23, 2020 · 2 comments · Fixed by #74869
Closed

rustc should probably warn on anonymous, unused closure statement #74691

rphmeier opened this issue Jul 23, 2020 · 2 comments · Fixed by #74869
Labels
A-closures Area: Closures (`|…| { … }`) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@rphmeier
Copy link
Contributor

rphmeier commented Jul 23, 2020

Not quite sure how to name this issue, please change the title to something more accurate!

Recently ran into this issue:

fn main() {
    let b = false; // dangerous semi-colon
        || true;
        
    println!("{}", b);
}

The boolean statement was long, so I broke it out onto a new line. However, I accidentally added the annotated semi-colon. The || true line is interpreted as an anonymous closure statement (no let binding, not even a callable closure) as opposed to a boolean OR.

When executing this code, it yields false. Luckily, someone caught the issue in code review, but a compiler warning noting the unused closure would have been really helpful!

@rphmeier
Copy link
Contributor Author

After playing around a bit, it seems like one way to accomplish this would be to make the anonymous closure types #[must_use]. Would that be a reasonable approach? What are the downsides?

@jyn514 jyn514 closed this as completed Jul 23, 2020
@jyn514 jyn514 reopened this Jul 23, 2020
@jyn514
Copy link
Member

jyn514 commented Jul 23, 2020

must_use is clever, I like that! Closures have some special casing in the compiler though, so I'm not exactly sure how to implement it, it would be a change to rustc and not to libstd.

@jyn514 jyn514 added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. A-closures Area: Closures (`|…| { … }`) labels Jul 23, 2020
@bors bors closed this as completed in 4230f96 Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants