Skip to content

Can we stop rustc --test warning of an unused main()? #12327

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

Closed
chris-morgan opened this issue Feb 16, 2014 · 15 comments
Closed

Can we stop rustc --test warning of an unused main()? #12327

chris-morgan opened this issue Feb 16, 2014 · 15 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@chris-morgan
Copy link
Member

$ echo 'fn main(){}' | rustc --test -
<anon>:1:1: 1:12 warning: code is never used: `main`, #[warn(dead_code)] on by default
<anon>:1 fn main(){}
         ^~~~~~~~~~~

I have put #[cfg(not(test))] on main a couple of times (though perhaps #[allow(dead_code)] would be better) and I have just observed another person doing it. Would it be reasonable to make the dead code lint permit main to be unused?

@ghost
Copy link

ghost commented Feb 17, 2014

Perhaps it would be better to disable all warnings by default in test mode. Some people use tests as a "poor man's REPL", and warnings are just noise there.

@huonw
Copy link
Member

huonw commented Feb 17, 2014

The dead_code lint is really helpful for indicating tests that are missing their #[test] (and also the lints are useful for generally making sure that the tests do what they're supposed to).

@nixpulvis
Copy link

👍 for ignoring unused main. I agree keeping other dead code warning is defiantly a good idea, but warning about main is just noise.

@doy
Copy link
Contributor

doy commented Jul 23, 2014

+1 to specifically ignoring main, but keeping other warnings.

@nixpulvis
Copy link

On a related note, ignoring main seems to ignore things when compiled with --test when trying to circumvent this issue.

@brycefisher
Copy link

:+ for ignoring main

@steveklabnik
Copy link
Member

I'm interested in sheparding a patch like this, if anyone wants to give it a shot.

@Siosm
Copy link
Contributor

Siosm commented Dec 16, 2014

@steveklabnik I've made a first try to fix this. This tentative fix is not valid but I already got failed tests that I'm investigating.
I think that I need to find a way to get the attributes associated with a function to check if it as the 'start' or 'main' attribute. The current documentation is unclear on this specific point (see http://doc.rust-lang.org/reference.html#function-only-attributes and http://doc.rust-lang.org/reference.html#marker-types).
Also, are functions starting with a '_' internal only? (src/librustc/middle/dead.rs:521)

@steveklabnik
Copy link
Member

Starting with a _ is intended to supress warnings, yeah

@huonw
Copy link
Member

huonw commented Dec 16, 2014

@Siosm, NB. it is purely a warning/conventions thing, it just controls the lints, it doesn't have any effect on what is accessible externally or anything like that (that's the pub keyword).

@Siosm
Copy link
Contributor

Siosm commented Dec 16, 2014

@huonw Ok. Is this documented somewhere? So far I can not find any mention of it in the reference or in the guide.

@felixc
Copy link
Contributor

felixc commented Jan 11, 2015

This issue appears to also affect the Emacs Flycheck mode for Rust. Emacs users will get Flycheck warnings saying main() is dead code. Arguably it's a bug in their end (is it?), but it's another data point to suggest it's unexpected behaviour.

@steveklabnik steveklabnik added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Jan 23, 2015
ryanhiebert added a commit to ryanhiebert/rust-chess that referenced this issue Apr 4, 2015
In addition to being a solid library, the chess package should also end
up as a decent command line program with a playable game. This is
possible because a package can be a library as well as a binary.

Because the main function isn't tested, and may be difficult to test,
using submodules is a pain, because they show up as unused imports. The
main function as well shows up as an unused function when testing. Since
main is such a special case, there is an open issue to make the test
runner not show that warning.

rust-lang/rust#12327

For the time being though, I've done as the OP of that issue said he
does: putting #[cfg(not(test))] before the main function so that the
linter ignores it.
@tbelaire
Copy link
Contributor

It'd be also a good idea to not just use #[cfg(not(test))] on main, since that'll cause spurious import not used errors on functions that are just imported for use in main.

felinira added a commit to Drakulix/zwreec that referenced this issue May 1, 2015
This fixes #18 by including std::thread
The dead code warning is not fixable and related to rust bug #12327 rust-lang/rust#12327
@daboross
Copy link
Contributor

@tbelaire Using #[cfg_attr(test, allow(dead_code))] instead of #[cfg(not(test))] would fix this error without causing import not used errors. It doesn't look the best, but it does do that.

steveklabnik added a commit to steveklabnik/rust-interfaces that referenced this issue Jul 21, 2015
wthrowe added a commit to wthrowe/rust that referenced this issue Aug 25, 2015
bors added a commit that referenced this issue Aug 26, 2015
* Suppresses warnings that main is unused when testing (#12327)
* Makes `--test` work with explicit `#[start]` (#11766)
* Fixes some cases where the normal main would not be disabled by `--test`, resulting in compilation failures.
@steveklabnik
Copy link
Member

🎊

sourcefrog added a commit to sourcefrog/conserve that referenced this issue Jul 5, 2016
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2022
…jonas-schievink

internal: Rename `Expr::Lambda` to `Expr::Closure`

They're never really called "Lambda"s in Rust, so let's call them "Closures" consistently.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

10 participants