-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
using unused arguments #3423
Comments
Historically I believe the underscore doesn't signify "this variable is unused" so much as it means "don't bother me for not using this variable". It's true that nowadays we have |
Code is not just for communication with the compiler though. Arguably it's at least as important to communicate with other programmers (or yourself six months from now). If _x means that _x is not used then the intent of the code is very clear and, even better, enforced by the compiler. If it merely means that warnings or errors are suppressed then not only can readers not rely on the intent, but they can easily become actively misled. |
Nominating for maturity milestone 1 (well-defined) |
On 2013-04-29 16:26:37, Jesse Jones wrote:
Having it be enforced (as in throw an error) would impact macros that generate functions, since the contents of the function may not be used, but it's not known to the macro author. |
@jesse99 I'm of the opinion that if you really don't want your variable to ever be used, name it as a single underscore so that it cannot ever be referred to. Also, leaving around unused variables seems like a code smell. For example, if you have |
To illustrate my first point, this compiles: fn main() {
make_five(1);
}
fn make_five(_: int) -> int
{
5
} But you can't ever use the variable: fn main() {
make_five(1);
}
fn make_five(_: int) -> int
{
_ + 5 // error: unexpected token: `_`
} |
Well like I said before readability is important. And _options is a hell of a lot easier to grok than _. Not sure that I'd agree that unused variables are a code smell. They can easily happen with trait implementations where only some need to use certain arguments. And also during development when the code dealing with some argument isn't written yet. |
I agree with bstrie (i.e., that |
just a bug, removing milestone/nomination. |
The use case for this seems sufficiently narrow that I'm comfortable closing this bug. I suggest that you merely ignore the variable entirely by binding it to |
test-cargo-miri: add proc-macro2 This is already in the dependency tree of `serde_derive`, but I guess there is is a host dependency, here it is a target dependency. The logic is presumably the same as in anyhow, so we don't need both; let's test the one that is more widely used.
Seems to me if an argument is marked unused (with an underscore) then there should be a warning or error if it is actually used. For example the below compiles and runs just fine:
The text was updated successfully, but these errors were encountered: